You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/src/development/defining_lints.md
+50-44Lines changed: 50 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
3
3
The first step in the journey of a new lint is the definition
4
4
and registration of the lint in Clippy's codebase.
5
-
We can use the Clippy dev tools to handle this step since setting up the
5
+
We can use the Clippy dev tools to handle this step since setting up the
6
6
lint involves some boilerplate code.
7
7
8
8
#### Lint types
9
9
10
10
A lint type is the category of items and expressions in which your lint focuses on.
11
11
12
-
As of the writing of this documentation update, there are 12 groups (a.k.a. _types_)
13
-
of lints besides the numerous standalone lints living under `clippy_lints/src/`:
12
+
As of the writing of this documentation update, there are 12 _types_ of lints
13
+
besides the numerous standalone lints living under `clippy_lints/src/`:
14
14
15
15
-`cargo`
16
16
-`casts`
@@ -25,47 +25,52 @@ of lints besides the numerous standalone lints living under `clippy_lints/src/`:
25
25
-`unit_types`
26
26
-`utils / internal` (Clippy internal lints)
27
27
28
-
These types group together lints that share some common behaviors.
29
-
For instance, `functions` groups together lints
30
-
that deal with some aspects of function calls in Rust.
28
+
These types group together lints that share some common behaviors. For instance,
29
+
`functions` groups together lints that deal with some aspects of functions in
30
+
Rust, like definitions, signatures and attributes.
31
31
32
32
For more information, feel free to compare the lint files under any category
33
-
with [All Clippy lints][all_lints] or
34
-
ask one of the maintainers.
33
+
with [All Clippy lints][all_lints] or ask one of the maintainers.
35
34
36
35
## Lint name
37
36
38
-
A good lint name is important, make sure to check the [lint naming guidelines][lint_naming]. Don't worry, if
39
-
the lint name doesn't fit, a Clippy team member will alert you in the PR process.
37
+
A good lint name is important, make sure to check the [lint naming
38
+
guidelines][lint_naming]. Don't worry, if the lint name doesn't fit, a Clippy
39
+
team member will alert you in the PR process.
40
40
41
41
---
42
42
43
-
We'll name our example lint that detects functions named "foo" `foo_functions`. Check the
44
-
[lint naming guidelines][lint_naming] to see why this name makes sense.
43
+
We'll name our example lint that detects functions named "foo" `foo_functions`.
44
+
Check the [lint naming guidelines][lint_naming] to see why this name makes
45
+
sense.
45
46
46
47
## Add and Register the Lint
47
48
48
-
Now that a name is chosen, we shall register `foo_functions` as a lint to the codebase.
49
-
There are two ways to register a lint.
49
+
Now that a name is chosen, we shall register `foo_functions` as a lint to the
50
+
codebase. There are two ways to register a lint.
50
51
51
52
### Standalone
52
53
53
-
If you believe that this new lint is a standalone lint (that doesn't belong to any specific [type](#lint-types) like `functions` or `loops`), you can run the following
54
-
command in your Clippy project:
54
+
If you believe that this new lint is a standalone lint (that doesn't belong to
55
+
any specific [type](#lint-types) like `functions` or `loops`), you can run the
56
+
following command in your Clippy project:
55
57
56
58
```sh
57
59
$ cargo dev new_lint --name=lint_name --pass=late --category=pedantic
58
60
```
59
61
60
62
There are two things to note here:
61
63
62
-
1. We set `--pass=late` in this command to do a late lint pass. The alternative
63
-
is an `early` lint pass. We will discuss this difference in a later chapter.
64
-
<!-- FIXME: Link that "later chapter" when lint_passes.md is merged -->
65
-
2. If not provided, the `category` of this new lint will default to `nursery`.
64
+
1.`--pass`: We set `--pass=late` in this command to do a late lint pass. The
65
+
alternative is an `early` lint pass. We will discuss this difference in a
66
+
later chapter.
67
+
<!-- FIXME: Link that "later chapter" when lint_passes.md is merged -->
68
+
2.`--category`: If not provided, the `category` of this new lint will default
69
+
to `nursery`.
66
70
67
-
The `cargo dev new_lint` command will create a new file: `clippy_lints/src/foo_functions.rs`
68
-
as well as [register the lint](#lint-registration).
71
+
The `cargo dev new_lint` command will create a new file:
72
+
`clippy_lints/src/foo_functions.rs` as well as [register the
73
+
lint](#lint-registration).
69
74
70
75
Overall, you should notice that the following files are modified or created:
71
76
@@ -115,7 +120,6 @@ call your lint from within the type's lint pass, found in `clippy_lints/src/{typ
115
120
A _type_ is just the name of a directory in `clippy_lints/src`, like `functions` in
116
121
the example command. Clippy groups together some lints that share common behaviors,
117
122
so if your lint falls into one, it would be best to add it to that type.
118
-
Read more about [lint types](#lint-types) below.
119
123
120
124
Overall, you should notice that the following files are modified or created:
121
125
@@ -138,24 +142,25 @@ Untracked files:
138
142
139
143
## The `define_clippy_lints` macro
140
144
141
-
After `cargo dev new_lint`, you should see a macro with the name `define_clippy_lints`. It will be in the same file
142
-
if you defined a standalone lint, and it will be in `mod.rs` if you defined a type-specific lint.
145
+
After `cargo dev new_lint`, you should see a macro with the name
146
+
`define_clippy_lints`. It will be in the same file if you defined a standalone
147
+
lint, and it will be in `mod.rs` if you defined a type-specific lint.
143
148
144
149
The macro looks something like this:
145
150
146
151
```rust
147
152
declare_clippy_lint! {
148
-
/// ### What it does
153
+
/// ### What it does
154
+
///
155
+
/// // Describe here what does the lint do.
156
+
///
157
+
/// Triggers when detects...
149
158
///
150
-
/// // Describe here what does the lint do.
151
-
///
152
-
/// Triggers when detects...
153
-
///
154
159
/// ### Why is this bad?
155
-
///
156
-
/// // Describe why this pattern would be bad
157
-
///
158
-
/// It can lead to...
160
+
///
161
+
/// // Describe why this pattern would be bad
162
+
///
163
+
/// It can lead to...
159
164
///
160
165
/// ### Example
161
166
/// ```rust
@@ -174,25 +179,26 @@ declare_clippy_lint! {
174
179
175
180
## Lint registration
176
181
177
-
If we run the `cargo dev new_lint` command for a new lint,
178
-
the lint will be automatically registered and there is nothing more to do.
182
+
If we run the `cargo dev new_lint` command for a new lint, the lint will be
183
+
automatically registered and there is nothing more to do.
179
184
180
-
However, sometimes we might want to declare a new lint by hand.
181
-
In this case, we'd use `cargo dev update_lints` command afterwards.
185
+
However, sometimes we might want to declare a new lint by hand. In this case,
186
+
we'd use `cargo dev update_lints` command afterwards.
182
187
183
188
When a lint is manually declared, we might need to register the lint pass
184
189
manually in the `register_plugins` function in `clippy_lints/src/lib.rs`:
0 commit comments