Email me

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-fn

The [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.
Restrictiongroup
allowdefault level
nofixed by cargo clippy --fix
1.72.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ