@@ -78,7 +78,7 @@ fn main() {
78
78
79
79
Now we can run the test with ` TESTNAME=ui/foo_functions cargo uitest ` .
80
80
Currently this test will fail. If you go through the output you will see that we
81
- have to add some missing imports to our lint file .
81
+ are told that ` clippy::foo_functions ` is an unknown lint, which is expected .
82
82
83
83
While you are working on implementing your lint, you can keep running the UI
84
84
test. That allows you to check if the output is turning into what you want.
@@ -97,16 +97,16 @@ that test. Rustfix will apply the suggestions from the lint to the code of the
97
97
test file and compare that to the contents of a ` .fixed ` file.
98
98
99
99
Use ` tests/ui/update-all-references.sh ` to automatically generate the
100
- ` .fixed ` file after running ` cargo test ` .
100
+ ` .fixed ` file after running the tests .
101
101
102
102
With tests in place, let's have a look at implementing our lint now.
103
103
104
104
### Testing manually
105
105
106
- Manually testing against an example file is useful if you have added some
107
- ` println! ` s and test suite output becomes unreadable. To try Clippy with your
108
- local modifications, run ` env CLIPPY_TESTS=true cargo run --bin clippy-driver -- -L ./target/debug input.rs `
109
- from the working copy root.
106
+ Manually testing against an example file can be useful if you have added some
107
+ ` println! ` s and the test suite output becomes unreadable. To try Clippy with
108
+ your local modifications, run `env CLIPPY_TESTS=true cargo run --bin
109
+ clippy-driver -- -L ./target/debug input.rs` from the working copy root.
110
110
111
111
### Lint declaration
112
112
@@ -177,9 +177,9 @@ in `clippy_lints/src/lib.rs`:
177
177
reg . register_early_lint_pass (box foo_functions :: FooFunctionsPass );
178
178
```
179
179
180
- Without that, running the UI tests would produce an error like `unknown clippy
181
- lint: clippy::foo_functions`. The next decision we have to make is which lint
182
- pass our lint is going to need.
180
+ This should fix the ` unknown clippy lint: clippy::foo_functions ` error that we
181
+ saw when we executed our tests the first time. The next decision we have to make
182
+ is which lint pass our lint is going to need.
183
183
184
184
### Lint passes
185
185
@@ -189,8 +189,6 @@ This is good, because it makes writing this particular lint less complicated.
189
189
190
190
We have to make this decision with every new Clippy lint. It boils down to using
191
191
either [ ` EarlyLintPass ` ] [ early_lint_pass ] or [ ` LateLintPass ` ] [ late_lint_pass ] .
192
- This is a result of Rust's compilation process. You can read more about it [ in
193
- the rustc guide] [ compilation_stages ] .
194
192
195
193
In short, the ` LateLintPass ` has access to type information while the
196
194
` EarlyLintPass ` doesn't. If you don't need access to type information, use the
@@ -209,8 +207,7 @@ use rustc::{declare_tool_lint, lint_array};
209
207
### Emitting a lint
210
208
211
209
With UI tests and the lint declaration in place, we can start working on the
212
- implementation of the lint logic. We can keep executing the tests until we make
213
- them pass.
210
+ implementation of the lint logic.
214
211
215
212
Let's start by implementing the ` EarlyLintPass ` for our ` FooFunctionsPass ` :
216
213
@@ -229,9 +226,12 @@ the next section. Let's worry about the details later and emit our lint for
229
226
* every* function definition first.
230
227
231
228
Depending on how complex we want our lint message to be, we can choose from a
232
- variety of lint emission functions. They can all be found in
229
+ variety of lint emission functions. They can all be found in
233
230
[ ` clippy_lints/src/utils/diagnostics.rs ` ] [ diagnostics ] .
234
231
232
+ ` span_help_and_lint ` seems most appropriate in this case. It allows us to
233
+ provide an extra help message and we can't really suggest a better name
234
+ automatically. This is how it looks:
235
235
236
236
``` rust
237
237
impl EarlyLintPass for Pass {
@@ -247,9 +247,11 @@ impl EarlyLintPass for Pass {
247
247
}
248
248
```
249
249
250
+ Running our UI test should now produce output that contains the lint message.
251
+
250
252
### Adding the lint logic
251
253
252
- Writing the logic for your lint will most likely be different from this example,
254
+ Writing the logic for our lint will most likely be different from this example,
253
255
so this section is kept rather short.
254
256
255
257
Using the [ ` check_fn ` ] [ check_fn ] method gives us access to [ ` FnKind ` ] [ fn_kind ]
0 commit comments