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/emitting_lints.md
+22-13
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@ Note that we will not go into concrete implementation of a lint logic in this
9
9
chapter. We will go into details in later chapters as well as in two examples
10
10
of real Clippy lints.
11
11
12
-
In this chapter, we'll see an example of an imaginary `LateLintPass` lint named `bar_expressions`, that checks for expresions named `bar`.
12
+
In this chapter, we'll see an example of an imaginary `LateLintPass` lint named `bar_expressions`, that checks for
13
+
expresions named `bar`.
13
14
14
15
To emit a lint with `LateLintPass`, we must implement it for the lint that we have
15
16
declared. Take a look at the [LateLintPass][late_lint_pass] documentation, which
@@ -81,7 +82,10 @@ impl<'tcx> LateLintPass<'tcx> for BarExpressions {
81
82
82
83
## Suggestions: Automatic fixes
83
84
84
-
Some lints know what to change in order to fix the lint. A real world example, the lint [`range_plus`][range_plus_one] lints for ranges where the user wrote `x..y + 1` instead of using an [inclusive range][inclusive_range] (`x..=1`). The fix to this lint would be just changing your `x..y + 1` expression to `x..=y`, **this is where suggestions come in**.
85
+
Some lints know what to change in order to fix the lint. A real world example, the lint
86
+
[`range_plus`][range_plus_one] lints for ranges where the user wrote `x..y + 1` instead of using an
87
+
[inclusive range][inclusive_range] (`x..=1`). The fix to this lint would be just changing your `x..y + 1` expression
88
+
to `x..=y`, **this is where suggestions come in**.
85
89
86
90
A suggestion is a change that the lint provides to fix the issue it is linting.
87
91
The output looks something like this (from the example earlier):
@@ -94,9 +98,12 @@ LL | for _ in 1..1 + 1 {}
94
98
| ^^^^^^^^ help: use: `1..=1`
95
99
```
96
100
97
-
**Not all suggestions are always right**, some of them require human supervision, that's why we have [Applicability][applicability].
101
+
**Not all suggestions are always right**, some of them require human supervision, that's why we have
102
+
[Applicability][applicability].
98
103
99
-
Applicability indicates confidence in the correctness of the suggestion, some are always right (`Applicability::MachineApplicable`), but we use `Applicability::MaybeIncorrect` and other when talking about a lint that may be incorrect.
104
+
Applicability indicates confidence in the correctness of the suggestion, some are always right
105
+
(`Applicability::MachineApplicable`), but we use `Applicability::MaybeIncorrect` and other when talking about a lint
106
+
that may be incorrect.
100
107
101
108
---
102
109
@@ -125,7 +132,8 @@ Suggestions generally use the [`format!`](format_macro) macro to interpolate the
125
132
126
133
## How to choose between notes, help messages and suggestions
127
134
128
-
Notes are presented separately from the main lint message, they provide useful information that the user needs to understand why the lint was activated. They are the most helpful when attached to a span.
135
+
Notes are presented separately from the main lint message, they provide useful information that the user needs to
136
+
understand why the lint was activated. They are the most helpful when attached to a span.
129
137
130
138
Example:
131
139
@@ -146,7 +154,8 @@ note: argument has type &SomeStruct
146
154
147
155
---
148
156
149
-
Help messages are specifically to help the user. These are used in situation where you can't provide a specific machine applicable suggestion. They can also be attached to a span.
157
+
Help messages are specifically to help the user. These are used in situation where you can't provide a specific
158
+
machine applicable suggestion. They can also be attached to a span.
150
159
151
160
Example:
152
161
@@ -162,7 +171,8 @@ error: constant division of 0.0 with 0.0 will always result in NaN
162
171
163
172
---
164
173
165
-
Suggestions are the most helpful, they are direct changes to the source code in order to fix the error. The magic in suggestions is that tools like rustfix can detect them and automatically fix your code.
174
+
Suggestions are the most helpful, they are direct changes to the source code in order to fix the error. The magic
175
+
in suggestions is that tools like rustfix can detect them and automatically fix your code.
166
176
167
177
Example:
168
178
@@ -178,9 +188,11 @@ error: This `.fold` can be more succinctly expressed as `.any`
178
188
179
189
### Snippets
180
190
181
-
Snippets are pieces of the source code (as a string), they are extracted generally using the [`snippet`][snippet_fn] function.
191
+
Snippets are pieces of the source code (as a string), they are extracted generally using the [`snippet`][snippet_fn]
192
+
function.
182
193
183
-
For example, if you want to know how an item looks (and you know the item's span), you could use `snippet(cx, span, "..")`.
194
+
For example, if you want to know how an item looks (and you know the item's span), you could use
195
+
`snippet(cx, span, "..")`.
184
196
185
197
## Final: Run UI Tests to Emit the Lint
186
198
@@ -191,8 +203,6 @@ The next step is to implement the logic properly, which is a detail that we will
0 commit comments