You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Identify when a stmt could have been parsed as an expr
There are some expressions that can be parsed as a statement without
a trailing semicolon depending on the context, which can lead to
confusing errors due to the same looking code being accepted in some
places and not others. Identify these cases and suggest enclosing in
parenthesis making the parse non-ambiguous without changing the
accepted grammar.
| help: parenthesis are required to parse this as an expression: `({2})`
8
+
9
+
error: expected expression, found `+`
10
+
--> $DIR/expr-as-stmt.rs:12:9
11
+
|
12
+
LL | {2} + 2
13
+
| --- ^ expected expression
14
+
| |
15
+
| help: parenthesis are required to parse this as an expression: `({2})`
16
+
17
+
error: expected expression, found `+`
18
+
--> $DIR/expr-as-stmt.rs:18:12
19
+
|
20
+
LL | { 42 } + foo;
21
+
| ------ ^ expected expression
22
+
| |
23
+
| help: parenthesis are required to parse this as an expression: `({ 42 })`
24
+
25
+
error: ambiguous parse
26
+
--> $DIR/expr-as-stmt.rs:30:5
27
+
|
28
+
LL | if let Some(x) = a { true } else { false }
29
+
| ------------------------------------------ help: parenthesis are required to parse this as an expression: `(if let Some(x) = a { true } else { false })`
30
+
LL | &&
31
+
| ^^
32
+
33
+
error[E0308]: mismatched types
34
+
--> $DIR/expr-as-stmt.rs:7:6
35
+
|
36
+
LL | {2} + {2}
37
+
| ^ expected (), found integer
38
+
|
39
+
= note: expected type `()`
40
+
found type `{integer}`
41
+
42
+
error[E0308]: mismatched types
43
+
--> $DIR/expr-as-stmt.rs:12:6
44
+
|
45
+
LL | {2} + 2
46
+
| ^ expected (), found integer
47
+
|
48
+
= note: expected type `()`
49
+
found type `{integer}`
50
+
51
+
error[E0308]: mismatched types
52
+
--> $DIR/expr-as-stmt.rs:18:7
53
+
|
54
+
LL | { 42 } + foo;
55
+
| ^^ expected (), found integer
56
+
|
57
+
= note: expected type `()`
58
+
found type `{integer}`
59
+
60
+
error[E0308]: mismatched types
61
+
--> $DIR/expr-as-stmt.rs:24:7
62
+
|
63
+
LL | { 3 } * 3
64
+
| ^ expected (), found integer
65
+
|
66
+
= note: expected type `()`
67
+
found type `{integer}`
68
+
69
+
error[E0614]: type `{integer}` cannot be dereferenced
70
+
--> $DIR/expr-as-stmt.rs:24:11
71
+
|
72
+
LL | { 3 } * 3
73
+
| ----- ^^^
74
+
| |
75
+
| help: parenthesis are required to parse this as an expression: `({ 3 })`
76
+
77
+
error: aborting due to 9 previous errors
78
+
79
+
Some errors have detailed explanations: E0308, E0614.
80
+
For more information about an error, try `rustc --explain E0308`.
0 commit comments