Skip to content

Commit d472d91

Browse files
committed
address review
1 parent 806b443 commit d472d91

File tree

5 files changed

+83
-32
lines changed

5 files changed

+83
-32
lines changed

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::{AssignOpKind, Label};
77
use rustc_errors::codes::*;
88
use rustc_errors::{
99
Applicability, Diag, DiagArgValue, DiagCtxtHandle, DiagSymbolList, Diagnostic,
10-
EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic, struct_span_code_err,
10+
EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic,
1111
};
1212
use rustc_hir as hir;
1313
use rustc_hir::ExprKind;
@@ -1146,13 +1146,16 @@ pub(crate) fn maybe_emit_plus_equals_diagnostic<'a>(
11461146
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &right.kind
11471147
&& matches!(path.res, hir::def::Res::Local(_))
11481148
{
1149-
let mut err = struct_span_code_err!(
1150-
fnctxt.dcx(),
1149+
let mut err = fnctxt.dcx().struct_span_err(
11511150
assign_op.span,
1152-
E0368,
11531151
"binary assignment operation `+=` cannot be used in a let chain",
11541152
);
11551153

1154+
err.span_label(
1155+
lhs_expr.span,
1156+
"you are add-assigning the right-hand side expression to the result of this let-chain",
1157+
);
1158+
11561159
err.span_label(assign_op.span, "cannot use `+=` in a let chain");
11571160

11581161
err.span_suggestion(

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::errors::{
4848
NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
4949
TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
5050
};
51+
use crate::op::contains_let_in_chain;
5152
use crate::{
5253
BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs,
5354
TupleArgumentsFlag, cast, fatally_break_rust, report_unexpected_variant_res, type_error_struct,
@@ -1251,7 +1252,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12511252

12521253
// Skip suggestion if LHS contains a let-chain at this would likely be spurious
12531254
// cc: https://github.com/rust-lang/rust/issues/147664
1254-
if crate::op::contains_let_in_chain(lhs) {
1255+
if contains_let_in_chain(lhs) {
12551256
return;
12561257
}
12571258

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ edition:2024
2+
//@ run-rustfix
3+
4+
#![allow(irrefutable_let_patterns)]
5+
6+
fn test_where_left_is_not_let() {
7+
let y = 2;
8+
if let _ = 1 && true && y == 2 {};
9+
//~^ ERROR expected expression, found `let` statement
10+
//~| NOTE only supported directly in conditions of `if` and `while` expressions
11+
//~| ERROR mismatched types
12+
//~| NOTE expected `bool`, found integer
13+
//~| NOTE you are add-assigning the right-hand side expression to the result of this let-chain
14+
//~| NOTE expected because this is `bool`
15+
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
16+
//~| NOTE cannot use `+=` in a let chain
17+
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
18+
}
19+
20+
fn test_where_left_is_let() {
21+
let y = 2;
22+
if let _ = 1 && y == 2 {};
23+
//~^ ERROR expected expression, found `let` statement
24+
//~| NOTE only supported directly in conditions of `if` and `while` expressions
25+
//~| ERROR mismatched types
26+
//~| NOTE expected `bool`, found integer
27+
//~| NOTE you are add-assigning the right-hand side expression to the result of this let-chain
28+
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
29+
//~| NOTE cannot use `+=` in a let chain
30+
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
31+
}
32+
33+
fn main() {
34+
test_where_left_is_let();
35+
test_where_left_is_not_let()
36+
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
//@ edition:2024
2+
//@ run-rustfix
3+
4+
#![allow(irrefutable_let_patterns)]
25

36
fn test_where_left_is_not_let() {
4-
let mut y = 2;
5-
if let x = 1 && true && y += 2 {};
7+
let y = 2;
8+
if let _ = 1 && true && y += 2 {};
69
//~^ ERROR expected expression, found `let` statement
710
//~| NOTE only supported directly in conditions of `if` and `while` expressions
811
//~| ERROR mismatched types
912
//~| NOTE expected `bool`, found integer
13+
//~| NOTE you are add-assigning the right-hand side expression to the result of this let-chain
1014
//~| NOTE expected because this is `bool`
1115
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
1216
//~| NOTE cannot use `+=` in a let chain
1317
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
1418
}
1519

1620
fn test_where_left_is_let() {
17-
let mut y = 2;
18-
if let x = 1 && y += 2 {};
21+
let y = 2;
22+
if let _ = 1 && y += 2 {};
1923
//~^ ERROR expected expression, found `let` statement
2024
//~| NOTE only supported directly in conditions of `if` and `while` expressions
2125
//~| ERROR mismatched types
2226
//~| NOTE expected `bool`, found integer
27+
//~| NOTE you are add-assigning the right-hand side expression to the result of this let-chain
2328
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
2429
//~| NOTE cannot use `+=` in a let chain
2530
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
2631
}
2732

28-
fn main() {}
33+
fn main() {
34+
test_where_left_is_let();
35+
test_where_left_is_not_let()
36+
}
Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
error: expected expression, found `let` statement
2-
--> $DIR/let-chains-assign-add-incorrect.rs:5:8
2+
--> $DIR/let-chains-assign-add-incorrect.rs:8:8
33
|
4-
LL | if let x = 1 && true && y += 2 {};
4+
LL | if let _ = 1 && true && y += 2 {};
55
| ^^^^^^^^^
66
|
77
= note: only supported directly in conditions of `if` and `while` expressions
88

99
error: expected expression, found `let` statement
10-
--> $DIR/let-chains-assign-add-incorrect.rs:18:8
10+
--> $DIR/let-chains-assign-add-incorrect.rs:22:8
1111
|
12-
LL | if let x = 1 && y += 2 {};
12+
LL | if let _ = 1 && y += 2 {};
1313
| ^^^^^^^^^
1414
|
1515
= note: only supported directly in conditions of `if` and `while` expressions
1616

1717
error[E0308]: mismatched types
18-
--> $DIR/let-chains-assign-add-incorrect.rs:5:29
18+
--> $DIR/let-chains-assign-add-incorrect.rs:8:29
1919
|
20-
LL | if let x = 1 && true && y += 2 {};
20+
LL | if let _ = 1 && true && y += 2 {};
2121
| ----------------- ^ expected `bool`, found integer
2222
| |
2323
| expected because this is `bool`
2424

25-
error[E0368]: binary assignment operation `+=` cannot be used in a let chain
26-
--> $DIR/let-chains-assign-add-incorrect.rs:5:31
25+
error: binary assignment operation `+=` cannot be used in a let chain
26+
--> $DIR/let-chains-assign-add-incorrect.rs:8:31
2727
|
28-
LL | if let x = 1 && true && y += 2 {};
29-
| ^^ cannot use `+=` in a let chain
28+
LL | if let _ = 1 && true && y += 2 {};
29+
| ---------------------- ^^ cannot use `+=` in a let chain
30+
| |
31+
| you are add-assigning the right-hand side expression to the result of this let-chain
3032
|
3133
help: you might have meant to compare with `==` instead of assigning with `+=`
3234
|
33-
LL - if let x = 1 && true && y += 2 {};
34-
LL + if let x = 1 && true && y == 2 {};
35+
LL - if let _ = 1 && true && y += 2 {};
36+
LL + if let _ = 1 && true && y == 2 {};
3537
|
3638

3739
error[E0308]: mismatched types
38-
--> $DIR/let-chains-assign-add-incorrect.rs:18:21
40+
--> $DIR/let-chains-assign-add-incorrect.rs:22:21
3941
|
40-
LL | if let x = 1 && y += 2 {};
42+
LL | if let _ = 1 && y += 2 {};
4143
| ^ expected `bool`, found integer
4244

43-
error[E0368]: binary assignment operation `+=` cannot be used in a let chain
44-
--> $DIR/let-chains-assign-add-incorrect.rs:18:23
45+
error: binary assignment operation `+=` cannot be used in a let chain
46+
--> $DIR/let-chains-assign-add-incorrect.rs:22:23
4547
|
46-
LL | if let x = 1 && y += 2 {};
47-
| ^^ cannot use `+=` in a let chain
48+
LL | if let _ = 1 && y += 2 {};
49+
| -------------- ^^ cannot use `+=` in a let chain
50+
| |
51+
| you are add-assigning the right-hand side expression to the result of this let-chain
4852
|
4953
help: you might have meant to compare with `==` instead of assigning with `+=`
5054
|
51-
LL - if let x = 1 && y += 2 {};
52-
LL + if let x = 1 && y == 2 {};
55+
LL - if let _ = 1 && y += 2 {};
56+
LL + if let _ = 1 && y == 2 {};
5357
|
5458

5559
error: aborting due to 6 previous errors
5660

57-
Some errors have detailed explanations: E0308, E0368.
58-
For more information about an error, try `rustc --explain E0308`.
61+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)