3 You are probably here because you want to add a new lint to Clippy. If this is
4 the first time you're contributing to Clippy, this document guides you through
5 creating an example lint from scratch.
7 To get started, we will create a lint that detects functions called `foo`,
8 because that's clearly a non-descriptive name.
11 * [Lint declaration](#Lint-declaration)
12 * [Lint passes](#Lint-passes)
13 * [Emitting a lint](#Emitting-a-lint)
14 * [Adding the lint logic](#Adding-the-lint-logic)
15 * [Documentation](#Documentation)
16 * [Debugging](#Debugging)
17 * [PR Checklist](#PR-Checklist)
18 * [Cheatsheet](#Cheatsheet)
22 Let's write some tests first that we can execute while we iterate on our lint.
24 Clippy uses UI tests for testing. UI tests check that the output of Clippy is
25 exactly as expected. Each test is just a plain Rust file that contains the code
26 we want to check. The output of Clippy is compared against a `.stderr` file.
28 Let's create the test file at `tests/ui/foo_functions.rs`. It doesn't really
29 matter what the file is called, but it's a good convention to name it after the
30 lint it is testing, so `foo_functions.rs` it is.
32 Inside we put some examples to get started:
35 #![warn(clippy::foo_functions)]
45 // Default trait methods
58 // We also don't want to lint method calls
66 Now we can run the test with `TESTNAME=ui/foo_functions cargo uitest`.
67 Currently this test will fail. If you go through the output you will see that we
68 have to add some missing imports to our lint file.
70 While you are working on implementing your lint, you can keep running the UI
71 test. That allows you to check if the output is turning into what you want.
73 Once you are satisfied with the output, you need to run
74 `tests/ui/update-all-references.sh` to update the `stderr` file for your lint.
75 Running `TESTNAME=ui/foo_functions cargo uitest` should pass then. When you
76 commit your lint, be sure to commit the `*.stderr` files, too.
78 Let's have a look at implementing our lint now.
82 We start by creating a new file in the `clippy_lints` crate. That's the crate
83 where all the lint code is. We are going to call the file
84 `clippy_lints/src/foo_functions.rs` and import some initial things we need:
87 use rustc::lint::{LintArray, LintPass};
88 use rustc::{declare_tool_lint, lint_array};
91 The next step is to provide a lint declaration. Lints are declared using the [`declare_clippy_lint!`][declare_clippy_lint] macro:
94 declare_clippy_lint! {
97 "function named `foo`, which is not a descriptive name"
101 * `FOO_FUNCTIONS` is the name of our lint. Be sure to follow the [lint naming
102 guidelines][lint_naming] here when naming your lint. In short, the name should
103 state the thing that is being checked for and read well when used with
104 `allow`/`warn`/`deny`.
105 * `pedantic` sets the lint level to `Allow`.
106 The exact mapping can be found [here][category_level_mapping]
107 * The last part should be a text that explains what exactly is wrong with the
110 With our lint declaration done, we will now make sure that our lint declaration is assigned to a lint pass:
113 // clippy_lints/src/foo_functions.rs
115 // .. imports and lint declaration ..
117 #[derive(Copy, Clone)]
118 pub struct FooFunctionsPass;
120 impl LintPass for FooFunctionsPass {
121 fn get_lints(&self) -> LintArray {
127 fn name(&self) -> &'static str {
133 Don't worry about the `name` method here. As long as it includes the name of the lint pass it should be fine.
135 Next you should run `util/dev update_lints` to register the lint in various
136 places, mainly in `clippy_lints/src/lib.rs`.
138 While `update_lints` automates some things, it doesn't automate everything. We will have to register our lint pass manually in the `register_plugins` function in `clippy_lints/src/lib.rs`:
141 reg.register_early_lint_pass(box foo_functions::FooFunctionsPass);
144 Without that, running the UI tests would produce an error like `unknown clippy lint: clippy::foo_functions`.
145 The next decision we have to make is which lint pass our lint is going to need.
149 Writing a lint that just checks for the name of a function means that we just
150 have to deal with the AST and don't have to deal with the type system at all.
151 This is good, because it makes writing this particular lint less complicated.
153 We have to make this decision with every new Clippy lint. It boils down to using either `EarlyLintPass` or `LateLintPass`. This is a result of Rust's compilation process. You can read more about it [in the rustc guide][compilation_stages].
155 In short, the `LateLintPass` has access to type information while the `EarlyLintPass` doesn't. If you don't need access to type information, use the `EarlyLintPass`. The `EarlyLintPass` is also faster. However linting speed hasn't really been a concern with Clippy so far.
157 Since we don't need type information for checking the function name, we are going to use the `EarlyLintPass`. It has to be imported as well, changing our imports to:
160 use rustc::lint::{LintArray, LintPass, EarlyLintPass, EarlyContext};
161 use rustc::{declare_tool_lint, lint_array};
166 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.
168 Let's start by emitting a lint for every function definition.
171 impl EarlyLintPass for Pass {
172 fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
173 // TODO: Emit lint here
178 We implement the [`check_fn`][check_fn] method from the [`EarlyLintPass`][early_lint_pass] trait. This gives us access to various information about the function that is currently being checked. More on that in the next section. Let's worry about the details later and emit our lint for *every* function definition first.
180 Depending on how complex we want our lint message to be, we can choose from a variety of lint emission functions.
181 They can all be found in [`clippy_lints/src/utils/diagnostics.rs`][diagnostics].
185 impl EarlyLintPass for Pass {
186 fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
191 "function named `foo`",
192 "consider using a more meaningful name"
198 ### Adding the lint logic
200 Writing the logic for your lint will most likely be different from this example,
201 so this section is kept rather short.
203 Using the [`check_fn`][check_fn] method gives us access to [`FnKind`][fn_kind]
204 that has two relevant variants for us `FnKind::ItemFn` and `FnKind::Method`.
205 Both provide access to the name of the function/method via an [`Ident`][ident].
206 and delegate our check to it's own function, passing through only the data we
209 In our example, the implementation would look like:
212 fn is_foo_fn(fn_kind: FnKind<'_>) -> bool {
214 FnKind::ItemFn(ident, ..) | FnKind::Method(ident, ..) => {
215 return ident.name == "foo"
217 FnKind::Closure(..) => false
223 Now you'll want to also run the full test suite with `cargo test`. Apart from
224 running all other UI tests, this ensures that our lint implementation is not
225 violating any Clippy lints itself. If you are still following the example,
226 you'll see that the `FooFunctionsPass` violates a Clippy lint. So we are going
227 to rename that struct to just `Pass`:
230 #[derive(Copy, Clone)]
233 impl LintPass for Pass { /* .. */ }
239 The final thing before submitting our PR is to add some documentation to our
242 Please document your lint with a doc comment akin to the following:
245 /// **What it does:** Checks for ... (describe what the lint matches).
247 /// **Why is this bad?** Supply the reason for linting the code.
249 /// **Known problems:** None. (Or describe where it could go wrong.)
255 /// Insert a short example of code that triggers the lint
258 /// Insert a short example of improved code that doesnt trigger the lint
260 declare_clippy_lint! // ...
263 Once your lint is merged, this documentation will show up in the [lint
268 If you want to debug parts of your lint implementation, you can use the `dbg!`
269 macro anywhere in your code. Running the tests should then include the debug
270 output in the `stdout` part.
276 - [ ] Followed [lint naming conventions][lint_naming]
277 - [ ] Added passing UI tests (including committed `.stderr` file)
278 - [ ] `cargo test` passes locally
279 - [ ] Added lint documentation
283 Here are some pointers to things you are likely going to need for every lint:
285 * [The `if_chain` macro][if_chain]
286 * [`in_macro`][in_macro] and [`in_external_macro`][in_external_macro]
288 * [Clippy diagnostics][diagnostics]
289 * [`Applicability`][applicability]
291 For `EarlyLintPass` lints:
293 * [`EarlyLintPass`][early_lint_pass]
294 * [`syntax::ast`][ast]
296 For `LateLintPass` lints:
298 * [`LateLintPass`][late_lint_pass]
302 While most of Clippy's lint utils are documented, most of rustc's internals lack
303 documentation currently. This is unfortunate, but in most cases you can probably
304 get away with copying things from existing similar lints. If you are stuck,
305 don't hesitate to ask on Discord, IRC or in the issue/PR.
307 [lint_list]: https://rust-lang.github.io/rust-clippy/master/index.html
308 [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints
309 [category_level_mapping]: https://github.com/rust-lang/rust-clippy/blob/bd23cb89ec0ea63403a17d3fc5e50c88e38dd54f/clippy_lints/src/lib.rs#L43
310 [declare_clippy_lint]: https://github.com/rust-lang/rust-clippy/blob/a71acac1da7eaf667ab90a1d65d10e5cc4b80191/clippy_lints/src/lib.rs#L39
311 [compilation_stages]: https://rust-lang.github.io/rustc-guide/high-level-overview.html#the-main-stages-of-compilation
312 [check_fn]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.EarlyLintPass.html#method.check_fn
313 [early_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.EarlyLintPass.html
314 [late_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.LateLintPass.html
315 [fn_kind]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/visit/enum.FnKind.html
316 [diagnostics]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/utils/diagnostics.rs
317 [ident]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/symbol/struct.Ident.html
318 [span]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax_pos/struct.Span.html
319 [applicability]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/enum.Applicability.html
320 [if_chain]: https://docs.rs/if_chain/0.1.2/if_chain/
321 [ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/sty/index.html
322 [ast]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/index.html
323 [in_macro]: https://github.com/rust-lang/rust-clippy/blob/d0717d1f9531a03d154aaeb0cad94c243915a146/clippy_lints/src/utils/mod.rs#L94
324 [in_external_macro]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/fn.in_external_macro.html