-
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
Add new lint: match with a single binding statement #5061
Conversation
380c325
to
f8a9b35
Compare
IMO suggesting let x = y;
{
// match body using `x`
} Is the best thing we could do here. Detecting if the let binding is necessary after rewriting the match expression is to hard to do. And only suggesting the match body breaks the code, when auto fixed |
You can also use 2 different spans for the lint and the suggestion with |
Thanks a lot ! let x = a;
{
println!("{}", x);
} I still need to fix |
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.
Suggestion is fine as it is. Adding indentation to the suggestion is a bit out off scope for Clippy currently. This has to be handled by rustfmt
afterwards.
I still need to fix let assignment based for this kind of match (
infallible_destructuring_match
lint colliding)
What you can do to fix is this is:
- Move
infallible_destructuring_match
into theMatches
lint pass, by copying thecheck_local
method incl. lint declaration, ... to thematches.rs
module. - Use
impl_lint_pass!
macro, instead ofdeclare_lint_pass
and define#[derive(Default)] pub struct Matches { infallible_destructuring_match_linted: bool // or a better name }
- When the
infallible_destructuring_match
lint triggers, setself.infallible_destructuring_match_linted = true
- Condition the
check_match_single_binding(cx, ex, arms, expr);
onself.infallible_destructuring_match_linted
:if self.infallible_destructuring_match_linted { self.infallible_destructuring_match_linted = false; } else { check_match_single_binding(cx, ex, arms, expr); }
de7752d
to
6f7e035
Compare
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.
With my suggestion below, you should be able to revert commit 6f7e035 without failing the dogfood test.
☔ The latest upstream changes (presumably #4945) made this pull request unmergeable. Please resolve the merge conflicts. |
b93c3f6
to
81c5cd6
Compare
Is there a way to make a clean distinction between:
and
I don't like very much my solution to add semi-colon to body expression if needed (first |
You could just check if the Let's go through the cases:
|
☔ The latest upstream changes (presumably #5098) made this pull request unmergeable. Please resolve the merge conflicts. |
- Lint name: MATCH_SINGLE_BINDING
81c5cd6
to
2aa4f3d
Compare
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! Can you squash some commits? After that, this should be ready to merge.
- moving infaillible lint to prevent collisions
52e7243
to
00904cb
Compare
Here you go ! Thanks a lot for hints and patience 😆 |
@bors r+ Thanks for the contribution! |
📌 Commit 00904cb has been approved by |
Add new lint: match with a single binding statement This lint catches `match` statements that binds to only one block and could be rewritten using a simple `let` statement. Lint name: MATCH_SINGLE_BINDING fixes: #3664 changelog: add lint for match with single binding statement
☀️ Test successful - checks-travis, status-appveyor |
This lint catches
match
statements that binds to only one block and could be rewritten using a simplelet
statement.Lint name: MATCH_SINGLE_BINDING
fixes: #3664
changelog: add lint for match with single binding statement