Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::disallowed_fields: Denies the configured fields in clippy.toml Note: Even though this lint is warn-by-default, it will only trigger if fields are defined in the clippy.toml file
What it meansDenies the configured fields in clippy.toml Note: Even though this lint is warn-by-default, it will only trigger if fields are defined in the clippy.toml file. It is in the style group and warns by default.
Does it matter?
Some fields 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 does not fix this one automatically. Change the code by hand:
Flagged
use std::ops::Range;
let range = Range { start: 0, end: 1 };
println!("{}", range.start); // `start` is disallowed in the config.
Write this instead
use std::ops::Range;
let range = Range { start: 0, end: 1 };
println!("{}", range.end); // `end` 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_fields)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
disallowed_fields = "allow"One run
cargo clippy -- -A clippy::disallowed-fieldsThe [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 fields in clippy.toml
Note: Even though this lint is warn-by-default, it will only trigger if fields are defined in the clippy.toml file.
Why is this bad?
Some fields are undesirable in certain contexts, and it’s beneficial to lint for them as needed.
Example
An example clippy.toml configuration:
disallowed-fields = [
# Can use a string as the path of the disallowed field.
"std::ops::Range::start",
# Can also use an inline table with a `path` key.
{ path = "std::ops::Range::start" },
# When using an inline table, can add a `reason` for why the field
# is disallowed.
{ path = "std::ops::Range::start", reason = "The start of the range is not used" },
]
use std::ops::Range;
let range = Range { start: 0, end: 1 };
println!("{}", range.start); // `start` is disallowed in the config.
Use instead:
use std::ops::Range;
let range = Range { start: 0, end: 1 };
println!("{}", range.end); // `end` is _not_ disallowed in the config.
Configuration
disallowed-fields: The list of disallowed fields, written as fully qualified paths.
Fields:
-
path(required): the fully qualified path to the field that should be disallowed -
reason(optional): explanation why this field is disallowed -
replacement(optional): suggested alternative method -
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
- collapsible_matchthis `match` can be collapsed into the outer `match`
- comparison_to_emptyChecks for comparing to an empty slice such as "" or [], and suggests using .is_empty() where applicable
- default_instead_of_iter_empty`std::iter::empty()` is the more idiomatic way
- 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`
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?