3 In `rustc`, lints are divided into five *levels*:
11 Each lint has a default level (explained in the lint listing later in this
12 chapter), and the compiler has a default warning level. First, let's explain
13 what these levels mean, and then we'll talk about configuration.
17 These lints exist, but by default, do nothing. For example, consider this
24 Compiling this file produces no warnings:
27 $ rustc lib.rs --crate-type=lib
31 But this code violates the `missing_docs` lint.
33 These lints exist mostly to be manually turned on via configuration, as we'll
34 talk about later in this section.
38 The 'warn' lint level will produce a warning if you violate the lint. For example,
39 this code runs afoul of the `unused_variables` lint:
47 This will produce this warning:
50 $ rustc lib.rs --crate-type=lib
51 warning: unused variable: `x`
57 = note: `#[warn(unused_variables)]` on by default
58 = note: to avoid this warning, consider using `_x` instead
63 'force-warn' is a special lint level. It's the same as 'warn' in that a lint
64 at this level will produce a warning, but unlike the 'warn' level, the
65 'force-warn' level cannot be overridden. If a lint is set to 'force-warn', it
66 is guaranteed to warn: no more, no less. This is true even if the overall lint
67 level is capped via cap-lints.
71 A 'deny' lint produces an error if you violate it. For example, this code
72 runs into the `exceeding_bitshifts` lint.
82 error: bitshift exceeds the type's number of bits
88 = note: `#[deny(exceeding_bitshifts)]` on by default
91 What's the difference between an error from a lint and a regular old error?
92 Lints are configurable via levels, so in a similar way to 'allow' lints,
93 warnings that are 'deny' by default let you allow them. Similarly, you may
94 wish to set up a lint that is `warn` by default to produce an error instead.
95 This lint level gives you that.
99 'forbid' is a special lint level that fills the same role for 'deny' that
100 'force-warn' does for 'warn'. It's the same as 'deny' in that a lint at this
101 level will produce an error, but unlike the 'deny' level, the 'forbid' level
102 can not be overridden to be anything lower than an error. However, lint
103 levels may still be capped with `--cap-lints` (see below) so `rustc --cap-
104 lints warn` will make lints set to 'forbid' just
107 ## Configuring warning levels
109 Remember our `missing_docs` example from the 'allow' lint level?
114 $ rustc lib.rs --crate-type=lib
118 We can configure this lint to operate at a higher level, both with
119 compiler flags, as well as with an attribute in the source code.
121 You can also "cap" lints so that the compiler can choose to ignore
122 certain lint levels. We'll talk about that last.
124 ### Via compiler flag
126 The `-A`, `-W`, `--force-warn` `-D`, and `-F` flags let you turn one or more lints
127 into allowed, warning, force-warn, deny, or forbid levels, like this:
130 $ rustc lib.rs --crate-type=lib -W missing-docs
131 warning: missing documentation for crate
137 = note: requested on the command line with `-W missing-docs`
139 warning: missing documentation for a function
147 $ rustc lib.rs --crate-type=lib -D missing-docs
148 error: missing documentation for crate
154 = note: requested on the command line with `-D missing-docs`
156 error: missing documentation for a function
162 error: aborting due to 2 previous errors
165 You can also pass each flag more than once for changing multiple lints:
168 $ rustc lib.rs --crate-type=lib -D missing-docs -D unused-variables
171 And of course, you can mix these five flags together:
174 $ rustc lib.rs --crate-type=lib -D missing-docs -A unused-variables
177 The order of these command line arguments is taken into account. The following allows the `unused-variables` lint, because it is the last argument for that lint:
180 $ rustc lib.rs --crate-type=lib -D unused-variables -A unused-variables
183 You can make use of this behavior by overriding the level of one specific lint out of a group of lints. The following example denies all the lints in the `unused` group, but explicitly allows the `unused-variables` lint in that group (forbid still trumps everything regardless of ordering):
186 $ rustc lib.rs --crate-type=lib -D unused -A unused-variables
189 Since `force-warn` and `forbid` cannot be overridden, setting
190 one of them will prevent any later level for the same lint from
195 You can also modify the lint level with a crate-wide attribute:
199 #![warn(missing_docs)]
202 $ rustc lib.rs --crate-type=lib
203 warning: missing documentation for crate
206 1 | / #![warn(missing_docs)]
208 3 | | pub fn foo() {}
211 note: lint level defined here
214 1 | #![warn(missing_docs)]
217 warning: missing documentation for a function
224 `warn`, `allow`, `deny`, and `forbid` all work this way. There is
225 no way to set a lint to `force-warn` using an attribute.
227 You can also pass in multiple lints per attribute:
230 #![warn(missing_docs, unused_variables)]
235 And use multiple attributes together:
238 #![warn(missing_docs)]
239 #![deny(unused_variables)]
246 `rustc` supports a flag, `--cap-lints LEVEL` that sets the "lint cap level."
247 This is the maximum level for all lints. So for example, if we take our
248 code sample from the "deny" lint level above:
256 And we compile it, capping lints to warn:
259 $ rustc lib.rs --cap-lints warn
260 warning: bitshift exceeds the type's number of bits
266 = note: `#[warn(exceeding_bitshifts)]` on by default
268 warning: this expression will panic at run-time
272 | ^^^^^^^^^^^ attempt to shift left with overflow
275 It now only warns, rather than errors. We can go further and allow all lints:
278 $ rustc lib.rs --cap-lints allow
282 This feature is used heavily by Cargo; it will pass `--cap-lints allow` when
283 compiling your dependencies, so that if they have any warnings, they do not
284 pollute the output of your build.