Skip to content

Commit 21f8ee3

Browse files
committed
Author lint usage via playground
1 parent d9b1737 commit 21f8ee3

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

doc/adding_lints.md

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ because that's clearly a non-descriptive name.
1414
* [Lint passes](#Lint-passes)
1515
* [Emitting a lint](#Emitting-a-lint)
1616
* [Adding the lint logic](#Adding-the-lint-logic)
17+
* [Author lint](#Author-lint)
1718
* [Documentation](#Documentation)
1819
* [Running rustfmt](#Running-rustfmt)
1920
* [Debugging](#Debugging)
@@ -165,7 +166,7 @@ lint pass it should be fine.
165166
Next you should run `util/dev update_lints` to register the lint in various
166167
places, mainly in `clippy_lints/src/lib.rs`.
167168

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
169170
will have to register our lint pass manually in the `register_plugins` function
170171
in `clippy_lints/src/lib.rs`:
171172

@@ -179,7 +180,7 @@ pass our lint is going to need.
179180

180181
### Lint passes
181182

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
183184
have to deal with the AST and don't have to deal with the type system at all.
184185
This is good, because it makes writing this particular lint less complicated.
185186

@@ -204,7 +205,9 @@ use rustc::{declare_tool_lint, lint_array};
204205

205206
### Emitting a lint
206207

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.
208211

209212
Let's start by implementing the `EarlyLintPass` for our `FooFunctionsPass`:
210213

@@ -270,7 +273,7 @@ impl EarlyLintPass for Pass {
270273

271274
We separate the lint conditional from the lint emissions because it makes the
272275
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.
274277

275278
In our example, `is_foo_fn` looks like:
276279

@@ -294,7 +297,7 @@ running `cargo test` should produce the expected output. Remember to run
294297
`cargo test` (as opposed to `cargo uitest`) will also ensure that our lint
295298
implementation is not violating any Clippy lints itself.
296299

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`
298301
violates a Clippy lint. So we are going to rename that struct to just `Pass`:
299302

300303
```rust
@@ -313,33 +316,12 @@ If you have trouble implementing your lint, there is also the internal `author`
313316
lint to generate Clippy code that detects the offending pattern. It does not
314317
work for all of the Rust syntax, but can give a good starting point.
315318

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.
318323

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.
343325

344326
If the command was executed successfully, you can copy the code over to where
345327
you are implementing your lint.
@@ -448,3 +430,5 @@ don't hesitate to ask on Discord, IRC or in the issue/PR.
448430
[ast]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/index.html
449431
[in_macro]: https://github.com/rust-lang/rust-clippy/blob/d0717d1f9531a03d154aaeb0cad94c243915a146/clippy_lints/src/utils/mod.rs#L94
450432
[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

Comments
 (0)