From 9ac0da8f39680f1b09321980187bc502580b526a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 13 Nov 2022 13:55:04 +0000 Subject: [PATCH] Make `unused_allocation` lint warn against `Box::new` --- compiler/rustc_lint/src/unused.rs | 8 ++++++-- compiler/rustc_span/src/symbol.rs | 1 + library/alloc/src/boxed.rs | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 3a92f5806c9..b32fc96164a 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1349,9 +1349,8 @@ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { /// ### Example /// /// ```rust - /// #![feature(box_syntax)] /// fn main() { - /// let a = (box [1, 2, 3]).len(); + /// let a = Box::new([1, 2, 3]).len(); /// } /// ``` /// @@ -1373,6 +1372,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation { fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) { match e.kind { hir::ExprKind::Box(_) => {} + hir::ExprKind::Call(path_expr, [_]) + if let hir::ExprKind::Path(qpath) = &path_expr.kind + && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() + && cx.tcx.is_diagnostic_item(sym::box_new, did) + => {} _ => return, } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index fb579e4ff77..a76bf45ec89 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -429,6 +429,7 @@ borrowck_graphviz_format, borrowck_graphviz_postflow, box_free, + box_new, box_patterns, box_syntax, bpf_target_feature, diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 44a37899007..241b11c3f5f 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -214,6 +214,7 @@ impl Box { #[inline(always)] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] + #[rustc_diagnostic_item = "box_new"] pub fn new(x: T) -> Self { #[rustc_box] Box::new(x)