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

Add new lint [needless_parens_on_range_literals] #8933

Merged
merged 12 commits into from
Jun 6, 2022
Merged

Add new lint [needless_parens_on_range_literals] #8933

merged 12 commits into from
Jun 6, 2022

Conversation

DennisOSRM
Copy link
Contributor

@DennisOSRM DennisOSRM commented Jun 3, 2022

changelog: Adds a new lint [needless_parens_on_range_literals] to warn on needless braces on literals in a range statement

For example, the lint would catch

error: needless parenthesis on range literals can be removed
  --> $DIR/needless_parens_on_range_literals.rs:8:13
   |
LL |     let _ = ('a')..=('z');
   |             ^^^^^ help: try: `'a'`
   |
   = note: `-D clippy::needless-parens-on-range-literals` implied by `-D warnings`

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dswij (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 3, 2022
@ghost
Copy link

ghost commented Jun 3, 2022

Not a review. Just some comments.

...

AFAIK, Rust uses the term parenthesis for (...) and braces for {...} so the name might be wrong.

Here is an example where the suggestion fails because the f64 has a trailing ..

fn main() {
    use rand::{thread_rng, Rng};
    
    let mut rng = thread_rng();
    
    let m: f64 = rng.gen_range((1.)..2.);
    println!("{}", m);
}

How does the lint handle macros? E.g. if the parens and literal are in a macro, does it recommend replacing the macro call with a literal? Maybe add some tests for that.

Copy link
Contributor

@Serial-ATA Serial-ATA left a comment

Choose a reason for hiding this comment

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

Welcome 🙂!

I'm not the reviewer, but I have some comments on top of @mikerite's.

clippy_lints/src/needless_braces_on_range_literal.rs Outdated Show resolved Hide resolved
clippy_lints/src/needless_braces_on_range_literal.rs Outdated Show resolved Hide resolved
/// ### Why is this bad?
/// Having superflous braces makes the code less legible as the impose an
/// overhead when reading.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like you took the examples out of here and put them at the top of the file. They need to be within this macro for the docs to appear on the lint list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented in 8185ecd


impl EarlyLintPass for NeedlessBracesOnRangeLiteral {
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
if let ExprKind::Range(Some(start), Some(end), _) = &e.kind {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about the following cases:

let _ = ('a')..;
let _ = ..('z');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented in 496155d

Comment on lines 52 to 54
if let Some(suggestion) = snippet_opt(cx, literal.span) {
diag.span_suggestion(e.span, "try", suggestion, Applicability::MachineApplicable);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is better expressed with snippet_with_applicability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented in afe37e1

@DennisOSRM
Copy link
Contributor Author

Not a review. Just some comments.

...

AFAIK, Rust uses the term parenthesis for (...) and braces for {...} so the name might be wrong.

Here is an example where the suggestion fails because the f64 has a trailing ..

fn main() {
    use rand::{thread_rng, Rng};
    
    let mut rng = thread_rng();
    
    let m: f64 = rng.gen_range((1.)..2.);
    println!("{}", m);
}

How does the lint handle macros? E.g. if the parens and literal are in a macro, does it recommend replacing the macro call with a literal? Maybe add some tests for that.

That's a great suggestion. Thanks, will add it

@DennisOSRM
Copy link
Contributor Author

Rewrote the check by impl LateLintPass. This gives access to type information and makes it easy to ignore parenthesis on a floating point start literal. The detection of parenthesis is done by inspecting the span of the corresponding expression and whether its length lines up with the length of the literal expression.

Copy link
Member

@dswij dswij left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, and welcome!

  1. The lint itself looks good, just some nits on spellings.
  2. Can you do a rebase instead of merge commits? We follow a no-merge policy
  3. We prefer plural form when naming lint. So the lint name would be needless_parens_on_range_literals. Can you also add the name to the changelog?

Also, huge thanks to @mikerite and @Serial-ATA for the suggestions and reviews!

clippy_lints/src/needless_parens_on_range_literal.rs Outdated Show resolved Hide resolved
clippy_lints/src/needless_parens_on_range_literal.rs Outdated Show resolved Hide resolved
clippy_lints/src/needless_parens_on_range_literal.rs Outdated Show resolved Hide resolved
@DennisOSRM
Copy link
Contributor Author

DennisOSRM commented Jun 5, 2022

Thanks for the PR, and welcome!

  1. The lint itself looks good, just some nits on spellings.
  2. Can you do a rebase instead of merge commits? We follow a no-merge policy
  3. We prefer plural form when naming lint. So the lint name would be needless_parens_on_range_literals. Can you also add the name to the changelog?

Also, huge thanks to @mikerite and @Serial-ATA for the suggestions and reviews!

Thanks a lot. Did the following changes:

  • Rebase. The merge commits are now gone.
  • Followed your suggestion to use the plural form.
  • CHANGELOG.md is updated.

@dswij dswij changed the title Add new lint [needless_braces_on_range_literal] Add new lint [needless_parens_on_range_literals] Jun 6, 2022
@dswij dswij changed the title Add new lint [needless_parens_on_range_literals] Add new lint [needless_parens_on_range_literals] Jun 6, 2022
@dswij
Copy link
Member

dswij commented Jun 6, 2022

Thanks for this! The lint looks great to me.

@bors r+

@bors
Copy link
Contributor

bors commented Jun 6, 2022

📌 Commit 61d7dd2 has been approved by dswij

@bors
Copy link
Contributor

bors commented Jun 6, 2022

⌛ Testing commit 61d7dd2 with merge 0f6e50f...

@bors
Copy link
Contributor

bors commented Jun 6, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: dswij
Pushing 0f6e50f to master...

@bors bors merged commit 0f6e50f into rust-lang:master Jun 6, 2022
@DennisOSRM DennisOSRM deleted the needless_braces_range_literal branch June 6, 2022 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants