@@ -1149,18 +1149,17 @@ declare_lint! {
11491149 /// ### Example
11501150 ///
11511151 /// ```rust
1152- /// #![feature(box_syntax)]
11531152 /// fn main() {
1154- /// let a = (box [1, 2, 3]).len();
1153+ /// let a = (Box::new( [1, 2, 3]) ).len();
11551154 /// }
11561155 /// ```
11571156 ///
11581157 /// {{produces}}
11591158 ///
11601159 /// ### Explanation
11611160 ///
1162- /// When a `box ` expression is immediately coerced to a reference, then
1163- /// the allocation is unnecessary, and a reference (using `&` or `&mut`)
1161+ /// When a `Box::new() ` expression is immediately coerced to a reference,
1162+ /// then the allocation is unnecessary, and a reference (using `&` or `&mut`)
11641163 /// should be used instead to avoid the allocation.
11651164 pub ( super ) UNUSED_ALLOCATION ,
11661165 Warn ,
@@ -1171,14 +1170,35 @@ declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
11711170
11721171impl < ' tcx > LateLintPass < ' tcx > for UnusedAllocation {
11731172 fn check_expr ( & mut self , cx : & LateContext < ' _ > , e : & hir:: Expr < ' _ > ) {
1174- match e. kind {
1175- hir:: ExprKind :: Box ( _) => { }
1173+ let span = match e. kind {
1174+ hir:: ExprKind :: Box ( _) => {
1175+ // Ideally, we'd underline the `box` part of the expression here,
1176+ // but at this point we have lost the span of the `box` keyword.
1177+ // Constructing a span from the inner and the entire expression's
1178+ // span won't work in many cases, so we just return the entire
1179+ // span of the `box foo`.
1180+ e. span
1181+ }
1182+ hir:: ExprKind :: Call ( ref callee, _) => {
1183+ // Look for Box::new(foo)
1184+ if let hir:: ExprKind :: Path ( ref qpath) = callee. kind &&
1185+ // `Res::Local` indicates a closure
1186+ let Res :: Def ( _, def_id) = cx. qpath_res ( qpath, callee. hir_id ) &&
1187+ let Some ( owned_box_new_def_id) = cx. tcx . lang_items ( ) . owned_box_new ( ) &&
1188+ def_id == owned_box_new_def_id
1189+ {
1190+ // We have a Box::new() call here
1191+ callee. span
1192+ } else {
1193+ return
1194+ }
1195+ }
11761196 _ => return ,
1177- }
1197+ } ;
11781198
11791199 for adj in cx. typeck_results ( ) . expr_adjustments ( e) {
11801200 if let adjustment:: Adjust :: Borrow ( adjustment:: AutoBorrow :: Ref ( _, m) ) = adj. kind {
1181- cx. struct_span_lint ( UNUSED_ALLOCATION , e . span , |lint| {
1201+ cx. struct_span_lint ( UNUSED_ALLOCATION , span, |lint| {
11821202 lint. build ( match m {
11831203 adjustment:: AutoBorrowMutability :: Not => fluent:: lint:: unused_allocation,
11841204 adjustment:: AutoBorrowMutability :: Mut { .. } => {
0 commit comments