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

Check expr usage for manual_flatten #7566

Merged
merged 2 commits into from
Aug 16, 2021
Merged

Conversation

dswij
Copy link
Member

@dswij dswij commented Aug 14, 2021

Fixes #6784
Fixes #7538

manual_flatten should not trigger when if let match expression will be used.

changelog: [manual_flatten] checks for expr usage after if let

@rust-highfive
Copy link

r? @flip1995

(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 Aug 14, 2021
@@ -47,6 +48,9 @@ pub(super) fn check<'tcx>(
let some_ctor = is_lang_ctor(cx, qpath, OptionSome);
let ok_ctor = is_lang_ctor(cx, qpath, ResultOk);
if some_ctor || ok_ctor;
// Ensure epxr in `if let` is not used afterwards
let mut used_visitor = LocalUsedVisitor::new(cx, pat_hir_id);
if !match_arms.iter().any(|arm| used_visitor.check_arm(arm));
Copy link
Member

@xFrednet xFrednet Aug 15, 2021

Choose a reason for hiding this comment

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

Small NIT: The usage of an iterator can be avoided here by deconstructing the slice in line 40ff like this:

if let ExprKind::Match(
    match_expr, [true_arm, _else_arm], MatchSource::IfLetDesugar{ contains_else_clause: false }
) = inner_expr.kind;

Then it should be possible to directly call if !LocalUsedVisitor::new(cx, pat_hir_id).check_arm(true_arm); and the direct index call in line 47 can also be avoided.


The rest looks good to me. I'm happy to merge this afterwards 🙃

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah that was a nice one. I should re-emphasize Rust's powerful pattern matching in my bible :)

Copy link
Member

Choose a reason for hiding this comment

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

Looking at this, it's actually surprising that we don't have a lint for this. I'll create am issue, even if this might ignore macros as most lints do 🙃

Copy link
Member

Choose a reason for hiding this comment

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

Created #7569 and also liked this PR as an example how this improves code quality. I also suggested to lint inside if_chain! macros to catch these cases inside Clippy. Thank you again for the contributing and inspiration 🙃

@xFrednet
Copy link
Member

r? @xFrednet

@rust-highfive rust-highfive assigned xFrednet and unassigned flip1995 Aug 15, 2021
@dswij dswij changed the title Check expr usage for manual_flaten Check expr usage for manual_flatten Aug 16, 2021
Add a scenario where `manual_flatten` is triggered when match expression will still be used after the match in `if let`.
`manual_flatten` should not trigger when match expression in `if let` is
going to be used.
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Awesome work and bonus points for having a Rust bible :P

@xFrednet
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 16, 2021

📌 Commit a09bc1b has been approved by xFrednet

@bors
Copy link
Contributor

bors commented Aug 16, 2021

⌛ Testing commit a09bc1b with merge 983e5b8...

@bors
Copy link
Contributor

bors commented Aug 16, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 983e5b8 to master...

@bors bors merged commit 983e5b8 into rust-lang:master Aug 16, 2021
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.

manual_flatten suggests code that does not compile manual_flatten suggestin make code not compilable
5 participants