|
1 | 1 | use clippy_utils::{diagnostics::span_lint_and_sugg, ty::implements_trait}; |
2 | 2 | use rustc_errors::Applicability; |
3 | | -use rustc_hir::{intravisit::FnKind, Body, FnDecl, FnRetTy, HirId, TraitItem, TraitItemKind}; |
| 3 | +use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, Item, ItemKind, TraitItem, TraitItemKind}; |
4 | 4 | use rustc_hir_analysis::hir_ty_to_ty; |
5 | 5 | use rustc_lint::{LateContext, LateLintPass}; |
6 | | -use rustc_session::{declare_lint_pass, declare_tool_lint}; |
7 | | -use rustc_span::Span; |
| 6 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
8 | 7 |
|
9 | 8 | declare_clippy_lint! { |
10 | 9 | /// ### What it does |
@@ -34,56 +33,62 @@ declare_clippy_lint! { |
34 | 33 | nursery, |
35 | 34 | "Needlessly returning a Box" |
36 | 35 | } |
37 | | -declare_lint_pass!(UnnecessaryBoxReturns => [UNNECESSARY_BOX_RETURNS]); |
38 | 36 |
|
39 | | -fn check_fn_decl(cx: &LateContext<'_>, decl: &FnDecl<'_>) { |
40 | | - let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
| 37 | +pub struct UnnecessaryBoxReturns { |
| 38 | + avoid_breaking_exported_api: bool, |
| 39 | +} |
41 | 40 |
|
42 | | - // this is safe, since we're not in a body |
43 | | - let return_ty = hir_ty_to_ty(cx.tcx, return_ty_hir); |
| 41 | +impl_lint_pass!(UnnecessaryBoxReturns => [UNNECESSARY_BOX_RETURNS]); |
44 | 42 |
|
45 | | - if !return_ty.is_box() { |
46 | | - return; |
| 43 | +impl UnnecessaryBoxReturns { |
| 44 | + pub fn new(avoid_breaking_exported_api: bool) -> Self { |
| 45 | + Self { |
| 46 | + avoid_breaking_exported_api, |
| 47 | + } |
47 | 48 | } |
48 | 49 |
|
49 | | - let boxed_ty = return_ty.boxed_ty(); |
50 | | - let Some(sized_trait) = cx.tcx.lang_items().sized_trait() else { return }; |
| 50 | + fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId) { |
| 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 | + let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
| 57 | + |
| 58 | + // this is safe, since we're not in a body |
| 59 | + let return_ty = hir_ty_to_ty(cx.tcx, return_ty_hir); |
51 | 60 |
|
52 | | - // it's sometimes useful to return Box<T> if T is unsized, so don't lint those |
53 | | - if implements_trait(cx, boxed_ty, sized_trait, &[]) { |
54 | | - span_lint_and_sugg( |
55 | | - cx, |
56 | | - UNNECESSARY_BOX_RETURNS, |
57 | | - return_ty_hir.span, |
58 | | - format!("boxed return of the sized type `{boxed_ty}`").as_str(), |
59 | | - "try", |
60 | | - boxed_ty.to_string(), |
61 | | - // the return value and function callers also needs to be changed, so this can't be MachineApplicable |
62 | | - Applicability::Unspecified, |
63 | | - ); |
| 61 | + if !return_ty.is_box() { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + let boxed_ty = return_ty.boxed_ty(); |
| 66 | + let Some(sized_trait) = cx.tcx.lang_items().sized_trait() else { return }; |
| 67 | + |
| 68 | + // it's sometimes useful to return Box<T> if T is unsized, so don't lint those |
| 69 | + if implements_trait(cx, boxed_ty, sized_trait, &[]) { |
| 70 | + span_lint_and_sugg( |
| 71 | + cx, |
| 72 | + UNNECESSARY_BOX_RETURNS, |
| 73 | + return_ty_hir.span, |
| 74 | + format!("boxed return of the sized type `{boxed_ty}`").as_str(), |
| 75 | + "try", |
| 76 | + boxed_ty.to_string(), |
| 77 | + // the return value and function callers also needs to be changed, so this can't be MachineApplicable |
| 78 | + Applicability::Unspecified, |
| 79 | + ); |
| 80 | + } |
64 | 81 | } |
65 | 82 | } |
66 | 83 |
|
67 | 84 | impl LateLintPass<'_> for UnnecessaryBoxReturns { |
68 | 85 | fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { |
69 | 86 | let TraitItemKind::Fn(signature, _) = &item.kind else { return }; |
70 | | - check_fn_decl(cx, signature.decl); |
| 87 | + self.check_fn_decl(cx, signature.decl, item.owner_id.def_id); |
71 | 88 | } |
72 | 89 |
|
73 | | - fn check_fn( |
74 | | - &mut self, |
75 | | - cx: &LateContext<'_>, |
76 | | - fn_kind: FnKind<'_>, |
77 | | - decl: &FnDecl<'_>, |
78 | | - _: &Body<'_>, |
79 | | - _: Span, |
80 | | - _: HirId, |
81 | | - ) { |
82 | | - // It's unclear what part of a closure you would span, so for now it's ignored. |
83 | | - // Trait implementations should also not be linted. |
84 | | - // If this is changed, please also make sure not to call `hir_ty_to_ty` below. |
85 | | - let FnKind::ItemFn(..) = fn_kind else { return }; |
86 | | - |
87 | | - check_fn_decl(cx, decl); |
| 90 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 91 | + let ItemKind::Fn(signature, ..) = &item.kind else { return }; |
| 92 | + self.check_fn_decl(cx, signature.decl, item.owner_id.def_id); |
88 | 93 | } |
89 | 94 | } |
0 commit comments