Skip to content

Commit c45eacd

Browse files
committed
rustc: Add long diagnostics for E0162
1 parent 48a376d commit c45eacd

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/librustc/diagnostics.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ reference when using guards or refactor the entire expression, perhaps by
112112
putting the condition inside the body of the arm.
113113
"##,
114114

115+
E0162: r##"
116+
An if-let pattern attempts to match the pattern, and enters the body if the
117+
match was succesful. If the match is irrefutable (when it cannot fail to match),
118+
use a regular `let`-binding instead. For instance:
119+
120+
struct Irrefutable(i32);
121+
let irr = Irrefutable(0);
122+
123+
// This fails to compile because the match is irrefutable.
124+
if let Irrefutable(x) = irr {
125+
// This body will always be executed.
126+
foo(x);
127+
}
128+
129+
// Try this instead:
130+
let Irrefutable(x) = irr;
131+
foo(x);
132+
"##,
133+
115134
E0297: r##"
116135
Patterns used to bind names must be irrefutable. That is, they must guarantee
117136
that a name will be extracted in all cases. Instead of pattern matching the
@@ -220,7 +239,6 @@ register_diagnostics! {
220239
E0152,
221240
E0158,
222241
E0161,
223-
E0162,
224242
E0165,
225243
E0170,
226244
E0261, // use of undeclared lifetime name

0 commit comments

Comments
 (0)