|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{def_id::LocalDefId, intravisit::FnKind, Body, FnDecl, FnRetTy}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 6 | +use rustc_span::Span; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// |
| 11 | + /// Checks for a return type containing a `Box<T>` where `T` implements `Sized` |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// |
| 15 | + /// It's better to just return `T` in these cases. The caller may not need |
| 16 | + /// the value to be boxed, and it's expensive to free the memory once the |
| 17 | + /// `Box<T>` been dropped. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```rust |
| 21 | + /// fn foo() -> Box<String> { |
| 22 | + /// Box::new(String::from("Hello, world!")) |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// fn foo() -> String { |
| 28 | + /// String::from("Hello, world!") |
| 29 | + /// } |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.70.0"] |
| 32 | + pub UNNECESSARY_BOX_RETURNS, |
| 33 | + pedantic, |
| 34 | + "Needlessly returning a Box" |
| 35 | +} |
| 36 | +declare_lint_pass!(UnnecessaryBoxReturns => [UNNECESSARY_BOX_RETURNS]); |
| 37 | + |
| 38 | +impl LateLintPass<'_> for UnnecessaryBoxReturns { |
| 39 | + fn check_fn( |
| 40 | + &mut self, |
| 41 | + cx: &LateContext<'_>, |
| 42 | + fn_kind: FnKind<'_>, |
| 43 | + decl: &FnDecl<'_>, |
| 44 | + _: &Body<'_>, |
| 45 | + _: Span, |
| 46 | + def_id: LocalDefId, |
| 47 | + ) { |
| 48 | + // it's unclear what part of a closure you would span, so for now it's ignored |
| 49 | + // if this is changed, please also make sure not to call `hir_ty_to_ty` below |
| 50 | + if matches!(fn_kind, FnKind::Closure) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
| 55 | + |
| 56 | + let return_ty = cx |
| 57 | + .tcx |
| 58 | + .erase_late_bound_regions(cx.tcx.fn_sig(def_id).skip_binder()) |
| 59 | + .output(); |
| 60 | + |
| 61 | + if !return_ty.is_box() { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + let boxed_ty = return_ty.boxed_ty(); |
| 66 | + |
| 67 | + // it's sometimes useful to return Box<T> if T is unsized, so don't lint those |
| 68 | + if boxed_ty.is_sized(cx.tcx, cx.param_env) { |
| 69 | + span_lint_and_then( |
| 70 | + cx, |
| 71 | + UNNECESSARY_BOX_RETURNS, |
| 72 | + return_ty_hir.span, |
| 73 | + format!("boxed return of the sized type `{boxed_ty}`").as_str(), |
| 74 | + |diagnostic| { |
| 75 | + diagnostic.span_suggestion( |
| 76 | + return_ty_hir.span, |
| 77 | + "try", |
| 78 | + boxed_ty.to_string(), |
| 79 | + // the return value and function callers also needs to |
| 80 | + // be changed, so this can't be MachineApplicable |
| 81 | + Applicability::Unspecified, |
| 82 | + ); |
| 83 | + diagnostic.help("changing this also requires a change to the return expressions in this function"); |
| 84 | + }, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments