Skip to content

Fixed FP in unused_io_amount for Ok(lit), unrachable! and unwrap de… #12217

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

Merged
merged 1 commit into from
Feb 2, 2024
Merged

Fixed FP in unused_io_amount for Ok(lit), unrachable! and unwrap de… #12217

merged 1 commit into from
Feb 2, 2024

Conversation

PartiallyUntyped
Copy link
Contributor

…sugar

Fixes fp caused by linting on Ok(_) for all cases outside binding.

We introduce the following rules for match exprs.

  • panic! and unreachable! are treated as consumed.
  • Ok( ) patterns outside DotDot and Wild are treated as consuming.

changelog: FP [unused_io_amount] when matching Ok(literal) or unreachable

fixes #12208

r? @blyxyas

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 30, 2024
@PartiallyUntyped
Copy link
Contributor Author

Sorry for opening this can of worms, and for taking as long to fix this ._.

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Just some early reviews and a proposal to make the code easier

@@ -130,23 +170,46 @@ fn should_lint<'a>(cx: &LateContext<'a>, mut inner: &'a hir::Expr<'a>) -> Option
check_io_mode(cx, inner)
}

fn pattern_is_ignored_ok(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> bool {
enum OkPatternKind {
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 removed and all instances removed by booleans, and all checking would be easier.

Comment on lines 207 to 210
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
return false;
}
return true;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
return false;
}
return true;
return !cx.tcx.hir().is_inside_const_context(expr.hir_id);

Comment on lines 130 to 127
if let Some(OkPatternKind::WildOrDotDot) = extract_ok_pattern(cx, arm.pat) {
return ControlFlow::Break(arm.pat);
}
Copy link
Member

Choose a reason for hiding this comment

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

With that previous comment, this could be:

Suggested change
if let Some(OkPatternKind::WildOrDotDot) = extract_ok_pattern(cx, arm.pat) {
return ControlFlow::Break(arm.pat);
}
return extract_ok_pattern(cx, arm.pat);

This would also need to return a bool. But as this is the only the branch that returns meaningful information, making the switch won't be hard I'd suppose.

Copy link
Contributor Author

@PartiallyUntyped PartiallyUntyped Feb 1, 2024

Choose a reason for hiding this comment

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

If we return a bool here, then we lose information on which arm is doing what. These are used to pull the right patterns out of a slice of 2 pats.

Comment on lines 151 to 152
if let (ControlFlow::Break(ok_pat), ControlFlow::Break(_)) = (ok_arm, err_arm) {
emit_lint(cx, expr.span, op, &[ok_pat.span]);
Copy link
Member

Choose a reason for hiding this comment

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

This would then be:

Suggested change
if let (ControlFlow::Break(ok_pat), ControlFlow::Break(_)) = (ok_arm, err_arm) {
emit_lint(cx, expr.span, op, &[ok_pat.span]);
if ok_arm && err_arm {
emit_lint(cx, expr.span, op, &[ok_arm.pat.span]);

@PartiallyUntyped
Copy link
Contributor Author

Do you agree with the changes to the structural matching of the match?

@blyxyas
Copy link
Member

blyxyas commented Feb 1, 2024

Watch match specifically? Like, the general pattern matching? It's good, even if a little bit confusing (I assume because of the time urgency)

@PartiallyUntyped
Copy link
Contributor Author

Sorry, basically based on your comments on the issue, I changed the matching logic to only consider two arm cases and nothing else.

Once this is stabilized I'd like to clean it up further.

@PartiallyUntyped
Copy link
Contributor Author

Ok, this looks better.

Copy link
Member

@blyxyas blyxyas 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 that this may be ready to merged! What do you think? Is there something else that needs to be addressed?

@PartiallyUntyped
Copy link
Contributor Author

PartiallyUntyped commented Feb 1, 2024

I hold no objections, but I have the hunch that this can be achieved in a simpler manner.

If you think it's okay to merge — given the urgency — then okay. Worst case we revert both commits completely.

Would you like me to squash?

@blyxyas
Copy link
Member

blyxyas commented Feb 1, 2024

Please :)

We introduce the following rules for match exprs.
- `panic!` and `unreachable!` are treated as consumption.
- guard expressions in any arm imply consumption.

For match exprs:
- Lint only if exacrtly 2 non-consuming arms exist
- Lint only if one arm is an `Ok(_)` and the other is `Err(_)`

Added additional requirement that for a block return expression
that is a match, the source must be `Normal`.

changelog: FP [`unused_io_amount`] when matching Ok(literal)
@PartiallyUntyped
Copy link
Contributor Author

Done, thanks again and sorry :(

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Meow meow LGTM! Thanks ❤️

@blyxyas
Copy link
Member

blyxyas commented Feb 1, 2024

No need to be sorry, you didn't see an edge case, in the same way that I didn't. Instead of having shame of our human error, we should be thankful of the person that filled the issue that led to this moment.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 1, 2024

📌 Commit fe8c2e2 has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 1, 2024

⌛ Testing commit fe8c2e2 with merge 0b00680...

bors added a commit that referenced this pull request Feb 1, 2024
Fixed FP in `unused_io_amount` for Ok(lit), unrachable! and unwrap de…

…sugar

Fixes fp caused by linting on Ok(_) for all cases outside binding.

We introduce the following rules for match exprs.
- `panic!` and `unreachable!` are treated as consumed.
- `Ok( )` patterns outside `DotDot` and `Wild` are treated as consuming.

changelog: FP [`unused_io_amount`] when matching Ok(literal) or unreachable

fixes #12208

r? `@blyxyas`
@bors
Copy link
Contributor

bors commented Feb 1, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 1, 2024

@bors retry

@bors
Copy link
Contributor

bors commented Feb 1, 2024

⌛ Testing commit fe8c2e2 with merge 3491ebb...

bors added a commit that referenced this pull request Feb 1, 2024
Fixed FP in `unused_io_amount` for Ok(lit), unrachable! and unwrap de…

…sugar

Fixes fp caused by linting on Ok(_) for all cases outside binding.

We introduce the following rules for match exprs.
- `panic!` and `unreachable!` are treated as consumed.
- `Ok( )` patterns outside `DotDot` and `Wild` are treated as consuming.

changelog: FP [`unused_io_amount`] when matching Ok(literal) or unreachable

fixes #12208

r? `@blyxyas`
@bors
Copy link
Contributor

bors commented Feb 1, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 1, 2024

@bors retry

@bors
Copy link
Contributor

bors commented Feb 1, 2024

⌛ Testing commit fe8c2e2 with merge 128a219...

bors added a commit that referenced this pull request Feb 1, 2024
Fixed FP in `unused_io_amount` for Ok(lit), unrachable! and unwrap de…

…sugar

Fixes fp caused by linting on Ok(_) for all cases outside binding.

We introduce the following rules for match exprs.
- `panic!` and `unreachable!` are treated as consumed.
- `Ok( )` patterns outside `DotDot` and `Wild` are treated as consuming.

changelog: FP [`unused_io_amount`] when matching Ok(literal) or unreachable

fixes #12208

r? `@blyxyas`
@bors
Copy link
Contributor

bors commented Feb 1, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Feb 2, 2024

@bors retry

@bors
Copy link
Contributor

bors commented Feb 2, 2024

⌛ Testing commit fe8c2e2 with merge 9b6f866...

@bors
Copy link
Contributor

bors commented Feb 2, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 9b6f866 to master...

@bors bors merged commit 9b6f866 into rust-lang:master Feb 2, 2024
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.

unused_io_amount false positive if the result is matched by Ok(<literal>) or unreachable!() cases
4 participants