@@ -14,6 +14,7 @@ because that's clearly a non-descriptive name.
14
14
* [ Lint passes] ( #Lint-passes )
15
15
* [ Emitting a lint] ( #Emitting-a-lint )
16
16
* [ Adding the lint logic] ( #Adding-the-lint-logic )
17
+ * [ Author lint] ( #Author-lint )
17
18
* [ Documentation] ( #Documentation )
18
19
* [ Running rustfmt] ( #Running-rustfmt )
19
20
* [ Debugging] ( #Debugging )
@@ -165,7 +166,7 @@ lint pass it should be fine.
165
166
Next you should run ` util/dev update_lints ` to register the lint in various
166
167
places, mainly in ` clippy_lints/src/lib.rs ` .
167
168
168
- While ` update_lints ` automates some things, it doesn't automate everything. We
169
+ While ` update_lints ` automates some things, it doesn't automate everything. You
169
170
will have to register our lint pass manually in the ` register_plugins ` function
170
171
in ` clippy_lints/src/lib.rs ` :
171
172
@@ -179,7 +180,7 @@ pass our lint is going to need.
179
180
180
181
### Lint passes
181
182
182
- Writing a lint that just checks for the name of a function means that we just
183
+ Writing a lint that only checks for the name of a function means that we only
183
184
have to deal with the AST and don't have to deal with the type system at all.
184
185
This is good, because it makes writing this particular lint less complicated.
185
186
@@ -204,7 +205,9 @@ use rustc::{declare_tool_lint, lint_array};
204
205
205
206
### Emitting a lint
206
207
207
- With UI tests in place, we can start working on the implementation of the lint logic. We can keep executing the tests until we make them pass.
208
+ With UI tests and the lint declaration in place, we can start working on the
209
+ implementation of the lint logic. We can keep executing the tests until we make
210
+ them pass.
208
211
209
212
Let's start by implementing the ` EarlyLintPass ` for our ` FooFunctionsPass ` :
210
213
@@ -270,7 +273,7 @@ impl EarlyLintPass for Pass {
270
273
271
274
We separate the lint conditional from the lint emissions because it makes the
272
275
code a bit easier to read. In some cases this separation would also allow to
273
- write some unit tests (as opposed to UI tests) for the separate function.
276
+ write some unit tests (as opposed to only UI tests) for the separate function.
274
277
275
278
In our example, ` is_foo_fn ` looks like:
276
279
@@ -294,7 +297,7 @@ running `cargo test` should produce the expected output. Remember to run
294
297
` cargo test ` (as opposed to ` cargo uitest ` ) will also ensure that our lint
295
298
implementation is not violating any Clippy lints itself.
296
299
297
- If you are still following the example, you'll see that the ` FooFunctionsPass `
300
+ If you are still following the example, you will see that ` FooFunctionsPass `
298
301
violates a Clippy lint. So we are going to rename that struct to just ` Pass ` :
299
302
300
303
``` rust
@@ -313,33 +316,12 @@ If you have trouble implementing your lint, there is also the internal `author`
313
316
lint to generate Clippy code that detects the offending pattern. It does not
314
317
work for all of the Rust syntax, but can give a good starting point.
315
318
316
- First, create a new UI test file in the ` tests/ui/ ` directory with the pattern
317
- you want to match:
319
+ The quickest way to use it, is the [ Rust playground] [ play ] .rust-lang.org).
320
+ Put the code you want to lint into the editor and add the ` #[clippy::author] `
321
+ attribute above the item. Then run Clippy via ` Tools -> Clippy ` and you should
322
+ see the generated code in the output below.
318
323
319
- ``` rust
320
- // ./tests/ui/my_lint.rs
321
- fn main () {
322
- #[clippy:: author]
323
- let arr : [i32 ; 1 ] = [7 ]; // Replace line with the code you want to match
324
- }
325
- ```
326
-
327
- Now you run ` TESTNAME=ui/my_lint cargo uitest ` to produce a ` .stdout ` file with
328
- the generated code:
329
-
330
- ``` rust
331
- // ./tests/ui/my_lint.stdout
332
-
333
- if_chain! {
334
- if let ExprKind :: Array (ref elements ) = stmt . node;
335
- if elements . len () == 1 ;
336
- if let ExprKind :: Lit (ref lit ) = elements [0 ]. node;
337
- if let LitKind :: Int (7 , _ ) = lit . node;
338
- then {
339
- // report your lint here
340
- }
341
- }
342
- ```
324
+ [ Here] [ author_example ] is an example on the playground.
343
325
344
326
If the command was executed successfully, you can copy the code over to where
345
327
you are implementing your lint.
@@ -448,3 +430,5 @@ don't hesitate to ask on Discord, IRC or in the issue/PR.
448
430
[ ast ] : https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/index.html
449
431
[ in_macro ] : https://github.com/rust-lang/rust-clippy/blob/d0717d1f9531a03d154aaeb0cad94c243915a146/clippy_lints/src/utils/mod.rs#L94
450
432
[ in_external_macro ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/fn.in_external_macro.html
433
+ [ play ] : https://play.rust-lang.org
434
+ [ author_example ] : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f093b986e80ad62f3b67a1f24f5e66e2
0 commit comments