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