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
Summary
--
This includes the user-facing updates to our linter docs and selection
settings to accompany #27666.
Test Plan
--
A few existing snapshot updates but mostly users reading this
---------
Co-authored-by: Micha Reiser <micha@reiser.io>
Copy file name to clipboardExpand all lines: crates/ruff/tests/config.rs
+5-2Lines changed: 5 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,14 +13,17 @@ fn lint_select() {
13
13
exit_code: 0
14
14
----- stdout -----
15
15
A list of rule codes or prefixes to enable. Prefixes can specify exact
16
-
rules (like `F841`), entire categories (like `F`), or anything in
16
+
rules (like `F841`), entire groups (like `F`), or anything in
17
17
between.
18
18
19
19
When breaking ties between enabled and disabled rules (via `select` and
20
20
`ignore`, respectively), more specific prefixes override less
21
21
specific prefixes. `ignore` takes precedence over `select` if the
22
22
same prefix appears in both.
23
23
24
+
In preview, categories like `correctness` and `suspicious` can be used
25
+
in addition to rule codes and linter group prefixes.
26
+
24
27
Default value: See https://docs.astral.sh/ruff/default-rules/ or run `ruff check --show-settings --isolated`
25
28
Type: list[RuleSelector]
26
29
Example usage:
@@ -42,7 +45,7 @@ fn lint_select_json() {
42
45
exit_code: 0
43
46
----- stdout -----
44
47
{
45
-
"doc": "A list of rule codes or prefixes to enable. Prefixes can specify exact\nrules (like `F841`), entire categories (like `F`), or anything in\nbetween.\n\nWhen breaking ties between enabled and disabled rules (via `select` and\n`ignore`, respectively), more specific prefixes override less\nspecific prefixes. `ignore` takes precedence over `select` if the\nsame prefix appears in both.",
48
+
"doc": "A list of rule codes or prefixes to enable. Prefixes can specify exact\nrules (like `F841`), entire groups (like `F`), or anything in\nbetween.\n\nWhen breaking ties between enabled and disabled rules (via `select` and\n`ignore`, respectively), more specific prefixes override less\nspecific prefixes. `ignore` takes precedence over `select` if the\nsame prefix appears in both.\n\nIn preview, categories like `correctness` and `suspicious` can be used\nin addition to rule codes and linter group prefixes.",
46
49
"default": "See https://docs.astral.sh/ruff/default-rules/ or run `ruff check --show-settings --isolated`",
Copy file name to clipboardExpand all lines: docs/linter.md
+124-1Lines changed: 124 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ If you're wondering how to configure Ruff, here are some **recommended guideline
61
61
62
62
- Prefer [`lint.select`](settings.md#lint_select) over [`lint.extend-select`](settings.md#lint_extend-select) to make your rule set explicit.
63
63
- Use `ALL` with discretion. Enabling `ALL` will implicitly enable new rules whenever you upgrade.
64
-
- Start with a small set of rules (`select = ["E", "F"]`) and add a category at-a-time. For example,
64
+
- Start with a small set of rules (`select = ["E", "F"]`) and add a group at-a-time. For example,
65
65
you might consider expanding to `select = ["E", "F", "B"]` to enable the popular flake8-bugbear
66
66
extension.
67
67
@@ -144,6 +144,129 @@ with the exception of `F401`.
144
144
When [preview mode](preview.md) is enabled, rule selectors also accept the human-readable name of a
145
145
rule (e.g., `unused-import`).
146
146
147
+
## Rule categories
148
+
149
+
In [preview](preview.md), Ruff supports rule categories in addition to the Flake8-style linter
150
+
groups described above. These categories organize rules by the types of issues they detect and
151
+
determine whether rules are enabled by default. These categories and their descriptions, in
152
+
order of decreasing severity, are:
153
+
154
+
-**Correctness**: These rules flag code that is outright wrong as written. If you encounter a
155
+
correctness issue, you should try to fix it rather than suppressing the error with `noqa` or
156
+
`ruff: ignore`.
157
+
-**Suspicious**: These rules are similar to `correctness` lints in that the code is likely wrong,
158
+
but `suspicious` lints acknowledge that there are valid reasons for the code to be written in this
159
+
way. You will still typically want to fix these issues, but using a suppression comment may
160
+
occasionally be necessary. Deprecations generally also fit into this category.
161
+
-**Complexity**: These rules detect code that can be written in a simpler or more readable way
162
+
without changing its semantics.
163
+
-**Performance**: These rules detect code that can be written in a more efficient way, without changing its semantics or significantly degrading readability.
164
+
-**Style**: These rules flag code that could be written more idiomatically and where the relevant
165
+
idiom has broad community acceptance.
166
+
-**Security**: These rules flag issues that could lead to security vulnerabilities, and as such,
167
+
bias heavily toward false positives to avoid false negatives.
168
+
-**Formatting**: These rules flag formatting issues and are generally redundant with a code
169
+
formatter.
170
+
-**Pedantic**: These rules are generally stylistic, like those in the `style` or similar
171
+
categories, but enforce styles that are too opinionated or are too prone to false positives to fit
172
+
into another category.
173
+
-**Restriction**: These rules restrict the usage of basic language features in arbitrary ways.
174
+
175
+
The first five categories compose the default rule set:
176
+
177
+
=== "pyproject.toml"
178
+
179
+
```toml
180
+
[tool.ruff.lint]
181
+
preview = true
182
+
select = [
183
+
"correctness",
184
+
"suspicious",
185
+
"complexity",
186
+
"performance",
187
+
"style",
188
+
]
189
+
```
190
+
191
+
=== "ruff.toml"
192
+
193
+
```toml
194
+
[lint]
195
+
preview = true
196
+
select = [
197
+
"correctness",
198
+
"suspicious",
199
+
"complexity",
200
+
"performance",
201
+
"style",
202
+
]
203
+
```
204
+
205
+
while the remaining four (`security`, `formatting`, `pedantic`, and `restriction`) are off by
206
+
default. For certain projects, you may want to enable either `security` or `formatting` as entire
207
+
categories, but `pedantic` and `restriction` contain a wider variety of opinionated lints, and you
208
+
will typically only want to select individual rules from these categories directly.
209
+
210
+
### Interaction with other selectors
211
+
212
+
Categories can be freely mixed with linter groups, linter prefixes, rule codes, and rule names. In
213
+
addition to the priority relationships described above for settings like `lint.select`,
214
+
`lint.extend-select`, and `lint.ignore`, and those for various configuration sources like
215
+
`pyproject.toml` files and the CLI, the various selectors also have precedence relationships with
216
+
each other. In general, you can think of this precedence as increasing from the broadest selector
217
+
(`ALL`) to the narrowest single-rule selectors (e.g. `F401` or `unused-import`):
218
+
219
+
```text
220
+
ALL < category < linter group < linter prefix < rule
221
+
```
222
+
223
+
As shown above, this means that configuration like:
224
+
225
+
=== "pyproject.toml"
226
+
227
+
```toml
228
+
[tool.ruff.lint]
229
+
preview = true
230
+
select = ["E", "F"]
231
+
ignore = ["F401"]
232
+
```
233
+
234
+
=== "ruff.toml"
235
+
236
+
```toml
237
+
[lint]
238
+
preview = true
239
+
select = ["E", "F"]
240
+
ignore = ["F401"]
241
+
```
242
+
243
+
will select all `E` and `F` rules, with the exception of `F401`. Analogously, a selection with the
244
+
`suspicious` category like:
245
+
246
+
=== "pyproject.toml"
247
+
248
+
```toml
249
+
[tool.ruff.lint]
250
+
preview = true
251
+
select = ["suspicious"]
252
+
ignore = ["UP"]
253
+
```
254
+
255
+
=== "ruff.toml"
256
+
257
+
```toml
258
+
[lint]
259
+
preview = true
260
+
select = ["suspicious"]
261
+
ignore = ["UP"]
262
+
```
263
+
264
+
would select all `suspicious` rules, except for the `UP` rules in that category.
265
+
266
+
Note that we plan to deprecate and eventually remove the linter groups in the future. If you give
267
+
the new categories a try and run into situations where you need to fall back on linter groups,
268
+
please let us know on the [tracking issue](https://github.com/astral-sh/ruff/issues/27959).
269
+
147
270
## Fixes
148
271
149
272
Ruff supports automatic fixes for a variety of lint errors. For example, Ruff can remove unused
0 commit comments