Skip to content

Commit 9e63e36

Browse files
committed
Document declare_lint_pass!
1 parent 5237c64 commit 9e63e36

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

doc/adding_lints.md

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ where all the lint code is. We are going to call the file
116116

117117
```rust
118118
use rustc::lint::{LintArray, LintPass, EarlyLintPass};
119-
use rustc::{declare_tool_lint, lint_array};
119+
use rustc::{declare_lint_pass, declare_tool_lint};
120120
```
121121

122122
The next step is to provide a lint declaration. Lints are declared using the
@@ -147,22 +147,9 @@ lint pass:
147147

148148
// .. imports and lint declaration ..
149149

150-
#[derive(Copy, Clone)]
151-
pub struct FooFunctionsPass;
152-
153-
impl LintPass for FooFunctionsPass {
154-
fn get_lints(&self) -> LintArray {
155-
lint_array!(
156-
FOO_FUNCTIONS,
157-
)
158-
}
159-
160-
fn name(&self) -> &'static str {
161-
"FooFunctions"
162-
}
163-
}
150+
declare_lint_pass!(FooFunctions => [FOO_FUNCTIONS]);
164151

165-
impl EarlyLintPass for FooFunctionsPass {}
152+
impl EarlyLintPass for FooFunctions {}
166153
```
167154

168155
Don't worry about the `name` method here. As long as it includes the name of the
@@ -176,7 +163,7 @@ will have to register our lint pass manually in the `register_plugins` function
176163
in `clippy_lints/src/lib.rs`:
177164

178165
```rust
179-
reg.register_early_lint_pass(box foo_functions::FooFunctionsPass);
166+
reg.register_early_lint_pass(box foo_functions::FooFunctions);
180167
```
181168

182169
This should fix the `unknown clippy lint: clippy::foo_functions` error that we
@@ -211,10 +198,10 @@ use rustc::{declare_tool_lint, lint_array};
211198
With UI tests and the lint declaration in place, we can start working on the
212199
implementation of the lint logic.
213200

214-
Let's start by implementing the `EarlyLintPass` for our `FooFunctionsPass`:
201+
Let's start by implementing the `EarlyLintPass` for our `FooFunctions`:
215202

216203
```rust
217-
impl EarlyLintPass for FooFunctionsPass {
204+
impl EarlyLintPass for FooFunctions {
218205
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
219206
// TODO: Emit lint here
220207
}
@@ -236,7 +223,7 @@ provide an extra help message and we can't really suggest a better name
236223
automatically. This is how it looks:
237224

238225
```rust
239-
impl EarlyLintPass for Pass {
226+
impl EarlyLintPass for FooFunctions {
240227
fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
241228
span_help_and_lint(
242229
cx,
@@ -263,7 +250,7 @@ Both provide access to the name of the function/method via an [`Ident`][ident].
263250
With that we can expand our `check_fn` method to:
264251

265252
```rust
266-
impl EarlyLintPass for Pass {
253+
impl EarlyLintPass for FooFunctions {
267254
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
268255
if is_foo_fn(fn_kind) {
269256
span_help_and_lint(
@@ -304,16 +291,6 @@ running `cargo test` should produce the expected output. Remember to run
304291
`cargo test` (as opposed to `cargo uitest`) will also ensure that our lint
305292
implementation is not violating any Clippy lints itself.
306293

307-
If you are still following the example, you will see that `FooFunctionsPass`
308-
violates a Clippy lint. So we are going to rename that struct to just `Pass`:
309-
310-
```rust
311-
#[derive(Copy, Clone)]
312-
pub struct Pass;
313-
314-
impl LintPass for Pass { /* .. */ }
315-
```
316-
317294
That should be it for the lint implementation. Running `cargo test` should now
318295
pass.
319296

0 commit comments

Comments
 (0)