3 In `rustc`, lints are divided into four *levels*:
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.
16 These lints exist, but by default, do nothing. For example, consider this
23 Compiling this file produces no warnings:
26 $ rustc lib.rs --crate-type=lib
30 But this code violates the `missing_docs` lint.
32 These lints exist mostly to be manually turned on via configuration, as we'll
33 talk about later in this section.
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:
46 This will produce this warning:
49 > rustc lib.rs --crate-type=lib
50 warning: unused variable: `x`
56 = note: #[warn(unused_variables)] on by default
57 = note: to avoid this warning, consider using `_x` instead
62 A 'deny' lint produces an error if you violate it. For example, this code
63 runs into the `exceeding_bitshifts` lint.
73 error: bitshift exceeds the type's number of bits
79 = note: #[deny(exceeding_bitshifts)] on by default
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.
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
95 ## Configuring warning levels
97 Remember our `missing_docs` example from the 'allow' lint level?
102 $ rustc lib.rs --crate-type=lib
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.
109 You can also "cap" lints so that the compiler can choose to ignore
110 certain lint levels. We'll talk about that last.
112 ### Via compiler flag
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:
118 $ rustc lib.rs --crate-type=lib -W missing-docs
119 warning: missing documentation for crate
125 = note: requested on the command line with `-W missing-docs`
127 warning: missing documentation for a function
132 > rustc lib.rs --crate-type=lib -D missing-docs
133 error: missing documentation for crate
139 = note: requested on the command line with `-D missing-docs`
141 error: missing documentation for a function
147 error: aborting due to 2 previous errors
150 You can also pass each flag more than once for changing multiple lints:
153 rustc lib.rs --crate-type=lib -D missing-docs -D unused-variables
156 And of course, you can mix these four flags together:
159 rustc lib.rs --crate-type=lib -D missing-docs -A unused-variables
164 You can also modify the lint level with a crate-wide attribute:
168 #![warn(missing_docs)]
171 $ rustc lib.rs --crate-type=lib
172 warning: missing documentation for crate
175 1 | / #![warn(missing_docs)]
177 3 | | pub fn foo() {}
180 note: lint level defined here
183 1 | #![warn(missing_docs)]
186 warning: missing documentation for a function
193 All four, `warn`, `allow`, `deny`, and `forbid` all work this way.
195 You can also pass in multiple lints per attribute:
198 #![warn(missing_docs, unused_variables)]
203 And use multiple attributes together:
206 #![warn(missing_docs)]
207 #![deny(unused_variables)]
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:
224 And we compile it, capping lints to warn:
227 $ rustc lib.rs --cap-lints warn
228 warning: bitshift exceeds the type's number of bits
234 = note: #[warn(exceeding_bitshifts)] on by default
236 warning: this expression will panic at run-time
240 | ^^^^^^^^^^^ attempt to shift left with overflow
243 It now only warns, rather than errors. We can go further and allow all lints:
246 $ rustc lib.rs --cap-lints allow
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.