Skip to content

Commit 264d365

Browse files
Update diagnostics.rs
1 parent 79a402b commit 264d365

File tree

1 file changed

+98
-76
lines changed

1 file changed

+98
-76
lines changed

src/librustc/diagnostics.rs

Lines changed: 98 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
7575
into a variable called `op_string` while simultaneously requiring the inner
7676
String to be moved into a variable called `s`.
7777
78-
let x = Some("s".to_string());
79-
match x {
80-
op_string @ Some(s) => ...
81-
None => ...
82-
}
78+
```
79+
let x = Some("s".to_string());
80+
match x {
81+
op_string @ Some(s) => ...
82+
None => ...
83+
}
84+
```
8385
8486
See also Error 303.
8587
"##,
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
9092
referenced in the pattern guard code. Doing so however would prevent the name
9193
from being available in the body of the match arm. Consider the following:
9294
93-
match Some("hi".to_string()) {
94-
Some(s) if s.len() == 0 => // use s.
95-
...
96-
}
95+
```
96+
match Some("hi".to_string()) {
97+
Some(s) if s.len() == 0 => // use s.
98+
...
99+
}
100+
```
97101
98102
The variable `s` has type String, and its use in the guard is as a variable of
99103
type String. The guard code effectively executes in a separate scope to the body
@@ -102,11 +106,13 @@ become unavailable in the body of the arm. Although this example seems
102106
innocuous, the problem is most clear when considering functions that take their
103107
argument by value.
104108
105-
match Some("hi".to_string()) {
106-
Some(s) if { drop(s); false } => (),
107-
Some(s) => // use s.
108-
...
109-
}
109+
```
110+
match Some("hi".to_string()) {
111+
Some(s) if { drop(s); false } => (),
112+
Some(s) => // use s.
113+
...
114+
}
115+
```
110116
111117
The value would be dropped in the guard then become unavailable not only in the
112118
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -123,37 +129,43 @@ This limitation may be removed in a future version of Rust.
123129
124130
Wrong example:
125131
126-
struct X { x: (), }
127-
128-
let x = Some((X { x: () }, X { x: () }));
129-
match x {
130-
Some((y, ref z)) => {},
131-
None => panic!()
132-
}
132+
```
133+
struct X { x: (), }
134+
135+
let x = Some((X { x: () }, X { x: () }));
136+
match x {
137+
Some((y, ref z)) => {},
138+
None => panic!()
139+
}
140+
```
133141
134142
You have two solutions:
135143
1. Bind the pattern's values the same way:
136144
137-
struct X { x: (), }
138-
139-
let x = Some((X { x: () }, X { x: () }));
140-
match x {
141-
Some((ref y, ref z)) => {},
142-
// or Some((y, z)) => {}
143-
None => panic!()
144-
}
145+
```
146+
struct X { x: (), }
147+
148+
let x = Some((X { x: () }, X { x: () }));
149+
match x {
150+
Some((ref y, ref z)) => {},
151+
// or Some((y, z)) => {}
152+
None => panic!()
153+
}
154+
```
145155
146156
2. Implement the `Copy` trait for the X structure (however, please
147157
keep in mind that the first solution should be preferred!):
148158
149-
#[derive(Clone, Copy)]
150-
struct X { x: (), }
151-
152-
let x = Some((X { x: () }, X { x: () }));
153-
match x {
154-
Some((y, ref z)) => {},
155-
None => panic!()
156-
}
159+
```
160+
#[derive(Clone, Copy)]
161+
struct X { x: (), }
162+
163+
let x = Some((X { x: () }, X { x: () }));
164+
match x {
165+
Some((y, ref z)) => {},
166+
None => panic!()
167+
}
168+
```
157169
"##,
158170

159171
E0015: r##"
@@ -175,8 +187,10 @@ them yourself.
175187
You can build a free-standing crate by adding `#![no_std]` to the crate
176188
attributes:
177189
178-
#![feature(no_std)]
179-
#![no_std]
190+
```
191+
#![feature(no_std)]
192+
#![no_std]
193+
```
180194
181195
See also https://doc.rust-lang.org/book/no-stdlib.html
182196
"##,
@@ -192,11 +206,13 @@ mutex can be declared `static` as well.
192206
193207
If you want to match against a `static`, consider using a guard instead:
194208
195-
static FORTY_TWO: i32 = 42;
196-
match Some(42) {
197-
Some(x) if x == FORTY_TWO => ...
198-
...
199-
}
209+
```
210+
static FORTY_TWO: i32 = 42;
211+
match Some(42) {
212+
Some(x) if x == FORTY_TWO => ...
213+
...
214+
}
215+
```
200216
"##,
201217

202218
E0161: r##"
@@ -212,54 +228,60 @@ An if-let pattern attempts to match the pattern, and enters the body if the
212228
match was succesful. If the match is irrefutable (when it cannot fail to match),
213229
use a regular `let`-binding instead. For instance:
214230
215-
struct Irrefutable(i32);
216-
let irr = Irrefutable(0);
217-
218-
// This fails to compile because the match is irrefutable.
219-
if let Irrefutable(x) = irr {
220-
// This body will always be executed.
221-
foo(x);
222-
}
223-
224-
// Try this instead:
225-
let Irrefutable(x) = irr;
231+
```
232+
struct Irrefutable(i32);
233+
let irr = Irrefutable(0);
234+
235+
// This fails to compile because the match is irrefutable.
236+
if let Irrefutable(x) = irr {
237+
// This body will always be executed.
226238
foo(x);
239+
}
240+
241+
// Try this instead:
242+
let Irrefutable(x) = irr;
243+
foo(x);
244+
```
227245
"##,
228246

229247
E0165: r##"
230248
A while-let pattern attempts to match the pattern, and enters the body if the
231249
match was succesful. If the match is irrefutable (when it cannot fail to match),
232250
use a regular `let`-binding inside a `loop` instead. For instance:
233251
234-
struct Irrefutable(i32);
235-
let irr = Irrefutable(0);
236-
237-
// This fails to compile because the match is irrefutable.
238-
while let Irrefutable(x) = irr {
239-
...
240-
}
241-
242-
// Try this instead:
243-
loop {
244-
let Irrefutable(x) = irr;
245-
...
246-
}
252+
```
253+
struct Irrefutable(i32);
254+
let irr = Irrefutable(0);
255+
256+
// This fails to compile because the match is irrefutable.
257+
while let Irrefutable(x) = irr {
258+
...
259+
}
260+
261+
// Try this instead:
262+
loop {
263+
let Irrefutable(x) = irr;
264+
...
265+
}
266+
```
247267
"##,
248268

249269
E0170: r##"
250270
Enum variants are qualified by default. For example, given this type:
251271
252-
enum Method {
253-
GET,
254-
POST
255-
}
272+
```
273+
enum Method {
274+
GET,
275+
POST
276+
}
277+
```
256278
257279
you would match it using:
258280
259-
match m {
260-
Method::GET => ...
261-
Method::POST => ...
262-
}
281+
match m {
282+
Method::GET => ...
283+
Method::POST => ...
284+
}
263285
264286
If you don't qualify the names, the code will bind new variables named "GET" and
265287
"POST" instead. This behavior is likely not what you want, so rustc warns when

0 commit comments

Comments
 (0)