-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
similar_names: No longer suggest inserting or appending an underscore #7221
Conversation
r? @giraffate (rust-highfive has picked a reviewer for you, use r? to override) |
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.
Why do only specific keywords fixed here? It looks like that it's not limited to specific keywords. For example, book__
is suggested in case below.
let books = ["a", "b", "c"];
let book_ = "a";
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.
In addition to @giraffate question, my question is why this is a fix for this lint? The lint documentation states:
It's hard to distinguish between names that differ only by a single character.
types
and type_
only differ by a single character. As do types
and type
. So the lint triggering here is correct. Since this lint is pedantic
and triggering here is correct by the lint definition, I wouldn't change this. I don't see a linked issue about this, was there discussions of this change anywhere?
If you want a keyword as a variable name, you should add r#
in front of the keyword instead of adding a _
. So types
and r#type
should be fine. Does this lint trigger on those?
let rust_keywords = [ | ||
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for", "if", "impl", | ||
"in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct", | ||
"super", "trait", "true", "type", "unsafe", "use", "where", "while", "async", "await", "dyn", "abstract", | ||
"become", "box", "do", "final", "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "union", | ||
]; | ||
rust_keywords.iter().any(|kw| *kw == maybe_keyword) |
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.
This could be
let rust_keywords = [ | |
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for", "if", "impl", | |
"in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct", | |
"super", "trait", "true", "type", "unsafe", "use", "where", "while", "async", "await", "dyn", "abstract", | |
"become", "box", "do", "final", "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "union", | |
]; | |
rust_keywords.iter().any(|kw| *kw == maybe_keyword) | |
Ident::with_dummy_span(Symbol::intern(maybe_keyword)).is_used_keyword() |
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.
Thanks, much better than hardcoding the list. To also catch yield
etc. I'd even use is_reserved()
.
In the case of The mentioned issue #6479 notes that an underscore is nicer to read that |
But
I'm not convinced that we should encourage adding an That being said, the suggestion with a trailing underscore is quite bad. I would just remove the help message if the distinguishing character is |
Indeed, that just technically fixes the lint. Will fix that in any case. What do @phansch and @camsteffen think? Should an exception be made when evading known keywords as the issue suggests? Maybe even go further and never count an underscore-character match, as |
The keyword thing doesn't make sense to me. The fact that
These statements are saying the same thing. I think this makes sense. Then I would add another caveat that |
Not really, I think? I'm saying, that we should remove (or improve) the help message telling the user to change the name to a double underscore. I'm not suggesting to stop linting in those cases. |
Oh I misunderstood. That makes sense too. I think we could stop linting those cases since one underscore vs no underscores is a more visually distinct than different letters IMO. |
It's still pretty similar though. And this lint is a I would like to see a test if |
ping from triage @th1000s. Does it still need to be discussed? |
Ok, then I'll just reword the current suggestion to "use a different name". Just adding a trailing |
ping from triage @th1000s. Can you have any update on this? |
similar_names
lint
The help text now always reads:
|
I don't find that very helpful. The two variables are already pointed out. How bout we just remove the help message? |
changelog: [`similar_names`] lint no longer suggests to insert or add an underscore to "fix" too similar names
Very well, done. |
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 it looks good.
@bors r+ Thanks! |
📌 Commit e8f57c3 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
changelog: [
similar_names
] lint no longer suggests to insert or add an underscore to "fix" too similar names