|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then;
|
2 | 2 | use rustc_errors::Applicability;
|
3 |
| -use rustc_hir::{def_id::LocalDefId, intravisit::FnKind, Body, FnDecl, FnRetTy}; |
| 3 | +use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind}; |
4 | 4 | use rustc_lint::{LateContext, LateLintPass};
|
5 | 5 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
6 |
| -use rustc_span::Span; |
7 | 6 |
|
8 | 7 | declare_clippy_lint! {
|
9 | 8 | /// ### What it does
|
@@ -35,54 +34,63 @@ declare_clippy_lint! {
|
35 | 34 | }
|
36 | 35 | declare_lint_pass!(UnnecessaryBoxReturns => [UNNECESSARY_BOX_RETURNS]);
|
37 | 36 |
|
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 |
| - } |
| 37 | +fn check_fn_decl(cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId) { |
| 38 | + let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
53 | 39 |
|
54 |
| - let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; |
| 40 | + let return_ty = cx |
| 41 | + .tcx |
| 42 | + .erase_late_bound_regions(cx.tcx.fn_sig(def_id).skip_binder()) |
| 43 | + .output(); |
55 | 44 |
|
56 |
| - let return_ty = cx |
57 |
| - .tcx |
58 |
| - .erase_late_bound_regions(cx.tcx.fn_sig(def_id).skip_binder()) |
59 |
| - .output(); |
| 45 | + if !return_ty.is_box() { |
| 46 | + return; |
| 47 | + } |
60 | 48 |
|
61 |
| - if !return_ty.is_box() { |
| 49 | + let boxed_ty = return_ty.boxed_ty(); |
| 50 | + |
| 51 | + // it's sometimes useful to return Box<T> if T is unsized, so don't lint those |
| 52 | + if boxed_ty.is_sized(cx.tcx, cx.param_env) { |
| 53 | + span_lint_and_then( |
| 54 | + cx, |
| 55 | + UNNECESSARY_BOX_RETURNS, |
| 56 | + return_ty_hir.span, |
| 57 | + format!("boxed return of the sized type `{boxed_ty}`").as_str(), |
| 58 | + |diagnostic| { |
| 59 | + diagnostic.span_suggestion( |
| 60 | + return_ty_hir.span, |
| 61 | + "try", |
| 62 | + boxed_ty.to_string(), |
| 63 | + // the return value and function callers also needs to |
| 64 | + // be changed, so this can't be MachineApplicable |
| 65 | + Applicability::Unspecified, |
| 66 | + ); |
| 67 | + diagnostic.help("changing this also requires a change to the return expressions in this function"); |
| 68 | + }, |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl LateLintPass<'_> for UnnecessaryBoxReturns { |
| 74 | + fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { |
| 75 | + let TraitItemKind::Fn(signature, _) = &item.kind else { return }; |
| 76 | + check_fn_decl(cx, signature.decl, item.owner_id.def_id); |
| 77 | + } |
| 78 | + |
| 79 | + fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) { |
| 80 | + // Ignore implementations of traits, because the lint should be on the |
| 81 | + // trait, not on the implmentation of it. |
| 82 | + let Node::Item(parent) = cx.tcx.hir().get_parent(item.hir_id()) else { return }; |
| 83 | + let ItemKind::Impl(parent) = parent.kind else { return }; |
| 84 | + if parent.of_trait.is_some() { |
62 | 85 | return;
|
63 | 86 | }
|
64 | 87 |
|
65 |
| - let boxed_ty = return_ty.boxed_ty(); |
| 88 | + let ImplItemKind::Fn(signature, ..) = &item.kind else { return }; |
| 89 | + check_fn_decl(cx, signature.decl, item.owner_id.def_id); |
| 90 | + } |
66 | 91 |
|
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 |
| - } |
| 92 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 93 | + let ItemKind::Fn(signature, ..) = &item.kind else { return }; |
| 94 | + check_fn_decl(cx, signature.decl, item.owner_id.def_id); |
87 | 95 | }
|
88 | 96 | }
|
0 commit comments