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-lints warn`
104 will make lints set to 'forbid' just warn.
106 ## Configuring warning levels
108 Remember our `missing_docs` example from the 'allow' lint level?
113 $ rustc lib.rs --crate-type=lib
117 We can configure this lint to operate at a higher level, both with
118 compiler flags, as well as with an attribute in the source code.
120 You can also "cap" lints so that the compiler can choose to ignore
121 certain lint levels. We'll talk about that last.
123 ### Via compiler flag
125 The `-A`, `-W`, `--force-warn` `-D`, and `-F` flags let you turn one or more lints
126 into allowed, warning, force-warn, deny, or forbid levels, like this:
129 $ rustc lib.rs --crate-type=lib -W missing-docs
130 warning: missing documentation for crate
136 = note: requested on the command line with `-W missing-docs`
138 warning: missing documentation for a function
146 $ rustc lib.rs --crate-type=lib -D missing-docs
147 error: missing documentation for crate
153 = note: requested on the command line with `-D missing-docs`
155 error: missing documentation for a function
161 error: aborting due to 2 previous errors
164 You can also pass each flag more than once for changing multiple lints:
167 $ rustc lib.rs --crate-type=lib -D missing-docs -D unused-variables
170 And of course, you can mix these five flags together:
173 $ rustc lib.rs --crate-type=lib -D missing-docs -A unused-variables
176 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:
179 $ rustc lib.rs --crate-type=lib -D unused-variables -A unused-variables
182 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):
185 $ rustc lib.rs --crate-type=lib -D unused -A unused-variables
188 Since `force-warn` and `forbid` cannot be overridden, setting
189 one of them will prevent any later level for the same lint from
194 You can also modify the lint level with a crate-wide attribute:
198 #![warn(missing_docs)]
201 $ rustc lib.rs --crate-type=lib
202 warning: missing documentation for crate
205 1 | / #![warn(missing_docs)]
207 3 | | pub fn foo() {}
210 note: lint level defined here
213 1 | #![warn(missing_docs)]
216 warning: missing documentation for a function
223 `warn`, `allow`, `deny`, and `forbid` all work this way. There is
224 no way to set a lint to `force-warn` using an attribute.
226 You can also pass in multiple lints per attribute:
229 #![warn(missing_docs, unused_variables)]
234 And use multiple attributes together:
237 #![warn(missing_docs)]
238 #![deny(unused_variables)]
245 `rustc` supports a flag, `--cap-lints LEVEL` that sets the "lint cap level."
246 This is the maximum level for all lints. So for example, if we take our
247 code sample from the "deny" lint level above:
255 And we compile it, capping lints to warn:
258 $ rustc lib.rs --cap-lints warn
259 warning: bitshift exceeds the type's number of bits
265 = note: `#[warn(exceeding_bitshifts)]` on by default
267 warning: this expression will panic at run-time
271 | ^^^^^^^^^^^ attempt to shift left with overflow
274 It now only warns, rather than errors. We can go further and allow all lints:
277 $ rustc lib.rs --cap-lints allow
281 This feature is used heavily by Cargo; it will pass `--cap-lints allow` when
282 compiling your dependencies, so that if they have any warnings, they do not
283 pollute the output of your build.