@@ -116,7 +116,7 @@ where all the lint code is. We are going to call the file
116
116
117
117
``` rust
118
118
use rustc :: lint :: {LintArray , LintPass , EarlyLintPass };
119
- use rustc :: {declare_tool_lint, lint_array };
119
+ use rustc :: {declare_lint_pass, declare_tool_lint };
120
120
```
121
121
122
122
The next step is to provide a lint declaration. Lints are declared using the
@@ -147,22 +147,9 @@ lint pass:
147
147
148
148
// .. imports and lint declaration ..
149
149
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 ]);
164
151
165
- impl EarlyLintPass for FooFunctionsPass {}
152
+ impl EarlyLintPass for FooFunctions {}
166
153
```
167
154
168
155
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
176
163
in ` clippy_lints/src/lib.rs ` :
177
164
178
165
``` rust
179
- reg . register_early_lint_pass (box foo_functions :: FooFunctionsPass );
166
+ reg . register_early_lint_pass (box foo_functions :: FooFunctions );
180
167
```
181
168
182
169
This should fix the ` unknown clippy lint: clippy::foo_functions ` error that we
@@ -211,10 +198,10 @@ use rustc::{declare_tool_lint, lint_array};
211
198
With UI tests and the lint declaration in place, we can start working on the
212
199
implementation of the lint logic.
213
200
214
- Let's start by implementing the ` EarlyLintPass ` for our ` FooFunctionsPass ` :
201
+ Let's start by implementing the ` EarlyLintPass ` for our ` FooFunctions ` :
215
202
216
203
``` rust
217
- impl EarlyLintPass for FooFunctionsPass {
204
+ impl EarlyLintPass for FooFunctions {
218
205
fn check_fn (& mut self , cx : & EarlyContext <'_ >, fn_kind : FnKind <'_ >, _ : & FnDecl , span : Span , _ : NodeId ) {
219
206
// TODO: Emit lint here
220
207
}
@@ -236,7 +223,7 @@ provide an extra help message and we can't really suggest a better name
236
223
automatically. This is how it looks:
237
224
238
225
``` rust
239
- impl EarlyLintPass for Pass {
226
+ impl EarlyLintPass for FooFunctions {
240
227
fn check_fn (& mut self , cx : & EarlyContext <'_ >, _ : FnKind <'_ >, _ : & FnDecl , span : Span , _ : NodeId ) {
241
228
span_help_and_lint (
242
229
cx ,
@@ -263,7 +250,7 @@ Both provide access to the name of the function/method via an [`Ident`][ident].
263
250
With that we can expand our ` check_fn ` method to:
264
251
265
252
``` rust
266
- impl EarlyLintPass for Pass {
253
+ impl EarlyLintPass for FooFunctions {
267
254
fn check_fn (& mut self , cx : & EarlyContext <'_ >, fn_kind : FnKind <'_ >, _ : & FnDecl , span : Span , _ : NodeId ) {
268
255
if is_foo_fn (fn_kind ) {
269
256
span_help_and_lint (
@@ -304,16 +291,6 @@ running `cargo test` should produce the expected output. Remember to run
304
291
` cargo test ` (as opposed to ` cargo uitest ` ) will also ensure that our lint
305
292
implementation is not violating any Clippy lints itself.
306
293
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
-
317
294
That should be it for the lint implementation. Running ` cargo test ` should now
318
295
pass.
319
296
0 commit comments