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

Suggest using matches or adding == on x == a || b || c #128159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

Fixes #128132.

This has two approaches. Given some expr == a || b || c

  1. If all of a, b, c, ... are approximately "pattern-like" i.e. only contain expr kinds that should show up in patterns, and the type is StructuralPeq, then we suggest changing the expression to a matches!().
  2. If not, then if the LHS (expr) is a local, then we suggest changing the expression to expr == a || expr == b || expr == c.

@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2024
@@ -0,0 +1,15 @@
fn main() {
let x = 1;
if x == 1 || 2 || 3 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test for x == 1 || 2 || a where let a = 3;

Comment on lines +3383 to +3392
lhs,
rhs,
),
hir_id: parent_hir_id,
span: full_span,
..
}) = self.tcx.parent_hir_node(expr.hir_id)
else {
return;
};
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be best if we documented this code. It took some time for me to fully realize what we are doing here. Consider documenting that:

  • We start to see if a suggestion is applicable when b in x == a || b isn't a bool. (type mismatch)
  • We don't try to suggest when b cannot be coerced to x.

// Track the span of the outermost `||` expr.
let mut full_span = full_span;

// Walk up the expr tree gathering up the binop spans of any subsequent `|| a || b || c`.
Copy link
Member

Choose a reason for hiding this comment

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

You might want to move this comment about walking to before the comment for is_literal so the reader knows what we would be doing better

Comment on lines +3407 to +3408
// Coercion here is not totally right, but w/e.
if !self.can_coerce(expr_ty, lhs_ty) {
Copy link
Member

Choose a reason for hiding this comment

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

This only checks one of the types of the exprs can be coerced. I can think of a weird way where the user wanted to write x == a | b | c in place of x == a || b || c where c has a different type to a or b or x but its BitOr::Output is the same type so the equality would work. I think you might want to bullet-proof this by checking that everything in the binops have the same types.

.into_iter()
.chain(binop_spans.into_iter().map(|span| (span, "|".to_string())))
.collect(),
Applicability::MachineApplicable,
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be MachineApplicable. We've only approximated that the exprs might be valid patterns but we aren't sure.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2024
@lolbinarycat lolbinarycat added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add diagnostic hint when or-ing values which may be missing an equality comparison
6 participants