Skip to content

Commit a10a8af

Browse files
committed
Update documentation for adding new lints
- Add instructions for adding new lints with the new automation
1 parent c9bf8e6 commit a10a8af

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

doc/adding_lints.md

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ because that's clearly a non-descriptive name.
99

1010
- [Adding a new lint](#adding-a-new-lint)
1111
- [Setup](#setup)
12+
- [Getting Started](#getting-started)
1213
- [Testing](#testing)
1314
- [Rustfix tests](#rustfix-tests)
1415
- [Edition 2018 tests](#edition-2018-tests)
@@ -31,6 +32,19 @@ which can change rapidly. Make sure you're working near rust-clippy's master,
3132
and use the `setup-toolchain.sh` script to configure the appropriate toolchain
3233
for the Clippy directory.
3334

35+
### Getting Started
36+
37+
There is a bit of boilerplate code that needs to be set up when creating a new
38+
lint. Fortunately, you can use the clippy dev tools to handle this for you. We
39+
are naming our new lint `foo_functions` (lints are generally written in snake
40+
case), and we don't need type information so it will have an early pass type
41+
(more on this later on). To get started on this lint you can run
42+
`./util/dev new_lint --name=foo_functions --pass=early --category=pedantic`
43+
(category will default to nursery if not provided). This command will create
44+
two files: `tests/ui/foo_functions.rs` and `clippy_lints/src/foo_functions.rs`,
45+
as well as run `./util/dev update_lints` to register the new lint. Next, we'll
46+
open up these files and add our lint!
47+
3448
### Testing
3549

3650
Let's write some tests first that we can execute while we iterate on our lint.
@@ -41,11 +55,9 @@ we want to check. The output of Clippy is compared against a `.stderr` file.
4155
Note that you don't have to create this file yourself, we'll get to
4256
generating the `.stderr` files further down.
4357

44-
We start by creating the test file at `tests/ui/foo_functions.rs`. It doesn't
45-
really matter what the file is called, but it's a good convention to name it
46-
after the lint it is testing, so `foo_functions.rs` it is.
58+
We start by opening the test file created at `tests/ui/foo_functions.rs`.
4759

48-
Inside the file we put some examples to get started:
60+
Update the file with some examples to get started:
4961

5062
```rust
5163
#![warn(clippy::foo_functions)]
@@ -90,8 +102,8 @@ Once we are satisfied with the output, we need to run
90102
`tests/ui/update-all-references.sh` to update the `.stderr` file for our lint.
91103
Please note that, we should run `TESTNAME=foo_functions cargo uitest`
92104
every time before running `tests/ui/update-all-references.sh`.
93-
Running `TESTNAME=foo_functions cargo uitest` should pass then. When we
94-
commit our lint, we need to commit the generated `.stderr` files, too.
105+
Running `TESTNAME=foo_functions cargo uitest` should pass then. When we commit
106+
our lint, we need to commit the generated `.stderr` files, too.
95107

96108
### Rustfix tests
97109

@@ -121,21 +133,34 @@ With tests in place, let's have a look at implementing our lint now.
121133

122134
### Lint declaration
123135

124-
We start by creating a new file in the `clippy_lints` crate. That's the crate
125-
where all the lint code is. We are going to call the file
126-
`clippy_lints/src/foo_functions.rs` and import some initial things we need:
136+
Let's start by opening the new file created in the `clippy_lints` crate
137+
at `clippy_lints/src/foo_functions.rs`. That's the crate where all the
138+
lint code is. This file has already imported some initial things we will need:
127139

128140
```rust
129-
use rustc::lint::{LintArray, LintPass, EarlyLintPass};
141+
use rustc::lint::{LintArray, LintPass, EarlyLintPass, EarlyContext};
130142
use rustc::declare_lint_pass;
131143
use rustc_session::declare_tool_lint;
144+
use syntax::ast::*;
132145
```
133146

134-
The next step is to provide a lint declaration. Lints are declared using the
135-
[`declare_clippy_lint!`][declare_clippy_lint] macro:
147+
The next step is to update the lint declaration. Lints are declared using the
148+
[`declare_clippy_lint!`][declare_clippy_lint] macro, and we just need to update
149+
the auto-generated lint declaration to have a real description, something like this:
136150

137151
```rust
138152
declare_clippy_lint! {
153+
/// **What it does:**
154+
///
155+
/// **Why is this bad?**
156+
///
157+
/// **Known problems:** None.
158+
///
159+
/// **Example:**
160+
///
161+
/// ```rust
162+
/// // example code
163+
/// ```
139164
pub FOO_FUNCTIONS,
140165
pedantic,
141166
"function named `foo`, which is not a descriptive name"
@@ -151,8 +176,8 @@ state the thing that is being checked for and read well when used with
151176
* The last part should be a text that explains what exactly is wrong with the
152177
code
153178

154-
With our lint declaration done, we will now make sure that it is assigned to a
155-
lint pass:
179+
The rest of this file contains an empty implementation for our lint pass,
180+
which in this case is `EarlyLintPass` and should look like this:
156181

157182
```rust
158183
// clippy_lints/src/foo_functions.rs
@@ -167,12 +192,9 @@ impl EarlyLintPass for FooFunctions {}
167192
Don't worry about the `name` method here. As long as it includes the name of the
168193
lint pass it should be fine.
169194

170-
Next we need to run `util/dev update_lints` to register the lint in various
171-
places, mainly in `clippy_lints/src/lib.rs`.
172-
173-
While `update_lints` automates some things, it doesn't automate everything. We
174-
will have to register our lint pass manually in the `register_plugins` function
175-
in `clippy_lints/src/lib.rs`:
195+
The new lint automation runs `update_lints`, which automates some things, but it
196+
doesn't automate everything. We will have to register our lint pass manually in
197+
the `register_plugins` function in `clippy_lints/src/lib.rs`:
176198

177199
```rust
178200
reg.register_early_lint_pass(box foo_functions::FooFunctions);
@@ -196,14 +218,9 @@ In short, the `LateLintPass` has access to type information while the
196218
`EarlyLintPass`. The `EarlyLintPass` is also faster. However linting speed
197219
hasn't really been a concern with Clippy so far.
198220

199-
Since we don't need type information for checking the function name, we are
200-
going to use the `EarlyLintPass`. It has to be imported as well, changing our
201-
imports to:
202-
203-
```rust
204-
use rustc::lint::{LintArray, LintPass, EarlyLintPass, EarlyContext};
205-
use rustc::{declare_tool_lint, lint_array};
206-
```
221+
Since we don't need type information for checking the function name, we used
222+
`--pass=early` when running the new lint automation and all the imports were
223+
added accordingly.
207224

208225
### Emitting a lint
209226

0 commit comments

Comments
 (0)