|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::is_default_equivalent_call; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use clippy_utils::ty::implements_trait; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{Expr, ExprKind, LangItem}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty::{self, GenericArgKind, Ty}; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | +use rustc_span::sym; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Detects assignments of `Default::default()` to a place of type `Box<T>`. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// This incurs an extra heap allocation compared to assigning the boxed |
| 18 | + /// storage. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```no_run |
| 22 | + /// let mut b = Box::new(1u32); |
| 23 | + /// b = Default::default(); |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```no_run |
| 27 | + /// *b = Default::default(); |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.89.0"] |
| 30 | + pub DEFAULT_BOX_ASSIGNMENTS, |
| 31 | + perf, |
| 32 | + "assigning `Default::default()` to `Box<T>` is inefficient" |
| 33 | +} |
| 34 | +declare_lint_pass!(DefaultBoxAssignments => [DEFAULT_BOX_ASSIGNMENTS]); |
| 35 | + |
| 36 | +impl LateLintPass<'_> for DefaultBoxAssignments { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { |
| 38 | + if let ExprKind::Assign(lhs, rhs, _) = &expr.kind { |
| 39 | + let lhs_ty = cx.typeck_results().expr_ty(lhs); |
| 40 | + if is_box_of_default(lhs_ty, cx) && is_default_call(rhs, cx) { |
| 41 | + span_lint_and_then( |
| 42 | + cx, |
| 43 | + DEFAULT_BOX_ASSIGNMENTS, |
| 44 | + expr.span, |
| 45 | + "assigning `Default::default()` to `Box<T>`", |
| 46 | + |diag| { |
| 47 | + let suggestion = format!("*({}) = Default::default()", snippet(cx, lhs.span, "_")); |
| 48 | + |
| 49 | + diag.note("this creates a needless allocation").span_suggestion( |
| 50 | + expr.span, |
| 51 | + "assign to the inner value", |
| 52 | + suggestion, |
| 53 | + Applicability::MaybeIncorrect, |
| 54 | + ); |
| 55 | + }, |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn is_box_of_default<'a>(ty: Ty<'a>, cx: &LateContext<'a>) -> bool { |
| 63 | + if let ty::Adt(def, args) = ty.kind() |
| 64 | + && cx.tcx.lang_items().get(LangItem::OwnedBox) == Some(def.did()) |
| 65 | + && let Some(inner) = args.iter().find_map(|arg| match arg.kind() { |
| 66 | + GenericArgKind::Type(ty) => Some(ty), |
| 67 | + _ => None, |
| 68 | + }) |
| 69 | + { |
| 70 | + cx.tcx |
| 71 | + .get_diagnostic_item(sym::Default) |
| 72 | + .map_or(false, |id| implements_trait(cx, inner, id, &[])) |
| 73 | + } else { |
| 74 | + false |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn is_default_call(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool { |
| 79 | + if let ExprKind::Call(func, _args) = expr.kind |
| 80 | + && is_default_equivalent_call(cx, func, Some(expr)) |
| 81 | + { |
| 82 | + true |
| 83 | + } else { |
| 84 | + false |
| 85 | + } |
| 86 | +} |
0 commit comments