Email me

Clippy lints / Style

Clippy lint · Style · checked with Clippy 0.1.99

clippy::disallowed_methods: Denies the configured methods and functions in clippy.toml Note: Even though this lint is warn-by-default, it will only trigger if methods are defined in the clippy.toml file

What it meansDenies the configured methods and functions in clippy.toml Note: Even though this lint is warn-by-default, it will only trigger if methods are defined in the clippy.toml file. It is in the style group and warns by default.

Does it matter?

Some methods are undesirable in certain contexts, and it’s beneficial to lint for them as needed.

Code that works but is not idiomatic Rust. Warns by default.

How to fix it

Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.

Flagged

let xs = vec![1, 2, 3, 4];
xs.leak(); // Vec::leak is disallowed in the config.
// The diagnostic contains the message "no leaking memory".

let _now = Instant::now(); // Instant::now is disallowed in the config.

let _box = Box::new(3); // Box::new is disallowed in the config.

Write this instead

let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
xs.push(123); // Vec::push is _not_ disallowed in the config.

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::disallowed_methods)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
disallowed_methods = "allow"

One run

cargo clippy -- -A clippy::disallowed-methods

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.

Stylegroup
warndefault level
yesfixed by cargo clippy --fix
1.49.0added in Rust

Clippy's own documentation

What it does

Denies the configured methods and functions in clippy.toml

Note: Even though this lint is warn-by-default, it will only trigger if methods are defined in the clippy.toml file.

Why is this bad?

Some methods are undesirable in certain contexts, and it’s beneficial to lint for them as needed.

Example

An example clippy.toml configuration:

disallowed-methods = [
    # Can use a string as the path of the disallowed method.
    "std::boxed::Box::new",
    # Can also use an inline table with a `path` key.
    { path = "std::time::Instant::now" },
    # When using an inline table, can add a `reason` for why the method
    # is disallowed.
    { path = "std::vec::Vec::leak", reason = "no leaking memory" },
    # Can also add a `replacement` that will be offered as a suggestion.
    { path = "std::sync::Mutex::new", reason = "prefer faster & simpler non-poisonable mutex", replacement = "parking_lot::Mutex::new" },
    # This would normally error if the path is incorrect, but with `allow-invalid` = `true`,
    # it will be silently ignored
    { path = "std::fs::InvalidPath", reason = "use alternative instead", allow-invalid = true },
]
let xs = vec![1, 2, 3, 4];
xs.leak(); // Vec::leak is disallowed in the config.
// The diagnostic contains the message "no leaking memory".

let _now = Instant::now(); // Instant::now is disallowed in the config.

let _box = Box::new(3); // Box::new is disallowed in the config.

Use instead:

let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
xs.push(123); // Vec::push is _not_ disallowed in the config.

Past names

  • disallowed_method

Configuration

  • disallowed-methods: The list of disallowed methods, written as fully qualified paths.

Fields:

  • path (required): the fully qualified path to the method that should be disallowed

  • reason (optional): explanation why this method is disallowed

  • replacement (optional): suggested alternative method

  • allow-invalid (optional, false by default): when set to true, it will ignore this entry if the path doesn’t exist, instead of emitting an error

    (default: [])

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::needless_returnunneeded `return` statementRead 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