|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | +use rustc_span::Symbol; |
| 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 | + |
| 37 | +pub struct UnnecessaryBoxReturns { |
| 38 | + avoid_breaking_exported_api: bool, |
| 39 | +} |
| 40 | + |
| 41 | +impl_lint_pass!(UnnecessaryBoxReturns => [UNNECESSARY_BOX_RETURNS]); |
| 42 | + |
| 43 | +impl UnnecessaryBoxReturns { |
| 44 | + pub fn new(avoid_breaking_exported_api: bool) -> Self { |
| 45 | + Self { |
| 46 | + avoid_breaking_exported_api, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn check_fn_item(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId, name: Symbol) { |
| 51 | + // we don't want to tell someone to break an exported function if they ask us not to |
| 52 | + if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // functions which contain the word "box" are exempt from this lint |
| 57 | + if name.as_str().contains("box") { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
| 62 | + |
| 63 | + let return_ty = cx |
| 64 | + .tcx |
| 65 | + .erase_late_bound_regions(cx.tcx.fn_sig(def_id).skip_binder()) |
| 66 | + .output(); |
| 67 | + |
| 68 | + if !return_ty.is_box() { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + let boxed_ty = return_ty.boxed_ty(); |
| 73 | + |
| 74 | + // it's sometimes useful to return Box<T> if T is unsized, so don't lint those |
| 75 | + if boxed_ty.is_sized(cx.tcx, cx.param_env) { |
| 76 | + span_lint_and_then( |
| 77 | + cx, |
| 78 | + UNNECESSARY_BOX_RETURNS, |
| 79 | + return_ty_hir.span, |
| 80 | + format!("boxed return of the sized type `{boxed_ty}`").as_str(), |
| 81 | + |diagnostic| { |
| 82 | + diagnostic.span_suggestion( |
| 83 | + return_ty_hir.span, |
| 84 | + "try", |
| 85 | + boxed_ty.to_string(), |
| 86 | + // the return value and function callers also needs to |
| 87 | + // be changed, so this can't be MachineApplicable |
| 88 | + Applicability::Unspecified, |
| 89 | + ); |
| 90 | + diagnostic.help("changing this also requires a change to the return expressions in this function"); |
| 91 | + }, |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl LateLintPass<'_> for UnnecessaryBoxReturns { |
| 98 | + fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { |
| 99 | + let TraitItemKind::Fn(signature, _) = &item.kind else { return }; |
| 100 | + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); |
| 101 | + } |
| 102 | + |
| 103 | + fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) { |
| 104 | + // Ignore implementations of traits, because the lint should be on the |
| 105 | + // trait, not on the implmentation of it. |
| 106 | + let Node::Item(parent) = cx.tcx.hir().get_parent(item.hir_id()) else { return }; |
| 107 | + let ItemKind::Impl(parent) = parent.kind else { return }; |
| 108 | + if parent.of_trait.is_some() { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + let ImplItemKind::Fn(signature, ..) = &item.kind else { return }; |
| 113 | + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); |
| 114 | + } |
| 115 | + |
| 116 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 117 | + let ItemKind::Fn(signature, ..) = &item.kind else { return }; |
| 118 | + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); |
| 119 | + } |
| 120 | +} |
0 commit comments