|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::{is_from_proc_macro, is_in_test_function}; |
| 3 | +use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_hir::def_id::LocalDefId; |
| 5 | +use rustc_hir::intravisit::{walk_expr, Visitor}; |
| 6 | +use rustc_hir::{intravisit::FnKind, Body, Expr, ExprKind, FnDecl}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 8 | +use rustc_middle::hir::nested_filter::OnlyBodies; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for functions that are only used once. Does not lint tests. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// It's usually not, splitting a function into multiple parts often improves readability and in |
| 19 | + /// the case of generics, can prevent the compiler from duplicating the function dozens of |
| 20 | + /// time; instead, only duplicating a thunk. But this can prevent segmentation across a |
| 21 | + /// codebase, where many small functions are used only once. |
| 22 | + /// |
| 23 | + /// Note: If this lint is used, prepare to allow this a lot. |
| 24 | + /// |
| 25 | + /// ### Example |
| 26 | + /// ```rust |
| 27 | + /// pub fn a<T>(t: &T) |
| 28 | + /// where |
| 29 | + /// T: AsRef<str>, |
| 30 | + /// { |
| 31 | + /// a_inner(t.as_ref()) |
| 32 | + /// } |
| 33 | + /// |
| 34 | + /// fn a_inner(t: &str) { |
| 35 | + /// /* snip */ |
| 36 | + /// } |
| 37 | + /// |
| 38 | + /// ``` |
| 39 | + /// Use instead: |
| 40 | + /// ```rust |
| 41 | + /// pub fn a<T>(t: &T) |
| 42 | + /// where |
| 43 | + /// T: AsRef<str>, |
| 44 | + /// { |
| 45 | + /// let t = t.as_ref(); |
| 46 | + /// /* snip */ |
| 47 | + /// } |
| 48 | + /// |
| 49 | + /// ``` |
| 50 | + #[clippy::version = "1.72.0"] |
| 51 | + pub SINGLE_CALL_FN, |
| 52 | + restriction, |
| 53 | + "checks for functions that are only used once" |
| 54 | +} |
| 55 | +impl_lint_pass!(SingleCallFn => [SINGLE_CALL_FN]); |
| 56 | + |
| 57 | +#[derive(Clone)] |
| 58 | +pub struct SingleCallFn { |
| 59 | + pub avoid_breaking_exported_api: bool, |
| 60 | + pub def_id_to_usage: FxHashMap<LocalDefId, (Span, Vec<Span>)>, |
| 61 | +} |
| 62 | + |
| 63 | +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_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span)) |
| 76 | + || is_in_test_function(cx.tcx, body.value.hir_id) |
| 77 | + { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + self.def_id_to_usage.insert(def_id, (span, vec![])); |
| 82 | + } |
| 83 | + |
| 84 | + 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 | + for usage in self.def_id_to_usage.values() { |
| 92 | + let single_call_fn_span = usage.0; |
| 93 | + if let [caller_span] = *usage.1 { |
| 94 | + span_lint_and_help( |
| 95 | + cx, |
| 96 | + SINGLE_CALL_FN, |
| 97 | + single_call_fn_span, |
| 98 | + "this function is only used once", |
| 99 | + Some(caller_span), |
| 100 | + "used here", |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +struct FnUsageVisitor<'a, 'tcx> { |
| 108 | + cx: &'a LateContext<'tcx>, |
| 109 | + def_id_to_usage: &'a mut FxHashMap<LocalDefId, (Span, Vec<Span>)>, |
| 110 | +} |
| 111 | + |
| 112 | +impl<'a, 'tcx> Visitor<'tcx> for FnUsageVisitor<'a, 'tcx> { |
| 113 | + type NestedFilter = OnlyBodies; |
| 114 | + |
| 115 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 116 | + self.cx.tcx.hir() |
| 117 | + } |
| 118 | + |
| 119 | + fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) { |
| 120 | + let Self { cx, .. } = *self; |
| 121 | + |
| 122 | + if let ExprKind::Path(qpath) = expr.kind |
| 123 | + && let res = cx.qpath_res(&qpath, expr.hir_id) |
| 124 | + && let Some(call_def_id) = res.opt_def_id() |
| 125 | + && let Some(def_id) = call_def_id.as_local() |
| 126 | + && let Some(usage) = self.def_id_to_usage.get_mut(&def_id) |
| 127 | + { |
| 128 | + usage.1.push(expr.span); |
| 129 | + } |
| 130 | + |
| 131 | + walk_expr(self, expr); |
| 132 | + } |
| 133 | +} |
0 commit comments