Skip to content

Commit 40e1a46

Browse files
committed
Silence reduntant type errors on assignment instead of equality check
1 parent 8863848 commit 40e1a46

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1177,12 +1177,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11771177
};
11781178
}
11791179
if eq {
1180+
let span = span.shrink_to_hi();
11801181
err.span_suggestion_verbose(
1181-
span.shrink_to_hi(),
1182+
span,
11821183
"you might have meant to compare for equality",
11831184
'=',
11841185
applicability,
11851186
);
1187+
if self.tcx.sess.parse_sess.silence_missing_comparison.borrow().contains(&span) {
1188+
err.delay_as_bug();
1189+
}
11861190
}
11871191

11881192
// If the assignment expression itself is ill-formed, don't

Diff for: compiler/rustc_parse/src/parser/expr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,7 @@ impl<'a> Parser<'a> {
24842484
// Avoid unnecessary resolve errors as we know we'll have some.
24852485
*self.sess.silence_resolve_errors.borrow_mut() = true;
24862486
}
2487+
self.sess.silence_missing_comparison.borrow_mut().append(&mut checker.seen_comparison);
24872488

24882489
if let ExprKind::Let(_, _, _, None) = cond.kind {
24892490
// Remove the last feature gating of a `let` expression since it's stable.
@@ -3563,6 +3564,7 @@ struct CondChecker<'a> {
35633564
missing_let: Option<errors::MaybeMissingLet>,
35643565
seen_missing_let: bool,
35653566
comparison: Option<errors::MaybeComparison>,
3567+
seen_comparison: Vec<Span>,
35663568
}
35673569

35683570
impl<'a> CondChecker<'a> {
@@ -3573,6 +3575,7 @@ impl<'a> CondChecker<'a> {
35733575
missing_let: None,
35743576
seen_missing_let: false,
35753577
comparison: None,
3578+
seen_comparison: vec![],
35763579
}
35773580
}
35783581
}
@@ -3585,6 +3588,9 @@ impl MutVisitor for CondChecker<'_> {
35853588
match e.kind {
35863589
ExprKind::Let(_, _, _, ref mut is_recovered @ None) => {
35873590
if let Some(reason) = self.forbid_let_reason {
3591+
if let Some(comparison) = self.comparison {
3592+
self.seen_comparison.push(comparison.span);
3593+
}
35883594
*is_recovered =
35893595
Some(self.parser.sess.emit_err(errors::ExpectedExpressionFoundLet {
35903596
span,
@@ -3630,7 +3636,8 @@ impl MutVisitor for CondChecker<'_> {
36303636
Some(errors::MaybeMissingLet { span: rhs.span.shrink_to_lo() });
36313637
}
36323638
let comparison = self.comparison;
3633-
self.comparison = Some(errors::MaybeComparison { span: span.shrink_to_hi() });
3639+
let span = span.shrink_to_hi();
3640+
self.comparison = Some(errors::MaybeComparison { span });
36343641
noop_visit_expr(e, self);
36353642
self.forbid_let_reason = forbid_let_reason;
36363643
self.missing_let = missing_let;

Diff for: compiler/rustc_session/src/parse.rs

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ pub struct ParseSess {
220220
/// Track parse errors that will cause unnecessary resolve errors. This is a crude mechanism,
221221
/// we should do something that is scope-based, instead of crate-global.
222222
pub silence_resolve_errors: Lock<bool>,
223+
/// Track parse errors that suggests changing an assignment to be an equality check.
224+
pub silence_missing_comparison: Lock<Vec<Span>>,
223225
}
224226

225227
impl ParseSess {
@@ -251,6 +253,7 @@ impl ParseSess {
251253
proc_macro_quoted_spans: Default::default(),
252254
attr_id_generator: AttrIdGenerator::new(),
253255
silence_resolve_errors: Default::default(),
256+
silence_missing_comparison: Default::default(),
254257
}
255258
}
256259

Diff for: tests/ui/expr/if/bad-if-let-suggestion-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
fn a() {
55
if let x = 1 && i = 2 {}
66
//~^ ERROR expected expression, found `let` statement
7-
//~| ERROR mismatched types
87
}
98

109
fn main() {}

Diff for: tests/ui/expr/if/bad-if-let-suggestion-2.stderr

+1-13
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,5 @@ help: you might have meant to compare for equality
1414
LL | if let x = 1 && i == 2 {}
1515
| +
1616

17-
error[E0308]: mismatched types
18-
--> $DIR/bad-if-let-suggestion-2.rs:5:8
19-
|
20-
LL | if let x = 1 && i = 2 {}
21-
| ^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
22-
|
23-
help: you might have meant to compare for equality
24-
|
25-
LL | if let x = 1 && i == 2 {}
26-
| +
27-
28-
error: aborting due to 2 previous errors
17+
error: aborting due to previous error
2918

30-
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)