Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::single_call_fn: this function is only used once
What it meansChecks for functions that are only used once. Does not lint tests. It is in the restriction group and off by default.
What you see
warning: this function is only used once --> main.rs:10:1 | 10 | / fn a_inner(t: &str) { 11 | | /* snip */ 12 | | } | |_^ | note: used here --> main.rs:7:5 | 7 | a_inner(t.as_ref()) | ^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#single_call_fn = note: requested on the command line with `-W clippy::single-call-fn`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
If a function is only used once (perhaps because it used to be used more widely), then the code could be simplified by moving that function’s code into its caller. However, there are reasons not to do this everywhere: Splitting a large function into multiple parts often improves readability by giving names to its parts. A function’s signature might serve a necessary purpose, such as constraining the type of a closure passed to it. Generic functions might call non-generic functions to reduce duplication in the produced machine code. If this lint is used, prepare to #[allow] it a lot.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
pub fn a<T>(t: &T)
where
T: AsRef<str>,
{
a_inner(t.as_ref())
}
fn a_inner(t: &str) {
/* snip */
}
Write this instead
pub fn a<T>(t: &T)
where
T: AsRef<str>,
{
let t = t.as_ref();
/* snip */
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
single_call_fn = "warn"In code
#![warn(clippy::single_call_fn)]One run
cargo clippy -- -W clippy::single-call-fnThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
avoid-breaking-exported-api: Suppress lints whenever the suggested change would cause breakage for other crates.
Clippy's own documentation
What it does
Checks for functions that are only used once. Does not lint tests.
Why restrict this?
If a function is only used once (perhaps because it used to be used more widely), then the code could be simplified by moving that function’s code into its caller.
However, there are reasons not to do this everywhere:
- Splitting a large function into multiple parts often improves readability by giving names to its parts.
- A function’s signature might serve a necessary purpose, such as constraining the type of a closure passed to it.
- Generic functions might call non-generic functions to reduce duplication in the produced machine code.
If this lint is used, prepare to #[allow] it a lot.
Example
pub fn a<T>(t: &T)
where
T: AsRef<str>,
{
a_inner(t.as_ref())
}
fn a_inner(t: &str) {
/* snip */
}
Use instead:
pub fn a<T>(t: &T)
where
T: AsRef<str>,
{
let t = t.as_ref();
/* snip */
}
Configuration
-
avoid-breaking-exported-api: Suppress lints whenever the suggested change would cause breakage for other crates.(default:
true)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- shadow_reuse`x` is shadowed
- shadow_sameChecks for bindings that shadow other bindings already in scope, while just changing reference level or mutability
- shadow_unrelatedChecks for bindings that shadow other bindings already in scope, either without an initialization or with one that does not even use the original value
- single_char_lifetime_namessingle-character lifetime names are likely uninformative
- std_instead_of_allocused import from `std` instead of `alloc`
- std_instead_of_coreused import from `std` instead of `core`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?