-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint against declaring fn to_string(&self) -> String
#4247
Comments
I think such a lint would make sense. I'd love to give it a try implementing it as my first real contribution to this project if nobody has any objections. Looking into the various APIs this should be doable in a manageable amount of code by taking advantage of the functions defined in The only thing I'm not quite sure about is turning up the report level from warn to error. Correct me if I'm wrong, but as far as I have understood the report level of a lint is indirectly defined by its lint category (correctness, style, ...). The actual level is not defined by the lint itself, but by the macro used to register the lints (see So from my point of view, this would require two separate, but quite similar lints:
From an implementation perspective this wouldn't be a real problem, of course, just requires a bit more boilerplate to register the lints. Or is there some nice functionality I've missed that allows for using different report levels at runtime? |
@Darth-Revan Yes basically what you described. For instance both rust-clippy/clippy_lints/src/methods/mod.rs Lines 2340 to 2362 in be3d6cf
|
@kennytm Thank you very much. That's exactly what I was looking for. I'll have a look into that and try to adapt it for the new lint. |
Implement lint for inherent to_string() method. Fixes #4247 changelog: Implement two new lints: `inherent_to_string` and `inherent_to_string_shadow_display` 1) Emits a warning if a type implements an inherent method `to_string(&self) -> String` 2) Emits an error if a type implements an inherent method `to_string(&self) -> String` and also implements the `Display` trait
Emit a lint when a type defines an inherent
to_string()
method. One should implementDisplay
instead.If a type both implements
Display
and defines an inherentto_string()
method, turn up the warn level to error, since the inherentto_string()
will shadow theDisplay
impl and they may behave differently.Do not emit the lint if
to_string()
is a trait method or free function.Do not emit the lint if the method is not exactly
fn(&self) -> String
, or maybe still emit a lint but suggest renaming the method.The text was updated successfully, but these errors were encountered: