-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint [single_call_fn
]
#10951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
new lint [single_call_fn
]
#10951
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::{is_from_proc_macro, is_in_test_function}; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_hir::intravisit::{walk_expr, Visitor}; | ||
use rustc_hir::{intravisit::FnKind, Body, Expr, ExprKind, FnDecl}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::hir::nested_filter::OnlyBodies; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for functions that are only used once. Does not lint tests. | ||
/// | ||
/// ### Why is this bad? | ||
/// It's usually not, splitting a function into multiple parts often improves readability and in | ||
/// the case of generics, can prevent the compiler from duplicating the function dozens of | ||
/// time; instead, only duplicating a thunk. But this can prevent segmentation across a | ||
/// codebase, where many small functions are used only once. | ||
/// | ||
/// Note: If this lint is used, prepare to allow this a lot. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// pub fn a<T>(t: &T) | ||
/// where | ||
/// T: AsRef<str>, | ||
/// { | ||
/// a_inner(t.as_ref()) | ||
/// } | ||
/// | ||
/// fn a_inner(t: &str) { | ||
/// /* snip */ | ||
/// } | ||
/// | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// pub fn a<T>(t: &T) | ||
/// where | ||
/// T: AsRef<str>, | ||
/// { | ||
/// let t = t.as_ref(); | ||
/// /* snip */ | ||
/// } | ||
/// | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub SINGLE_CALL_FN, | ||
restriction, | ||
"checks for functions that are only used once" | ||
} | ||
impl_lint_pass!(SingleCallFn => [SINGLE_CALL_FN]); | ||
|
||
#[derive(Clone)] | ||
pub struct SingleCallFn { | ||
pub avoid_breaking_exported_api: bool, | ||
pub def_id_to_usage: FxHashMap<LocalDefId, (Span, Vec<Span>)>, | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for SingleCallFn { | ||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
kind: FnKind<'tcx>, | ||
_: &'tcx FnDecl<'_>, | ||
body: &'tcx Body<'_>, | ||
span: Span, | ||
def_id: LocalDefId, | ||
) { | ||
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) | ||
|| in_external_macro(cx.sess(), span) | ||
|| is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span)) | ||
|| is_in_test_function(cx.tcx, body.value.hir_id) | ||
{ | ||
return; | ||
} | ||
|
||
self.def_id_to_usage.insert(def_id, (span, vec![])); | ||
} | ||
|
||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) { | ||
let mut v = FnUsageVisitor { | ||
cx, | ||
def_id_to_usage: &mut self.def_id_to_usage, | ||
}; | ||
cx.tcx.hir().visit_all_item_likes_in_crate(&mut v); | ||
|
||
for usage in self.def_id_to_usage.values() { | ||
let single_call_fn_span = usage.0; | ||
if let [caller_span] = *usage.1 { | ||
span_lint_and_help( | ||
cx, | ||
SINGLE_CALL_FN, | ||
single_call_fn_span, | ||
"this function is only used once", | ||
Some(caller_span), | ||
"used here", | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct FnUsageVisitor<'a, 'tcx> { | ||
cx: &'a LateContext<'tcx>, | ||
def_id_to_usage: &'a mut FxHashMap<LocalDefId, (Span, Vec<Span>)>, | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for FnUsageVisitor<'a, 'tcx> { | ||
type NestedFilter = OnlyBodies; | ||
|
||
fn nested_visit_map(&mut self) -> Self::Map { | ||
self.cx.tcx.hir() | ||
} | ||
|
||
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) { | ||
let Self { cx, .. } = *self; | ||
|
||
if let ExprKind::Path(qpath) = expr.kind | ||
&& let res = cx.qpath_res(&qpath, expr.hir_id) | ||
&& let Some(call_def_id) = res.opt_def_id() | ||
&& let Some(def_id) = call_def_id.as_local() | ||
&& let Some(usage) = self.def_id_to_usage.get_mut(&def_id) | ||
{ | ||
usage.1.push(expr.span); | ||
} | ||
|
||
walk_expr(self, expr); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//@aux-build:proc_macros.rs | ||
//@compile-flags: --test | ||
#![allow(clippy::redundant_closure_call, unused)] | ||
#![warn(clippy::single_call_fn)] | ||
#![no_main] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
// Do not lint since it's public | ||
pub fn f() {} | ||
|
||
fn i() {} | ||
fn j() {} | ||
|
||
fn h() { | ||
// Linted | ||
let a = i; | ||
// Do not lint closures | ||
let a = (|| { | ||
// Not linted | ||
a(); | ||
// Imo, it's reasonable to lint this as the function is still only being used once. Just in | ||
// a closure. | ||
j(); | ||
}); | ||
a(); | ||
} | ||
|
||
fn g() { | ||
f(); | ||
} | ||
|
||
fn c() { | ||
println!("really"); | ||
println!("long"); | ||
println!("function..."); | ||
} | ||
|
||
fn d() { | ||
c(); | ||
} | ||
|
||
fn a() {} | ||
|
||
fn b() { | ||
a(); | ||
|
||
external! { | ||
fn lol() { | ||
lol_inner(); | ||
} | ||
fn lol_inner() {} | ||
} | ||
with_span! { | ||
span | ||
fn lol2() { | ||
lol2_inner(); | ||
} | ||
fn lol2_inner() {} | ||
} | ||
} | ||
|
||
fn e() { | ||
b(); | ||
b(); | ||
} | ||
|
||
#[test] | ||
fn k() {} | ||
|
||
#[test] | ||
fn l() { | ||
k(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
error: this function is only used once | ||
--> $DIR/single_call_fn.rs:34:1 | ||
| | ||
LL | / fn c() { | ||
LL | | println!("really"); | ||
LL | | println!("long"); | ||
LL | | println!("function..."); | ||
LL | | } | ||
| |_^ | ||
| | ||
help: used here | ||
--> $DIR/single_call_fn.rs:41:5 | ||
| | ||
LL | c(); | ||
| ^ | ||
= note: `-D clippy::single-call-fn` implied by `-D warnings` | ||
|
||
error: this function is only used once | ||
--> $DIR/single_call_fn.rs:13:1 | ||
| | ||
LL | fn i() {} | ||
| ^^^^^^^^^ | ||
| | ||
help: used here | ||
--> $DIR/single_call_fn.rs:18:13 | ||
| | ||
LL | let a = i; | ||
| ^ | ||
|
||
error: this function is only used once | ||
--> $DIR/single_call_fn.rs:44:1 | ||
| | ||
LL | fn a() {} | ||
| ^^^^^^^^^ | ||
| | ||
help: used here | ||
--> $DIR/single_call_fn.rs:47:5 | ||
| | ||
LL | a(); | ||
| ^ | ||
|
||
error: this function is only used once | ||
--> $DIR/single_call_fn.rs:14:1 | ||
| | ||
LL | fn j() {} | ||
| ^^^^^^^^^ | ||
| | ||
help: used here | ||
--> $DIR/single_call_fn.rs:25:9 | ||
| | ||
LL | j(); | ||
| ^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.