Skip to content

Commit 9ac0da8

Browse files
committed
Make unused_allocation lint warn against Box::new
1 parent 18caf88 commit 9ac0da8

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,9 +1349,8 @@ declare_lint! {
13491349
/// ### Example
13501350
///
13511351
/// ```rust
1352-
/// #![feature(box_syntax)]
13531352
/// fn main() {
1354-
/// let a = (box [1, 2, 3]).len();
1353+
/// let a = Box::new([1, 2, 3]).len();
13551354
/// }
13561355
/// ```
13571356
///
@@ -1373,6 +1372,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
13731372
fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
13741373
match e.kind {
13751374
hir::ExprKind::Box(_) => {}
1375+
hir::ExprKind::Call(path_expr, [_])
1376+
if let hir::ExprKind::Path(qpath) = &path_expr.kind
1377+
&& let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1378+
&& cx.tcx.is_diagnostic_item(sym::box_new, did)
1379+
=> {}
13761380
_ => return,
13771381
}
13781382

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ symbols! {
429429
borrowck_graphviz_format,
430430
borrowck_graphviz_postflow,
431431
box_free,
432+
box_new,
432433
box_patterns,
433434
box_syntax,
434435
bpf_target_feature,

library/alloc/src/boxed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl<T> Box<T> {
214214
#[inline(always)]
215215
#[stable(feature = "rust1", since = "1.0.0")]
216216
#[must_use]
217+
#[rustc_diagnostic_item = "box_new"]
217218
pub fn new(x: T) -> Self {
218219
#[rustc_box]
219220
Box::new(x)

0 commit comments

Comments
 (0)