Make unused_allocation lint warn against Box::new

This commit is contained in:
Maybe Waffle 2022-11-13 13:55:04 +00:00
parent 18caf88956
commit 9ac0da8f39
3 changed files with 8 additions and 2 deletions

View File

@ -1349,9 +1349,8 @@ declare_lint! {
/// ### Example /// ### Example
/// ///
/// ```rust /// ```rust
/// #![feature(box_syntax)]
/// fn main() { /// 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<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
match e.kind { match e.kind {
hir::ExprKind::Box(_) => {} 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, _ => return,
} }

View File

@ -429,6 +429,7 @@ symbols! {
borrowck_graphviz_format, borrowck_graphviz_format,
borrowck_graphviz_postflow, borrowck_graphviz_postflow,
box_free, box_free,
box_new,
box_patterns, box_patterns,
box_syntax, box_syntax,
bpf_target_feature, bpf_target_feature,

View File

@ -214,6 +214,7 @@ impl<T> Box<T> {
#[inline(always)] #[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[must_use] #[must_use]
#[rustc_diagnostic_item = "box_new"]
pub fn new(x: T) -> Self { pub fn new(x: T) -> Self {
#[rustc_box] #[rustc_box]
Box::new(x) Box::new(x)