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

Fix match_same_arms to fail late #4102

Merged
merged 4 commits into from
May 27, 2019

Conversation

Urriel
Copy link

@Urriel Urriel commented May 16, 2019

Changes:

  • Add a function search_same_list which return a list of matched expressions
  • Change the match_same_arms implementation behavior. It will lint each same arms found.

fixes #4096

changelog: none

Changes:
- Add a function search_same_list which return a list of matched expressions
- Change the match_same_arms implementation behaviour. It will lint each same arms found.
@Urriel
Copy link
Author

Urriel commented May 16, 2019

Hello, I would like some feedback, especially for the tests. I have some tests failing and did not figure out yet how to fix them.

@flip1995
Copy link
Member

Hey, thanks for the contribution!

Have you looked at https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md#testing? It is described there how to update reference files (*.stderr files) and how to add tests.
If you have and tests are still failing, please push the tests+stderr files, so I can take a look at them.

Changes:
- Add a test to match multiple arms in the same match statement
@Urriel
Copy link
Author

Urriel commented May 16, 2019

Thank you for the tips I did not pay attention to the doc folder. You guys did a good job making contributing trivial.

My last commit should add the right test and fix the test errors.

@flip1995
Copy link
Member

flip1995 commented May 16, 2019

You guys did a good job making contributing trivial.

Happy to hear that!

With your changes the first error doesn't get printed anymore. That's a regression we want to avoid. Could you look into that?

Copy link
Contributor

@pJunger pJunger left a comment

Choose a reason for hiding this comment

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

Is the implementation for search_same still needed?
Or maybe more important: Shouldn't the if check also fail late?

clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
clippy_lints/src/copies.rs Show resolved Hide resolved
@Urriel
Copy link
Author

Urriel commented May 20, 2019

I have questions about the "unwrap" line 387. I am not sure its the right way to do it.
Also at the moment, we can see the expression found twice.

clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
@Urriel
Copy link
Author

Urriel commented May 20, 2019

@pJunger Thank you for your insight it helps a lot.

@pJunger
Copy link
Contributor

pJunger commented May 20, 2019

You're welcome!

Copy link
Contributor

@pJunger pJunger left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@Urriel Urriel marked this pull request as ready for review May 20, 2019 09:42
@Urriel
Copy link
Author

Urriel commented May 20, 2019

Great !
Hope this little PR helps the cause.

@flip1995
Copy link
Member

flip1995 commented May 20, 2019

Thanks for the review @pJunger !

Now that search_same is removed you could rename the search_same_list function to search_same, since it does the same as before, just better. Thoughts?

Also (not really related to the issue, but while you're at it... 😄), could you change these span_notes:

db.span_note(
i.body.span,
&format!(
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
lhs
),
);
} else {
db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));

to span_help and use the span of the pats instead of the body. The lint message should then look something like this:

 ...
+help: consider refactoring into `41 | 52`
-note: consider refactoring into `41 | 52`
   --> $DIR/match_same_arms.rs:115:15
    |
 LL |         41 => 2,
+   |         ^^
-   |               ^

And last but not least: Could you add a test where more than 2 match arms are the same:

match x {
    1 => 2,
    2 => 2,
    3 => 2, // 3rd time the same arm
    4 => 3,
}

@flip1995
Copy link
Member

Thanks! You have to run tests/ui/update-all-references.sh again, since tests/ui/matches.rs fails now.

@pJunger
Copy link
Contributor

pJunger commented May 20, 2019

Maybe also add a function to the if_same_then_else block with multiple branches?

if true {
    let foo = "";
    return Ok(&foo[0..]);
} else if false {
    let foo = "bar";
    return Ok(&foo[0..]);
} else if true {
    let foo = "";
    return Ok(&foo[0..]);
} else {
    let foo = "";
    return Ok(&foo[0..]);
}

@Urriel
Copy link
Author

Urriel commented May 20, 2019

I hope this fixes everything. I forgot to do a full uitest before commiting.

@flip1995
Copy link
Member

LGTM now.

2 Things left to do:

  • run cargo fmt --all
  • squash some of the commits

and this is good to go.

@Urriel
Copy link
Author

Urriel commented May 21, 2019

I try to install rustfmt, but I get this errors:

'cargo-fmt' is not installed for the toolchain 'master'
toolchain 'master' does not support components

@flip1995
Copy link
Member

Yeah, you can't really install components on the master toolchain.

For Clippy we use nightly rustfmt anyway (for this reason). So install rustfmt for nightly and then run cargo +nightly fmt --all

Vincent Dal Maso added 2 commits May 21, 2019 13:08
Changes:
- Refactor the common case search into a function.
- Fix the useless Option around the vec in the search_same_list.
Changes:
- Fix stderr breaking the tests
- Adding tests over the if arms
@Urriel
Copy link
Author

Urriel commented May 21, 2019

That should cover it.

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.

Thanks!

Every PR is currently blocked on #4121, but I'll merge this once this bug is fixed.

@flip1995 flip1995 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 21, 2019
@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented May 27, 2019

📌 Commit 40f3665 has been approved by flip1995

flip1995 added a commit to flip1995/rust-clippy that referenced this pull request May 27, 2019
…lip1995

Fix match_same_arms to fail late

Changes:
- Add a function search_same_list which return a list of matched expressions
- Change the match_same_arms implementation behavior. It will lint each same arms found.

fixes rust-lang#4096

changelog: none
bors added a commit that referenced this pull request May 27, 2019
Rollup of 2 pull requests

Successful merges:

 - #4102 (Fix match_same_arms to fail late)
 - #4119 (Improve non ascii literal)

Failed merges:

r? @ghost
@bors bors merged commit 40f3665 into rust-lang:master May 27, 2019
@bors
Copy link
Collaborator

bors commented May 27, 2019

⌛ Testing commit 40f3665 with merge eb0a288...

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.

Make match_same_arms lint fail late
4 participants