@@ -171,11 +171,17 @@ crate.
171
171
172
172
[ builtin ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/index.html
173
173
174
- Each lint is defined as a ` struct ` that implements the ` LintPass ` ` trait ` . The
175
- trait implementation allows you to check certain syntactic constructs the
176
- linter walks the source code. You can then choose to emit lints in a very
177
- similar way to compile errors. Finally, you register the lint to actually get
178
- it to be run by the compiler by using the ` declare_lint! ` macro.
174
+ Every lint is implemented via a ` struct ` that implements the ` LintPass ` ` trait `
175
+ (you also implement one of the more specific lint pass traits, either
176
+ ` EarlyLintPass ` or ` LateLintPass ` ). The trait implementation allows you to
177
+ check certain syntactic constructs the linter walks the source code. You can
178
+ then choose to emit lints in a very similar way to compile errors.
179
+
180
+ You also declare the metadata of a particular lint via the ` declare_lint! `
181
+ macro. This includes the name, the default level, a short description, and some
182
+ more details.
183
+
184
+ Note that the lint and the lint pass must be registered with the compiler.
179
185
180
186
For example, the following lint checks for uses
181
187
of ` while true { ... } ` and suggests using ` loop { ... } ` instead.
@@ -196,11 +202,15 @@ declare_lint! {
196
202
#[derive(Copy, Clone)]
197
203
pub struct WhileTrue;
198
204
199
- impl LintPass for WhileTrue {
200
- fn get_lints(&self) -> LintArray {
201
- lint_array!(WHILE_TRUE)
202
- }
203
- }
205
+ // This declares a lint pass, providing a list of associated lints. The
206
+ // compiler currently doesn't use the associated lints directly (e.g., to not
207
+ // run the pass or otherwise check that the pass emits the appropriate set of
208
+ // lints). However, it's good to be accurate here as it's possible that we're
209
+ // going to register the lints via the get_lints method on our lint pass (that
210
+ // this macro generates).
211
+ impl_lint_pass!(
212
+ WhileTrue => [WHILE_TRUE],
213
+ );
204
214
205
215
// LateLintPass has lots of methods. We only override the definition of
206
216
// `check_expr` for this lint because that's all we need, but you could
@@ -242,9 +252,24 @@ declare_lint! {
242
252
This makes the ` ANONYMOUS_PARAMETERS ` lint allow-by-default in the 2015 edition
243
253
but warn-by-default in the 2018 edition.
244
254
245
- Lints that represent an incompatibility (i.e. error) in the upcoming edition
246
- should also be registered as ` FutureIncompatibilityLint ` s in
247
- [ ` register_builtins ` ] [ rbuiltins ] function in [ ` rustc_lint::lib ` ] [ builtin ] .
255
+ A future-incompatible lint should be declared with the ` @future_incompatible `
256
+ additional "field":
257
+
258
+ ``` rust,ignore
259
+ declare_lint! {
260
+ pub ANONYMOUS_PARAMETERS,
261
+ Allow,
262
+ "detects anonymous parameters",
263
+ @future_incompatible = FutureIncompatibleInfo {
264
+ reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
265
+ edition: Some(Edition::Edition2018),
266
+ };
267
+ }
268
+ ```
269
+
270
+ If you need a combination of options that's not supported by the
271
+ ` declare_lint! ` macro, you can always define your own static with a type of
272
+ ` &Lint ` but this is currently linted against in the compiler tree.
248
273
249
274
### Lint Groups
250
275
0 commit comments