Skip to content
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

Merged
merged 1 commit into from
Jul 19, 2021

Conversation

th1000s
Copy link
Contributor

@th1000s th1000s commented May 13, 2021

changelog: [similar_names] lint no longer suggests to insert or add an underscore to "fix" too similar names

@rust-highfive
Copy link

r? @giraffate

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 13, 2021
Copy link
Contributor

@giraffate giraffate left a 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";

@giraffate giraffate added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 14, 2021
Copy link
Member

@flip1995 flip1995 left a 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?

Comment on lines 172 to 178
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be

Suggested change
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()

Copy link
Contributor Author

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().

@th1000s
Copy link
Contributor Author

th1000s commented May 14, 2021

In the case of books / book_ the name book is available: One trailing character is not counted, maybe to allow for plurals such as this one.

The mentioned issue #6479 notes that an underscore is nicer to read that r#type which may look a bit out of place. I also agree that an underscore is visually distinct enough and would even suggest to not count trailing underscores at all, keyword or not.

@flip1995
Copy link
Member

r#<keyword> exists in Rust, so that you can use keywords as idents. So

an underscore is nicer to read that r#type

But r#type is the Rust way to do it, so this isn't a valid argument for a change in Clippy. Also I disagree personally.

I also agree that an underscore is visually distinct enough and would even suggest to not count trailing underscores at all, keyword or not.

I'm not convinced that we should encourage adding an _ as the only distinguishing thing between variable names.


That being said, the suggestion with a trailing underscore is quite bad. I would just remove the help message if the distinguishing character is _.

@th1000s
Copy link
Contributor Author

th1000s commented May 31, 2021

suggest[ing] a trailing underscore is quite bad.

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 abc and a_c are distinct enough?

@camsteffen
Copy link
Contributor

The keyword thing doesn't make sense to me. The fact that type is a keyword doesn't make type_ and types any less similar.

I would just remove the help message if the distinguishing character is _.

...never count an underscore-character match, as abc and a_c are distinct enough?

These statements are saying the same thing. I think this makes sense. Then I would add another caveat that foo_ and foo__ should still lint.

@flip1995
Copy link
Member

flip1995 commented Jun 2, 2021

I would just remove the help message if the distinguishing character is _.

...never count an underscore-character match, as abc and a_c are distinct enough?

These statements are saying the same thing. I think this makes sense.

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.

@camsteffen
Copy link
Contributor

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.

@flip1995
Copy link
Member

flip1995 commented Jun 2, 2021

It's still pretty similar though. And this lint is a pedantic lint. So I wouldn't change it and let the lint be pedantic.

I would like to see a test if r#type vs types gets linted by this lint.

@giraffate
Copy link
Contributor

ping from triage @th1000s. Does it still need to be discussed?

@th1000s
Copy link
Contributor Author

th1000s commented Jun 23, 2021

Ok, then I'll just reword the current suggestion to "use a different name". Just adding a trailing _ "fixes" the lint but not the intent behind it.

@giraffate
Copy link
Contributor

ping from triage @th1000s. Can you have any update on this?

@th1000s th1000s changed the title Add keyword whitelist to similar_names lint similar_names: No longer suggest inserting or appending an underscore Jul 11, 2021
@th1000s
Copy link
Contributor Author

th1000s commented Jul 11, 2021

The help text now always reads:

help: use a more distinct name for either `bpple`, `apple`, or both

@camsteffen
Copy link
Contributor

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
@th1000s
Copy link
Contributor Author

th1000s commented Jul 13, 2021

Very well, done.

Copy link
Contributor

@giraffate giraffate left a 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.

@giraffate
Copy link
Contributor

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Jul 19, 2021

📌 Commit e8f57c3 has been approved by giraffate

@bors
Copy link
Collaborator

bors commented Jul 19, 2021

⌛ Testing commit e8f57c3 with merge 6103814...

@bors
Copy link
Collaborator

bors commented Jul 19, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing 6103814 to master...

@bors bors merged commit 6103814 into rust-lang:master Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants