Skip to content

Commit cdd4dfc

Browse files
committed
Auto merge of rust-lang#9182 - Serial-ATA:new_lint-type-flag, r=xFrednet
Add `--type` flag to `dev new_lint` changelog: none This only works with directories, `--type={copies, doc, ...}` won't work. Should they be counted?
2 parents d814681 + 8319196 commit cdd4dfc

File tree

7 files changed

+355
-74
lines changed

7 files changed

+355
-74
lines changed

book/src/development/adding_lints.md

+35-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ because that's clearly a non-descriptive name.
1010
- [Adding a new lint](#adding-a-new-lint)
1111
- [Setup](#setup)
1212
- [Getting Started](#getting-started)
13+
- [Defining Our Lint](#defining-our-lint)
14+
- [Standalone](#standalone)
15+
- [Specific Type](#specific-type)
16+
- [Tests Location](#tests-location)
1317
- [Testing](#testing)
1418
- [Cargo lints](#cargo-lints)
1519
- [Rustfix tests](#rustfix-tests)
@@ -36,17 +40,38 @@ See the [Basics](basics.md#get-the-code) documentation.
3640
## Getting Started
3741

3842
There is a bit of boilerplate code that needs to be set up when creating a new
39-
lint. Fortunately, you can use the clippy dev tools to handle this for you. We
43+
lint. Fortunately, you can use the Clippy dev tools to handle this for you. We
4044
are naming our new lint `foo_functions` (lints are generally written in snake
41-
case), and we don't need type information so it will have an early pass type
42-
(more on this later on). If you're not sure if the name you chose fits the lint,
43-
take a look at our [lint naming guidelines][lint_naming]. To get started on this
44-
lint you can run `cargo dev new_lint --name=foo_functions --pass=early
45-
--category=pedantic` (category will default to nursery if not provided). This
46-
command will create two files: `tests/ui/foo_functions.rs` and
47-
`clippy_lints/src/foo_functions.rs`, as well as [registering the
48-
lint](#lint-registration). For cargo lints, two project hierarchies (fail/pass)
49-
will be created by default under `tests/ui-cargo`.
45+
case), and we don't need type information, so it will have an early pass type
46+
(more on this later). If you're unsure if the name you chose fits the lint,
47+
take a look at our [lint naming guidelines][lint_naming].
48+
49+
## Defining Our Lint
50+
To get started, there are two ways to define our lint.
51+
52+
### Standalone
53+
Command: `cargo dev new_lint --name=foo_functions --pass=early --category=pedantic`
54+
(category will default to nursery if not provided)
55+
56+
This command will create a new file: `clippy_lints/src/foo_functions.rs`, as well
57+
as [register the lint](#lint-registration).
58+
59+
### Specific Type
60+
Command: `cargo dev new_lint --name=foo_functions --type=functions --category=pedantic`
61+
62+
This command will create a new file: `clippy_lints/src/{type}/foo_functions.rs`.
63+
64+
Notice how this command has a `--type` flag instead of `--pass`. Unlike a standalone
65+
definition, this lint won't be registered in the traditional sense. Instead, you will
66+
call your lint from within the type's lint pass, found in `clippy_lints/src/{type}/mod.rs`.
67+
68+
A "type" is just the name of a directory in `clippy_lints/src`, like `functions` in
69+
the example command. These are groupings of lints with common behaviors, so if your
70+
lint falls into one, it would be best to add it to that type.
71+
72+
### Tests Location
73+
Both commands will create a file: `tests/ui/foo_functions.rs`. For cargo lints,
74+
two project hierarchies (fail/pass) will be created by default under `tests/ui-cargo`.
5075

5176
Next, we'll open up these files and add our lint!
5277

clippy_dev/src/main.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ fn main() {
3636
match new_lint::create(
3737
matches.get_one::<String>("pass"),
3838
matches.get_one::<String>("name"),
39-
matches.get_one::<String>("category"),
39+
matches.get_one::<String>("category").map(String::as_str),
40+
matches.get_one::<String>("type").map(String::as_str),
4041
matches.contains_id("msrv"),
4142
) {
4243
Ok(_) => update_lints::update(update_lints::UpdateMode::Change),
@@ -157,7 +158,8 @@ fn get_clap_config() -> ArgMatches {
157158
.help("Specify whether the lint runs during the early or late pass")
158159
.takes_value(true)
159160
.value_parser([PossibleValue::new("early"), PossibleValue::new("late")])
160-
.required(true),
161+
.conflicts_with("type")
162+
.required_unless_present("type"),
161163
Arg::new("name")
162164
.short('n')
163165
.long("name")
@@ -183,6 +185,11 @@ fn get_clap_config() -> ArgMatches {
183185
PossibleValue::new("internal_warn"),
184186
])
185187
.takes_value(true),
188+
Arg::new("type")
189+
.long("type")
190+
.help("What directory the lint belongs in")
191+
.takes_value(true)
192+
.required(false),
186193
Arg::new("msrv").long("msrv").help("Add MSRV config code to the lint"),
187194
]),
188195
Command::new("setup")

0 commit comments

Comments
 (0)