New lint: disallowed_trait_usage#16810
Conversation
|
r? @dswij rustbot has assigned @dswij. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
r? clippy |
|
I'm unclear on the value of this lint, though I don't mind people being able to choose to restrict how they write their code. The reason I comment at all is because this is approximate to an extension of an already-existing lint, I'd like to ask if the |
|
My main motivation is to be able to outlaw uses of |
New lint:
disallowed_trait_usagedisallowed-trait-usage#15765disallowed_methodson trait impls #8581This adds a new configurable lint [
disallowed_trait_usage] that lets users forbid using a type via a specific trait interface.Motivation
A common mistake is using
Debugformatting ({:?}) whereDisplayformatting ({}) would be more appropriate, especially for user-facing output like error messages, logs, and GUIs. For example:This applies broadly — paths, errors, and many other types have
Debugoutput that is noisy and not meant for end users. There's no way to systematically catch these with existing lints. Theunnecessary_debug_formattinglint only coversOsStrandPath, and can't be extended to arbitrary types or project-specific conventions.Configuration
Each entry requires a
traitfield (the trait being disallowed) and either:type: a concrete type path, orimplements: a trait path — matches any type implementing that traitWhat it detects
println!("{:?}", my_path),format!("{path:?}"),write!(buf, "{:?}", x)my_struct.trait_method()Works with:
std::path::PathBuf+std::fmt::Debug)i32,bool,str, etc.)Debug,Display,Binary,Octal,LowerHex,UpperHex,LowerExp,UpperExp,Pointer)implements(any type implementing the given trait)Path validation
Invalid paths produce clear warnings, following the same pattern as
disallowed_types/disallowed_methods:changelog: [
disallowed_trait_usage]: new lint that forbids using a configured type (or implementors of a trait) via a configured trait interfaceImplemented with a lot of help from Claude Code