Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::disallowed_types: Denies the configured types in clippy.toml. Note: Even though this lint is warn-by-default, it will only trigger if types are defined in the clippy.toml file
What it meansDenies the configured types in clippy.toml. Note: Even though this lint is warn-by-default, it will only trigger if types are defined in the clippy.toml file. It is in the style group and warns by default.
Does it matter?
Some types are undesirable in certain contexts.
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
use std::collections::BTreeMap;
// or its use
let x = std::collections::BTreeMap::new();
Write this instead
// A similar type that is allowed by the config
use std::collections::HashMap;
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_types)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
disallowed_types = "allow"One run
cargo clippy -- -A clippy::disallowed-typesThe [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.
Clippy's own documentation
What it does
Denies the configured types in clippy.toml.
Note: Even though this lint is warn-by-default, it will only trigger if types are defined in the clippy.toml file.
Why is this bad?
Some types are undesirable in certain contexts.
Example:
An example clippy.toml configuration:
disallowed-types = [
# Can use a string as the path of the disallowed type.
"std::collections::BTreeMap",
# Can also use an inline table with a `path` key.
{ path = "std::net::TcpListener" },
# When using an inline table, can add a `reason` for why the type
# is disallowed.
{ path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" },
# Can also add a `replacement` that will be offered as a suggestion.
{ path = "std::sync::Mutex", reason = "prefer faster & simpler non-poisonable mutex", replacement = "parking_lot::Mutex" },
# This would normally error if the path is incorrect, but with `allow-invalid` = `true`,
# it will be silently ignored
{ path = "std::invalid::Type", reason = "use alternative instead", allow-invalid = true }
]
use std::collections::BTreeMap;
// or its use
let x = std::collections::BTreeMap::new();
Use instead:
// A similar type that is allowed by the config
use std::collections::HashMap;
Past names
- disallowed_type
Configuration
disallowed-types: The list of disallowed types, written as fully qualified paths.
Fields:
-
path(required): the fully qualified path to the type that should be disallowed -
reason(optional): explanation why this type is disallowed -
replacement(optional): suggested alternative type -
allow-invalid(optional,falseby default): when set totrue, 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
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- disallowed_macrosDenies the configured macros in clippy.toml Note: Even though this lint is warn-by-default, it will only trigger if macros are defined in the clippy.toml file
- disallowed_methodsDenies 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
- disallowed_namesuse of a disallowed/placeholder name `foo`
- doc_lazy_continuationdoc quote line without `>` marker
- doc_overindented_list_itemsDetects overindented list items in doc comments where the continuation lines are indented more than necessary
- double_must_usethis function has a `#[must_use]` attribute with no message, but returns a type already considered as `#[must_use]`
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?