Skip to content

Commit 07dbcbd

Browse files
committed
new lint single_call_fn
1 parent 8c8ff5f commit 07dbcbd

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,6 +5158,7 @@ Released 2018-09-13
51585158
[`significant_drop_in_scrutinee`]: https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_in_scrutinee
51595159
[`significant_drop_tightening`]: https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
51605160
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
5161+
[`single_call_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_call_fn
51615162
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
51625163
[`single_char_lifetime_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_lifetime_names
51635164
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
570570
crate::shadow::SHADOW_SAME_INFO,
571571
crate::shadow::SHADOW_UNRELATED_INFO,
572572
crate::significant_drop_tightening::SIGNIFICANT_DROP_TIGHTENING_INFO,
573+
crate::single_call_fn::SINGLE_CALL_FN_INFO,
573574
crate::single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES_INFO,
574575
crate::single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS_INFO,
575576
crate::single_range_in_vec_init::SINGLE_RANGE_IN_VEC_INIT_INFO,

clippy_lints/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ mod semicolon_if_nothing_returned;
287287
mod serde_api;
288288
mod shadow;
289289
mod significant_drop_tightening;
290+
mod single_call_fn;
290291
mod single_char_lifetime_names;
291292
mod single_component_path_imports;
292293
mod single_range_in_vec_init;
@@ -1055,6 +1056,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10551056
store.register_late_pass(move |_| Box::new(large_stack_frames::LargeStackFrames::new(stack_size_threshold)));
10561057
store.register_late_pass(|_| Box::new(single_range_in_vec_init::SingleRangeInVecInit));
10571058
store.register_late_pass(|_| Box::new(incorrect_impls::IncorrectImpls));
1059+
store.register_late_pass(move |_| {
1060+
Box::new(single_call_fn::SingleCallFn {
1061+
avoid_breaking_exported_api,
1062+
def_id_to_usage: rustc_data_structures::fx::FxHashMap::default(),
1063+
})
1064+
});
10581065
// add lints here, do not remove this comment, it's used in `new_lint`
10591066
}
10601067

clippy_lints/src/single_call_fn.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::is_from_proc_macro;
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.
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+
{
77+
return;
78+
}
79+
80+
self.def_id_to_usage.insert(def_id, (span, vec![]));
81+
}
82+
83+
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
84+
let mut v = FnUsageVisitor {
85+
cx,
86+
def_id_to_usage: &mut self.def_id_to_usage,
87+
};
88+
cx.tcx.hir().visit_all_item_likes_in_crate(&mut v);
89+
90+
for usage in self.def_id_to_usage.values() {
91+
let fn_span = usage.0;
92+
if let [usage] = *usage.1 {
93+
span_lint_and_help(
94+
cx,
95+
SINGLE_CALL_FN,
96+
fn_span,
97+
"this function is only used once",
98+
Some(usage),
99+
"used here",
100+
);
101+
}
102+
}
103+
}
104+
}
105+
106+
struct FnUsageVisitor<'a, 'tcx> {
107+
cx: &'a LateContext<'tcx>,
108+
def_id_to_usage: &'a mut FxHashMap<LocalDefId, (Span, Vec<Span>)>,
109+
}
110+
111+
impl<'a, 'tcx> Visitor<'tcx> for FnUsageVisitor<'a, 'tcx> {
112+
type NestedFilter = OnlyBodies;
113+
114+
fn nested_visit_map(&mut self) -> Self::Map {
115+
self.cx.tcx.hir()
116+
}
117+
118+
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
119+
let Self { cx, .. } = *self;
120+
121+
if let ExprKind::Path(qpath) = expr.kind
122+
&& let res = cx.qpath_res(&qpath, expr.hir_id)
123+
&& let Some(call_def_id) = res.opt_def_id()
124+
&& let Some(def_id) = call_def_id.as_local()
125+
&& let Some(usage) = self.def_id_to_usage.get_mut(&def_id)
126+
{
127+
usage.1.push(expr.span);
128+
}
129+
130+
walk_expr(self, expr);
131+
}
132+
}

tests/ui/single_call_fn.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//@aux-build:proc_macros.rs
2+
#![allow(unused)]
3+
#![warn(clippy::single_call_fn)]
4+
#![no_main]
5+
6+
#[macro_use]
7+
extern crate proc_macros;
8+
9+
// Do not lint since it's public
10+
pub fn f() {}
11+
12+
fn g() {
13+
f();
14+
}
15+
16+
fn c() {
17+
println!("really");
18+
println!("long");
19+
println!("function...");
20+
}
21+
22+
fn d() {
23+
c();
24+
}
25+
26+
fn a() {}
27+
28+
fn b() {
29+
a();
30+
31+
external! {
32+
fn lol() {
33+
lol_inner();
34+
}
35+
fn lol_inner() {}
36+
}
37+
with_span! {
38+
span
39+
fn lol2() {
40+
lol2_inner();
41+
}
42+
fn lol2_inner() {}
43+
}
44+
}
45+
46+
fn e() {
47+
b();
48+
b();
49+
}

tests/ui/single_call_fn.stderr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: this function is only used once
2+
--> $DIR/single_call_fn.rs:26:1
3+
|
4+
LL | fn a() {}
5+
| ^^^^^^^^^
6+
|
7+
help: used here
8+
--> $DIR/single_call_fn.rs:29:5
9+
|
10+
LL | a();
11+
| ^
12+
= note: `-D clippy::single-call-fn` implied by `-D warnings`
13+
14+
error: this function is only used once
15+
--> $DIR/single_call_fn.rs:16:1
16+
|
17+
LL | / fn c() {
18+
LL | | println!("really");
19+
LL | | println!("long");
20+
LL | | println!("function...");
21+
LL | | }
22+
| |_^
23+
|
24+
help: used here
25+
--> $DIR/single_call_fn.rs:23:5
26+
|
27+
LL | c();
28+
| ^
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)