Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backquotes to have better looking rust code #24523

Merged
merged 2 commits into from
Apr 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
into a variable called `op_string` while simultaneously requiring the inner
String to be moved into a variable called `s`.

```
let x = Some("s".to_string());
match x {
op_string @ Some(s) => ...
None => ...
}
```

See also Error 303.
"##,
Expand All @@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
referenced in the pattern guard code. Doing so however would prevent the name
from being available in the body of the match arm. Consider the following:

```
match Some("hi".to_string()) {
Some(s) if s.len() == 0 => // use s.
...
}
```

The variable `s` has type String, and its use in the guard is as a variable of
type String. The guard code effectively executes in a separate scope to the body
Expand All @@ -102,11 +106,13 @@ become unavailable in the body of the arm. Although this example seems
innocuous, the problem is most clear when considering functions that take their
argument by value.

```
match Some("hi".to_string()) {
Some(s) if { drop(s); false } => (),
Some(s) => // use s.
...
}
```

The value would be dropped in the guard then become unavailable not only in the
body of that arm but also in all subsequent arms! The solution is to bind by
Expand Down Expand Up @@ -181,8 +187,10 @@ them yourself.
You can build a free-standing crate by adding `#![no_std]` to the crate
attributes:

```
#![feature(no_std)]
#![no_std]
```

See also https://doc.rust-lang.org/book/no-stdlib.html
"##,
Expand All @@ -198,11 +206,13 @@ mutex can be declared `static` as well.

If you want to match against a `static`, consider using a guard instead:

```
static FORTY_TWO: i32 = 42;
match Some(42) {
Some(x) if x == FORTY_TWO => ...
...
}
```
"##,

E0161: r##"
Expand All @@ -218,6 +228,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
match was succesful. If the match is irrefutable (when it cannot fail to match),
use a regular `let`-binding instead. For instance:

```
struct Irrefutable(i32);
let irr = Irrefutable(0);

Expand All @@ -230,13 +241,15 @@ if let Irrefutable(x) = irr {
// Try this instead:
let Irrefutable(x) = irr;
foo(x);
```
"##,

E0165: r##"
A while-let pattern attempts to match the pattern, and enters the body if the
match was succesful. If the match is irrefutable (when it cannot fail to match),
use a regular `let`-binding inside a `loop` instead. For instance:

```
struct Irrefutable(i32);
let irr = Irrefutable(0);

Expand All @@ -250,22 +263,27 @@ loop {
let Irrefutable(x) = irr;
...
}
```
"##,

E0170: r##"
Enum variants are qualified by default. For example, given this type:

```
enum Method {
GET,
POST
}
```

you would match it using:

```
match m {
Method::GET => ...
Method::POST => ...
}
```

If you don't qualify the names, the code will bind new variables named "GET" and
"POST" instead. This behavior is likely not what you want, so rustc warns when
Expand All @@ -274,8 +292,10 @@ that happens.
Qualified names are good practice, and most code works well with them. But if
you prefer them unqualified, you can import the variants into scope:

```
use Method::*;
enum Method { GET, POST }
```
"##,

E0267: r##"
Expand All @@ -295,7 +315,9 @@ E0296: r##"
This error indicates that the given recursion limit could not be parsed. Ensure
that the value provided is a positive integer between quotes, like so:

```
#![recursion_limit="1000"]
```
"##,

E0297: r##"
Expand All @@ -304,6 +326,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
loop variable, consider using a `match` or `if let` inside the loop body. For
instance:

```
// This fails because `None` is not covered.
for Some(x) in xs {
...
Expand All @@ -323,6 +346,7 @@ for item in xs {
...
}
}
```
"##,

E0301: r##"
Expand All @@ -332,11 +356,13 @@ on which the match depends in such a way, that the match would not be
exhaustive. For instance, the following would not match any arm if mutable
borrows were allowed:

```
match Some(()) {
None => { },
option if option.take().is_none() => { /* impossible, option is `Some` */ },
Some(_) => { } // When the previous match failed, the option became `None`.
}
```
"##,

E0302: r##"
Expand All @@ -346,21 +372,24 @@ on which the match depends in such a way, that the match would not be
exhaustive. For instance, the following would not match any arm if assignments
were allowed:

```
match Some(()) {
None => { },
option if { option = None; false } { },
Some(_) => { } // When the previous match failed, the option became `None`.
}
```
"##,

E0303: r##"
In certain cases it is possible for sub-bindings to violate memory safety.
Updates to the borrow checker in a future version of Rust may remove this
restriction, but for now patterns must be rewritten without sub-bindings.

// Before.
match Some("hi".to_string()) {
ref op_string_ref @ Some(ref s) => ...
```
// Code like this...
match Some(5) {
ref op_num @ Some(num) => ...
None => ...
}

Expand All @@ -372,6 +401,7 @@ match Some("hi".to_string()) {
}
None => ...
}
```

The `op_string_ref` binding has type &Option<&String> in both cases.

Expand Down