rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.44k stars 1.54k forks source link

Lint on method/struct fields sharing the same name #1481

Open gnzlbg opened 7 years ago

gnzlbg commented 7 years ago

Would it be possible to lint on the situation in which a struct field shares a name with a struct method? For example, in the following situation:

struct Foo {
  bar: i32 
}

impl Foo {
  fn bar(self);
}
oli-obk commented 7 years ago

This lint could have 2 versions. One that lints when the field is public (warn by default) and there's a method that could cause a collisions, and one that lints even if the field is not public (allow by default, since users of your type won't see it).

TedDriggs commented 7 years ago

Isn't this the preferred way of exposing public accessors for private fields?

gnzlbg commented 7 years ago

I thought that the preferred way of exposing public accessors for private fields was just to make the fields public. In particular, if you want to expose read/write access to the private fields, you need two accessors anyways right? So they cannot both have the same name.

oli-obk commented 7 years ago

Isn't this the preferred way of exposing public accessors for private fields?

That's why I suggested two lints. If both the method and the type are public, you get a collision, otherwise not.

ssokolow commented 7 years ago

@gnzlbg The way I interpreted @TedDriggs was something similar to Qt's approach:

That said, as someone who programs mostly in Python, my solution there would just be to make the internal field name _inputMask.

UPDATE: ...though prefixing the property with an underscore would probably make it more difficult to auto-generate a nice table of properties in the style of Qt's docs.

gnzlbg commented 7 years ago

@ssokolow yeah I think i got it now, @oli-obk proposal should allow that.