-
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
[WIP] Add getter prefix name lint #3616
Conversation
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
☔ The latest upstream changes (presumably #3603) made this pull request unmergeable. Please resolve the merge conflicts. |
The current WIP looks good, so I'll just answer your questions.
With 5+6 answered, this lint won't trigger on |
☔ The latest upstream changes (presumably #3600) made this pull request unmergeable. Please resolve the merge conflicts. |
ping from triage @0xazure. Any updates on this? |
Hey @flip1995 thanks for the ping, I lost track of this with the start of the new year. I should have more time to revisit this at the end of the week.
As for 1/5/6 that's interesting! I was just running through on the basis of the function names (
Maybe I've misunderstood, but aren't associated functions the ones in the
I probably haven't implemented it to look at Trait I'll need to verify if it lints Trait definitions, and add some more test cases to document that behaviour along with updating the doc comment. |
@oli-obk any opinions on that, as you created the original issue?
Yes!
Sure! With Trait impls I mean thinks like The author of the crate which includes the Trait has influence on the names of the function names. So the Trait definition ( TL;DR: Lint on |
/// } | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub GETTER_PREFIX, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lint naming guidelines state that we should be able to read #[allow(foo)]
sort of as a sentence, so "allow getter prefix" should probably be pluralized to "allow getter prefixes".
span_lint( | ||
cx, | ||
GETTER_PREFIX, | ||
implitem.span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think implitem.ident.span
should give you the span of just the name (which both leads to a better diagnostic message and should be helpful for a structured suggestion)
ping from triage @0xazure. Do you want to continue working on this? Are there any open questions I missed and that need to be answered for you to continue? |
ping from triage @0xazure. This PR was tale for quite some time now. Thanks for your contribution, feel free to reopen this PR whenever you want to continue working on this lint! |
Could someone please summarize what is missing from this to be mergable? 😃 As far as I can tell, the 2 review changes suggested by @oli-obk and making the lint auto-fixable. |
A quick looks seems to suggest that we also need:
and
|
Add a lint to detect getter methods prefixed with
get_
which do notfollow the Rust convention for getter names as described in the
naming section of the Rust API Guidelines.
The
get_
prefix is not used unless there is a single and obvious thingthat could be gotten by a getter, including:
get
get_mut
get_unchecked
get_unchecked_mut
get_ref
Closes #1673.
TODO:
The basic lint is implemented in this pull request, but there are still a couple things I'd like to add/finalize before I feel this is ready to merge. I wanted to open a pull request as soon as possible to collect and start incorporating feedback though, so this is currently a WIP.
get_
prefix from the method name (usingspan_lint_and_sugg
?)Questions:
Since this is my first contribution to Clippy, I would really appreciate your feedback! I found the
rustc
documentation to be a bit lacking, so if there are better ways to approach this lint I'm all ears.I also have a few specific items I would appreciate some guidance on:
LintPass::get_lints
and thustests/ui/lint_without_lint_pass.rs
(as well as a few otherrustc
methods likerustc_codegen_llvm::callee::get_fn
andsyntax::ext::tt::quoted::TokenTree::get_tt
, for example) will not pass this lint rule in its current form. I'm not sure of the next step with these methods; exceptions can be added to the allowed name list, or is this something that will need to be changed inrustc
first before this lint can land?target/debug/cargo-clippy clippy --all-targets --all-features -- -D clippy::all -D clippy::internal -D clippy::pedantic
against my local changes, I got the following error:clippy_lints/src/functions.rs
or another existing file?set_*
andget_*
methods that do nothing but access the field #1673 @flip1995 brought up an interesting point about linting just prefixes (e.g. justget_unchecked*
instead ofget_unchecked
andget_unchecked_mut
separately). Are there any opinions on how rigid we want the exceptions? As I commented on the issue, more permissive prefix matching could lead to some methods being missed simply because they start with one of the allowed names.impl
s, but what about functions not associated with a type? As far as I can tell they aren't covered by the API guidelines but could be added to this lint as well.self
to be considered a 'getter'?