-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Lint set_*
and get_*
methods that do nothing but access the field
#1673
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
Comments
Hey @oli-obk I'm interested in having a go at implementing this lint. Is it still up for grabs? |
Actually, I'm not sure if this really is valid rust style -- setters and getters may be added to future proof an API, for example. |
That's a really good point, I can definitely see that use case. That would also nix linting if it only has a Move to close this? |
The "If the type only has a |
If we're considering the case of future-proofing an API, if the method named Maybe I'm missing something about the use case though, @hcpl do you have an example? |
@0xazure yeah, for example, Not sure if this particular instance is idiomatic by any means, but it shows that this naming scheme has a precedent in the standard library. Also, |
Thanks for the example, @hcpl! I don't think it's idiomatic, but it looks like This proves the point of future-proofing though; |
Personally, I find the And I just found this in API guidelines: https://rust-lang-nursery.github.io/api-guidelines/naming.html#c-getter. Curiously, setters aren't mentioned anywhere in the guidelines (would be good to notify maintainers over there about this topic). |
I don't personally have any preference for That API Guidelines section is a great find!
Based on that section the It looks like we should lint to disallow |
Searching the std lib of rust for
I think we can add a |
|
Yes I don't think this should be included in the special cases, just listed it for completeness. |
122: rename getter functions r=reiner-dolp a=flip1995 See [API-guidelines](https://rust-lang-nursery.github.io/api-guidelines/naming.html?highlight=getter#getter-names-follow-rust-convention-c-getter) Soon this will be a Clippy lint: rust-lang/rust-clippy#1673 Co-authored-by: flip1995 <[email protected]>
To summarize, if the type has a
This should be enough to get me started on a style lint for this convention, I should have some time over the next couple days to start digging into this. |
Exactly! It shouldn't be required to explicitly filter |
I think that's an interesting question, should we lint for only the prefixes (e.g. |
Yeah this methods would pass the lint. But is this bad? |
Just wanted to post an update, I'm still working on this but have other demands on my time right now. I should get back to it in a week or so, hopefully have a PR up soon after that. |
Add a lint to detect getter methods prefixed with `get_` which do not follow the Rust convention for getter names as described in the [naming section][0] of the Rust API Guidelines. The `get_` prefix is not used unless there is a single and obvious thing that could be gotten by a getter, including: - `get` - `get_mut` - `get_unchecked` - `get_unchecked_mut` - `get_ref` Closes rust-lang#1673. [0]: https://rust-lang-nursery.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter
Would a lint that implements this rule still be accepted into clippy today? If so, I'm happy to pick this up. |
It is not idiomatic in Rust to have setters and getters. Make the field public instead.
If the type only has a
get_foo
method but not aset_foo
method, suggest naming itfoo
instead.The text was updated successfully, but these errors were encountered: