|
1 | 1 | use clippy_utils::diagnostics::span_lint_hir_and_then;
|
2 | 2 | use clippy_utils::{is_from_proc_macro, is_in_test_function};
|
3 |
| -use rustc_data_structures::fx::FxHashMap; |
| 3 | +use rustc_data_structures::fx::{FxIndexMap, IndexEntry}; |
| 4 | +use rustc_hir::def::DefKind; |
4 | 5 | use rustc_hir::def_id::LocalDefId;
|
5 |
| -use rustc_hir::intravisit::{walk_expr, FnKind, Visitor}; |
6 |
| -use rustc_hir::{Body, Expr, ExprKind, FnDecl}; |
| 6 | +use rustc_hir::{Expr, ExprKind, HirId, Node}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
8 |
| -use rustc_middle::hir::nested_filter::OnlyBodies; |
9 | 8 | use rustc_middle::lint::in_external_macro;
|
10 | 9 | use rustc_session::impl_lint_pass;
|
11 | 10 | use rustc_span::Span;
|
@@ -54,82 +53,93 @@ declare_clippy_lint! {
|
54 | 53 | }
|
55 | 54 | impl_lint_pass!(SingleCallFn => [SINGLE_CALL_FN]);
|
56 | 55 |
|
| 56 | +#[derive(Debug, Clone)] |
| 57 | +pub enum CallState { |
| 58 | + Once { call_site: Span }, |
| 59 | + Multiple, |
| 60 | +} |
| 61 | + |
57 | 62 | #[derive(Clone)]
|
58 | 63 | pub struct SingleCallFn {
|
59 | 64 | pub avoid_breaking_exported_api: bool,
|
60 |
| - pub def_id_to_usage: FxHashMap<LocalDefId, (Span, Vec<Span>)>, |
| 65 | + pub def_id_to_usage: FxIndexMap<LocalDefId, CallState>, |
| 66 | +} |
| 67 | + |
| 68 | +impl SingleCallFn { |
| 69 | + fn is_function_allowed( |
| 70 | + &self, |
| 71 | + cx: &LateContext<'_>, |
| 72 | + fn_def_id: LocalDefId, |
| 73 | + fn_hir_id: HirId, |
| 74 | + fn_span: Span, |
| 75 | + ) -> bool { |
| 76 | + (self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(fn_def_id)) |
| 77 | + || in_external_macro(cx.sess(), fn_span) |
| 78 | + || cx |
| 79 | + .tcx |
| 80 | + .hir() |
| 81 | + .maybe_body_owned_by(fn_def_id) |
| 82 | + .map(|body| cx.tcx.hir().body(body)) |
| 83 | + .map_or(true, |body| is_in_test_function(cx.tcx, body.value.hir_id)) |
| 84 | + || match cx.tcx.hir_node(fn_hir_id) { |
| 85 | + Node::Item(item) => is_from_proc_macro(cx, item), |
| 86 | + Node::ImplItem(item) => is_from_proc_macro(cx, item), |
| 87 | + Node::TraitItem(item) => is_from_proc_macro(cx, item), |
| 88 | + _ => true, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Whether a called function is a kind of item that the lint cares about. |
| 94 | +/// For example, calling an `extern "C" { fn fun(); }` only once is totally fine and does not |
| 95 | +/// to be considered. |
| 96 | +fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool { |
| 97 | + matches!( |
| 98 | + cx.tcx.hir_node(cx.tcx.local_def_id_to_hir_id(def_id)), |
| 99 | + Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_) |
| 100 | + ) |
61 | 101 | }
|
62 | 102 |
|
63 | 103 | impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
|
64 |
| - fn check_fn( |
65 |
| - &mut self, |
66 |
| - cx: &LateContext<'tcx>, |
67 |
| - kind: FnKind<'tcx>, |
68 |
| - _: &'tcx FnDecl<'_>, |
69 |
| - body: &'tcx Body<'_>, |
70 |
| - span: Span, |
71 |
| - def_id: LocalDefId, |
72 |
| - ) { |
73 |
| - if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) |
74 |
| - || in_external_macro(cx.sess(), span) |
75 |
| - || is_in_test_function(cx.tcx, body.value.hir_id) |
76 |
| - || is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span)) |
| 104 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) { |
| 105 | + if let ExprKind::Path(qpath) = expr.kind |
| 106 | + && let res = cx.qpath_res(&qpath, expr.hir_id) |
| 107 | + && let Some(call_def_id) = res.opt_def_id() |
| 108 | + && let Some(def_id) = call_def_id.as_local() |
| 109 | + && let DefKind::Fn | DefKind::AssocFn = cx.tcx.def_kind(def_id) |
| 110 | + && is_valid_item_kind(cx, def_id) |
77 | 111 | {
|
78 |
| - return; |
| 112 | + match self.def_id_to_usage.entry(def_id) { |
| 113 | + IndexEntry::Occupied(mut entry) => { |
| 114 | + if let CallState::Once { .. } = entry.get() { |
| 115 | + entry.insert(CallState::Multiple); |
| 116 | + } |
| 117 | + }, |
| 118 | + IndexEntry::Vacant(entry) => { |
| 119 | + entry.insert(CallState::Once { call_site: expr.span }); |
| 120 | + }, |
| 121 | + } |
79 | 122 | }
|
80 |
| - |
81 |
| - self.def_id_to_usage.insert(def_id, (span, vec![])); |
82 | 123 | }
|
83 | 124 |
|
84 | 125 | fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
85 |
| - let mut v = FnUsageVisitor { |
86 |
| - cx, |
87 |
| - def_id_to_usage: &mut self.def_id_to_usage, |
88 |
| - }; |
89 |
| - cx.tcx.hir().visit_all_item_likes_in_crate(&mut v); |
90 |
| - |
91 | 126 | for (&def_id, usage) in &self.def_id_to_usage {
|
92 |
| - let single_call_fn_span = usage.0; |
93 |
| - if let [caller_span] = *usage.1 { |
| 127 | + if let CallState::Once { call_site } = *usage |
| 128 | + && let fn_hir_id = cx.tcx.local_def_id_to_hir_id(def_id) |
| 129 | + && let fn_span = cx.tcx.hir().span_with_body(fn_hir_id) |
| 130 | + && !self.is_function_allowed(cx, def_id, fn_hir_id, fn_span) |
| 131 | + { |
94 | 132 | span_lint_hir_and_then(
|
95 | 133 | cx,
|
96 | 134 | SINGLE_CALL_FN,
|
97 |
| - cx.tcx.local_def_id_to_hir_id(def_id), |
98 |
| - single_call_fn_span, |
| 135 | + fn_hir_id, |
| 136 | + fn_span, |
99 | 137 | "this function is only used once",
|
100 | 138 | |diag| {
|
101 |
| - diag.span_help(caller_span, "used here"); |
| 139 | + diag.span_note(call_site, "used here"); |
102 | 140 | },
|
103 | 141 | );
|
104 | 142 | }
|
105 | 143 | }
|
106 | 144 | }
|
107 | 145 | }
|
108 |
| - |
109 |
| -struct FnUsageVisitor<'a, 'tcx> { |
110 |
| - cx: &'a LateContext<'tcx>, |
111 |
| - def_id_to_usage: &'a mut FxHashMap<LocalDefId, (Span, Vec<Span>)>, |
112 |
| -} |
113 |
| - |
114 |
| -impl<'a, 'tcx> Visitor<'tcx> for FnUsageVisitor<'a, 'tcx> { |
115 |
| - type NestedFilter = OnlyBodies; |
116 |
| - |
117 |
| - fn nested_visit_map(&mut self) -> Self::Map { |
118 |
| - self.cx.tcx.hir() |
119 |
| - } |
120 |
| - |
121 |
| - fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) { |
122 |
| - let Self { cx, .. } = *self; |
123 |
| - |
124 |
| - if let ExprKind::Path(qpath) = expr.kind |
125 |
| - && let res = cx.qpath_res(&qpath, expr.hir_id) |
126 |
| - && let Some(call_def_id) = res.opt_def_id() |
127 |
| - && let Some(def_id) = call_def_id.as_local() |
128 |
| - && let Some(usage) = self.def_id_to_usage.get_mut(&def_id) |
129 |
| - { |
130 |
| - usage.1.push(expr.span); |
131 |
| - } |
132 |
| - |
133 |
| - walk_expr(self, expr); |
134 |
| - } |
135 |
| -} |
|
0 commit comments