Skip to content

Commit 50b3a3d

Browse files
committed
add a lot of text about lints
1 parent 78b076d commit 50b3a3d

File tree

7 files changed

+824
-0
lines changed

7 files changed

+824
-0
lines changed

src/doc/rustc/src/lints/groups.md

+28
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
# Lint Groups
2+
3+
`rustc` has the concept of a "lint group", where you can toggle several warnings
4+
through one name.
5+
6+
For example, the `nonstandard-style` lint sets `non-camel-case-types`,
7+
`non-snake-case`, and `non-upper-case-globals` all at once. So these are
8+
equivalent:
9+
10+
```bash
11+
$ rustc -D nonstandard-style
12+
$ rustc -D non-camel-case-types -D non-snake-case -D non-upper-case-globals
13+
```
14+
15+
Here's a list of each lint group, and the lints that they are made up of:
16+
17+
| group | description | lints |
18+
|---------------------|---------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
19+
| nonstandard-style | Violation of standard naming conventions | non-camel-case-types, non-snake-case, non-upper-case-globals |
20+
| warnings | all lints that would be issuing warnings | all lints that would be issuing warnings |
21+
| edition-2018 | Lints that will be turned into errors in Rust 2018 | tyvar-behind-raw-pointer |
22+
| rust-2018-idioms | Lints to nudge you toward idiomatic features of Rust 2018 | bare-trait-object, unreachable-pub |
23+
| unused | These lints detect things being declared but not used | unused-imports, unused-variables, unused-assignments, dead-code, unused-mut, unreachable-code, unreachable-patterns, unused-must-use, unused-unsafe, path-statements, unused-attributes, unused-macros, unused-allocation, unused-doc-comment, unused-extern-crates, unused-features, unused-parens |
24+
| future-incompatible | Lints that detect code that has future-compatibility problems | private-in-public, pub-use-of-private-extern-crate, patterns-in-fns-without-body, safe-extern-statics, invalid-type-param-default, legacy-directory-ownership, legacy-imports, legacy-constructor-visibility, missing-fragment-specifier, illegal-floating-point-literal-pattern, anonymous-parameters, parenthesized-params-in-types-and-modules, late-bound-lifetime-arguments, safe-packed-borrows, incoherent-fundamental-impls, tyvar-behind-raw-pointer, unstable-name-collision |
25+
26+
Additionally, there's a `bad-style` lint group that's a deprecated alias for `nonstandard-style`.
27+
28+
Finally, you can also see the table above by invoking `rustc -W help`. This will give you the exact values for the specific
29+
compiler you have installed.

src/doc/rustc/src/lints/index.md

+27
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
# Lints
2+
3+
In software, a "lint" is a tool used to help improve your source code. The
4+
Rust compiler contains a number of lints, and when it compiles your code, it will
5+
also run the lints. These lints may produce a warning, an error, or nothing at all,
6+
depending on how you've configured things.
7+
8+
Here's a small example:
9+
10+
```rust
11+
$ cat main.rs
12+
fn main() {
13+
let x = 5;
14+
}
15+
> rustc main.rs
16+
warning: unused variable: `x`
17+
--> main.rs:2:9
18+
|
19+
2 | let x = 5;
20+
| ^
21+
|
22+
= note: #[warn(unused_variables)] on by default
23+
= note: to avoid this warning, consider using `_x` instead
24+
```
25+
26+
This is the `unused_variables` lint, and it tells you that you've introduced
27+
a variable that you don't use in your code. That's not *wrong*, so it's not
28+
an error, but it might be a bug, so you get a warning.

src/doc/rustc/src/lints/levels.md

+251
Original file line numberDiff line numberDiff line change
@@ -1 +1,252 @@
11
# Lint levels
2+
3+
In `rustc`, lints are divided into four *levels*:
4+
5+
1. allow
6+
2. warn
7+
3. deny
8+
4. forbid
9+
10+
Each lint has a default level (explained in the lint listing later in this
11+
chapter), and the compiler has a default warning level. First, let's explain
12+
what these levels mean, and then we'll talk about configuration.
13+
14+
## allow
15+
16+
These lints exist, but by default, do nothing. For example, consider this
17+
source:
18+
19+
```rust
20+
pub fn foo() {}
21+
```
22+
23+
Compiling this file produces no warnings:
24+
25+
```rust
26+
$ rustc lib.rs --crate-type=lib
27+
$
28+
```
29+
30+
But this code violates the `missing_docs` lint.
31+
32+
These lints exist mostly to be manually turned on via configuration, as we'll
33+
talk about later in this section.
34+
35+
## warn
36+
37+
The 'warn' lint level will produce a warning if you violate the lint. For example,
38+
this code runs afoul of the `unused_variable` lint:
39+
40+
```rust
41+
pub fn foo() {
42+
let x = 5;
43+
}
44+
```
45+
46+
This will produce this warning:
47+
48+
```bash
49+
> rustc lib.rs --crate-type=lib
50+
warning: unused variable: `x`
51+
--> lib.rs:2:9
52+
|
53+
2 | let x = 5;
54+
| ^
55+
|
56+
= note: #[warn(unused_variables)] on by default
57+
= note: to avoid this warning, consider using `_x` instead
58+
```
59+
60+
## deny
61+
62+
A 'deny' lint produces an error if you violate it. For example, this code
63+
runs into the `exceeding_bitshifts` lint.
64+
65+
```rust
66+
fn main() {
67+
100u8 << 10;
68+
}
69+
```
70+
71+
```bash
72+
> rustc main.rs
73+
error: bitshift exceeds the type's number of bits
74+
--> main.rs:2:13
75+
|
76+
2 | 100u8 << 10;
77+
| ^^^^^^^^^^^
78+
|
79+
= note: #[deny(exceeding_bitshifts)] on by default
80+
```
81+
82+
What's the difference between an error from a lint and a regular old error?
83+
Lints are configurable via levels, so in a similar way to 'allow' lints,
84+
warnings that are 'deny' by default let you allow them. Similarly, you may
85+
wish to set up a lint that is `warn` by default to produce an error instead.
86+
This lint level gives you that.
87+
88+
## forbid
89+
90+
'forbid' is a special lint level that's stronger than 'deny'. It's the same
91+
as 'deny' in that a lint at this level will produce an error, but unlike the
92+
'deny' level, the 'forbid' level can not be overridden to be anything lower
93+
than an error.
94+
95+
## Configuring warning levels
96+
97+
Remember our `missing_docs` example from the 'allow' lint level?
98+
99+
```bash
100+
$ cat lib.rs
101+
pub fn foo() {}
102+
$ rustc lib.rs --crate-type=lib
103+
$
104+
```
105+
106+
We can configure this lint to operate at a higher level, both with
107+
compiler flags, as well as with an attribute in the source code.
108+
109+
You can also "cap" lints so that the compiler can choose to ignore
110+
certain lint levels. We'll talk about that last.
111+
112+
### Via compiler flag
113+
114+
The `-A`, `-W`, `-D`, and `-F` flags let you turn one or more lints
115+
into allowed, warning, deny, or forbid levels, like this:
116+
117+
```bash
118+
$ rustc lib.rs --crate-type=lib -W missing-docs
119+
warning: missing documentation for crate
120+
--> lib.rs:1:1
121+
|
122+
1 | pub fn foo() {}
123+
| ^^^^^^^^^^^^
124+
|
125+
= note: requested on the command line with `-W missing-docs`
126+
127+
warning: missing documentation for a function
128+
--> lib.rs:1:1
129+
|
130+
1 | pub fn foo() {}
131+
| ^^^^^^^^^^^^
132+
> rustc lib.rs --crate-type=lib -D missing-docs
133+
error: missing documentation for crate
134+
--> lib.rs:1:1
135+
|
136+
1 | pub fn foo() {}
137+
| ^^^^^^^^^^^^
138+
|
139+
= note: requested on the command line with `-D missing-docs`
140+
141+
error: missing documentation for a function
142+
--> lib.rs:1:1
143+
|
144+
1 | pub fn foo() {}
145+
| ^^^^^^^^^^^^
146+
147+
error: aborting due to 2 previous errors
148+
```
149+
150+
You can also pass each flag more than once for changing multiple lints:
151+
152+
```bash
153+
rustc lib.rs --crate-type=lib -D missing-docs -D unused-variables
154+
```
155+
156+
And of course, you can mix these four flags together:
157+
158+
```bash
159+
rustc lib.rs --crate-type=lib -D missing-docs -A unused-variables
160+
```
161+
162+
### Via an attribute
163+
164+
You can also modify the lint level with a crate-wide attribute:
165+
166+
```bash
167+
> cat lib.rs
168+
#![warn(missing_docs)]
169+
170+
pub fn foo() {}
171+
$ rustc lib.rs --crate-type=lib
172+
warning: missing documentation for crate
173+
--> lib.rs:1:1
174+
|
175+
1 | / #![warn(missing_docs)]
176+
2 | |
177+
3 | | pub fn foo() {}
178+
| |_______________^
179+
|
180+
note: lint level defined here
181+
--> lib.rs:1:9
182+
|
183+
1 | #![warn(missing_docs)]
184+
| ^^^^^^^^^^^^
185+
186+
warning: missing documentation for a function
187+
--> lib.rs:3:1
188+
|
189+
3 | pub fn foo() {}
190+
| ^^^^^^^^^^^^
191+
```
192+
193+
All four, `warn`, `allow`, `deny`, and `forbid` all work this way.
194+
195+
You can also pass in multiple lints per attribute:
196+
197+
```rust
198+
#![warn(missing_docs, unused_variables)]
199+
200+
pub fn foo() {}
201+
```
202+
203+
And use multiple attributes together:
204+
205+
```rust
206+
#![warn(missing_docs)]
207+
#![deny(unused_variables)]
208+
209+
pub fn foo() {}
210+
```
211+
212+
### Capping lints
213+
214+
`rustc` supports a flag, `--cap-lints LEVEL` that sets the "lint cap level."
215+
This is the maximum level for all lints. So for example, if we take our
216+
code sample from the "deny" lint level above:
217+
218+
```rust
219+
fn main() {
220+
100u8 << 10;
221+
}
222+
```
223+
224+
And we compile it, capping lints to warn:
225+
226+
```bash
227+
$ rustc lib.rs --cap-lints warn
228+
warning: bitshift exceeds the type's number of bits
229+
--> lib.rs:2:5
230+
|
231+
2 | 100u8 << 10;
232+
| ^^^^^^^^^^^
233+
|
234+
= note: #[warn(exceeding_bitshifts)] on by default
235+
236+
warning: this expression will panic at run-time
237+
--> lib.rs:2:5
238+
|
239+
2 | 100u8 << 10;
240+
| ^^^^^^^^^^^ attempt to shift left with overflow
241+
```
242+
243+
It now only warns, rather than errors. We can go further and allow all lints:
244+
245+
```bash
246+
$ rustc lib.rs --cap-lints allow
247+
$
248+
```
249+
250+
This feature is used heavily by Cargo; it will pass `--cap-lints allow` when
251+
compiling your dependencies, so that if they have any warnings, they do not
252+
pollute the output of your build.

0 commit comments

Comments
 (0)