|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use clippy_utils::ty::approx_ty_size; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{AmbigArg, Expr, ExprKind, GenericArg, Path, PathSegment, QPath, Ty, TyKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// ### Why is this bad? |
| 13 | + /// |
| 14 | + /// ### Example |
| 15 | + /// ```no_run |
| 16 | + /// // example code where clippy issues a warning |
| 17 | + /// ``` |
| 18 | + /// Use instead: |
| 19 | + /// ```no_run |
| 20 | + /// // example code which does not raise clippy warning |
| 21 | + /// ``` |
| 22 | + #[clippy::version = "1.88.0"] |
| 23 | + pub REDUNDANT_BOX, |
| 24 | + nursery, |
| 25 | + "default lint description" |
| 26 | +} |
| 27 | + |
| 28 | +// TODO Rename lint as we are not just checking references anymore |
| 29 | +declare_lint_pass!(RedundantBox => [REDUNDANT_BOX]); |
| 30 | + |
| 31 | +// TODO could we do everything with only check_ty() xor check_expr()? |
| 32 | +impl LateLintPass<'_> for RedundantBox { |
| 33 | + fn check_ty<'tcx>(&mut self, cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) { |
| 34 | + let ty = clippy_utils::ty::ty_from_hir_ty(cx, hir_ty.as_unambig_ty()); |
| 35 | + if let Some(boxed_ty) = ty.boxed_ty() |
| 36 | + && is_thin_type(cx, boxed_ty) |
| 37 | + // Extract the contained type for the lint suggestion span |
| 38 | + // TODO is there a simpler way to do this?: |
| 39 | + && let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = hir_ty.kind |
| 40 | + && let [PathSegment { args: Some(args), .. }] = segments |
| 41 | + && let [GenericArg::Type(ty)] = args.args |
| 42 | + { |
| 43 | + span_lint_and_sugg_(cx, hir_ty.span, ty.span); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { |
| 48 | + let ty = cx.typeck_results().expr_ty(expr); |
| 49 | + if let Some(boxed_ty) = ty.boxed_ty() |
| 50 | + && is_thin_type(cx, boxed_ty) |
| 51 | + && let ExprKind::Call(_, &[Expr { span, .. }]) = expr.kind |
| 52 | + { |
| 53 | + span_lint_and_sugg_(cx, expr.span, span); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn is_thin_type<'tcx>(cx: &LateContext<'tcx>, ty: rustc_middle::ty::Ty<'tcx>) -> bool { |
| 59 | + //TODO: usize's width will be the host's so lints may be misleading when the intended |
| 60 | + // target is a different architecture. Can/should we do someting about it? Maybe make it |
| 61 | + // configurable? |
| 62 | + ty.is_sized(cx.tcx, cx.typing_env()) && { |
| 63 | + let size = approx_ty_size(cx, ty); |
| 64 | + 0 < size && size <= size_of::<usize>() as u64 |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn span_lint_and_sugg_(cx: &LateContext<'_>, from_span: rustc_span::Span, to_span: rustc_span::Span) { |
| 69 | + span_lint_and_sugg( |
| 70 | + cx, |
| 71 | + REDUNDANT_BOX, |
| 72 | + from_span, |
| 73 | + "TODO: lint msg", |
| 74 | + "Remove Box", |
| 75 | + format!("{}", snippet(cx, to_span, "<default>")), |
| 76 | + Applicability::MachineApplicable, |
| 77 | + ); |
| 78 | +} |
0 commit comments