Skip to content

Commit 444ef3e

Browse files
committed
Auto merge of rust-lang#7880 - Alexendoo:newlint-add-pass, r=flip1995
Register the generated lints from `cargo dev new_lint` How to register a lint was something that took me a couple reads to figure out, this will hopefully make that easier. It appends the created lint to the end of the list when running `cargo dev new_lint` changelog: none
2 parents ba2ac3e + bd778e2 commit 444ef3e

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

clippy_dev/src/new_lint.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str
4242
};
4343

4444
create_lint(&lint, msrv).context("Unable to create lint implementation")?;
45-
create_test(&lint).context("Unable to create a test for the new lint")
45+
create_test(&lint).context("Unable to create a test for the new lint")?;
46+
add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")
4647
}
4748

4849
fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
@@ -80,6 +81,33 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
8081
}
8182
}
8283

84+
fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
85+
let path = "clippy_lints/src/lib.rs";
86+
let mut lib_rs = fs::read_to_string(path).context("reading")?;
87+
88+
let comment_start = lib_rs.find("// add lints here,").expect("Couldn't find comment");
89+
90+
let new_lint = if enable_msrv {
91+
format!(
92+
"store.register_{lint_pass}_pass(move || Box::new({module_name}::{camel_name}::new(msrv)));\n ",
93+
lint_pass = lint.pass,
94+
module_name = lint.name,
95+
camel_name = to_camel_case(lint.name),
96+
)
97+
} else {
98+
format!(
99+
"store.register_{lint_pass}_pass(|| Box::new({module_name}::{camel_name}));\n ",
100+
lint_pass = lint.pass,
101+
module_name = lint.name,
102+
camel_name = to_camel_case(lint.name),
103+
)
104+
};
105+
106+
lib_rs.insert_str(comment_start, &new_lint);
107+
108+
fs::write(path, lib_rs).context("writing")
109+
}
110+
83111
fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
84112
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
85113
OpenOptions::new()
@@ -151,7 +179,6 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
151179
};
152180

153181
let lint_name = lint.name;
154-
let pass_name = lint.pass;
155182
let category = lint.category;
156183
let name_camel = to_camel_case(lint.name);
157184
let name_upper = lint_name.to_uppercase();
@@ -228,18 +255,14 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
228255
extract_msrv_attr!({context_import});
229256
}}
230257
231-
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
232-
// e.g. store.register_{pass_name}_pass(move || Box::new({module_name}::{name_camel}::new(msrv)));
233258
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
234259
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
235260
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
236261
"},
237262
pass_type = pass_type,
238263
pass_lifetimes = pass_lifetimes,
239-
pass_name = pass_name,
240264
name_upper = name_upper,
241265
name_camel = name_camel,
242-
module_name = lint_name,
243266
context_import = context_import,
244267
)
245268
} else {
@@ -248,16 +271,11 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
248271
declare_lint_pass!({name_camel} => [{name_upper}]);
249272
250273
impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
251-
//
252-
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
253-
// e.g. store.register_{pass_name}_pass(|| Box::new({module_name}::{name_camel}));
254274
"},
255275
pass_type = pass_type,
256276
pass_lifetimes = pass_lifetimes,
257-
pass_name = pass_name,
258277
name_upper = name_upper,
259278
name_camel = name_camel,
260-
module_name = lint_name,
261279
)
262280
});
263281

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
777777
store.register_late_pass(|| Box::new(match_str_case_mismatch::MatchStrCaseMismatch));
778778
store.register_late_pass(move || Box::new(format_args::FormatArgs));
779779
store.register_late_pass(|| Box::new(trailing_empty_array::TrailingEmptyArray));
780-
780+
// add lints here, do not remove this comment, it's used in `new_lint`
781781
}
782782

783783
#[rustfmt::skip]

doc/adding_lints.md

+24-21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ because that's clearly a non-descriptive name.
1616
- [Edition 2018 tests](#edition-2018-tests)
1717
- [Testing manually](#testing-manually)
1818
- [Lint declaration](#lint-declaration)
19+
- [Lint registration](#lint-registration)
1920
- [Lint passes](#lint-passes)
2021
- [Emitting a lint](#emitting-a-lint)
2122
- [Adding the lint logic](#adding-the-lint-logic)
@@ -43,9 +44,9 @@ take a look at our [lint naming guidelines][lint_naming]. To get started on this
4344
lint you can run `cargo dev new_lint --name=foo_functions --pass=early
4445
--category=pedantic` (category will default to nursery if not provided). This
4546
command will create two files: `tests/ui/foo_functions.rs` and
46-
`clippy_lints/src/foo_functions.rs`, as well as run `cargo dev update_lints` to
47-
register the new lint. For cargo lints, two project hierarchies (fail/pass) will
48-
be created by default under `tests/ui-cargo`.
47+
`clippy_lints/src/foo_functions.rs`, as well as
48+
[registering the lint](#lint-registration). For cargo lints, two project
49+
hierarchies (fail/pass) will be created by default under `tests/ui-cargo`.
4950

5051
Next, we'll open up these files and add our lint!
5152

@@ -220,32 +221,34 @@ declare_lint_pass!(FooFunctions => [FOO_FUNCTIONS]);
220221
impl EarlyLintPass for FooFunctions {}
221222
```
222223

223-
Normally after declaring the lint, we have to run `cargo dev update_lints`,
224-
which updates some files, so Clippy knows about the new lint. Since we used
225-
`cargo dev new_lint ...` to generate the lint declaration, this was done
226-
automatically. While `update_lints` automates most of the things, it doesn't
227-
automate everything. We will have to register our lint pass manually in the
228-
`register_plugins` function in `clippy_lints/src/lib.rs`:
224+
[declare_clippy_lint]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/lib.rs#L60
225+
[example_lint_page]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
226+
[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints
227+
[category_level_mapping]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/lib.rs#L110
228+
229+
## Lint registration
230+
231+
When using `cargo dev new_lint`, the lint is automatically registered and
232+
nothing more has to be done.
233+
234+
When declaring a new lint by hand and `cargo dev update_lints` is used, the lint
235+
pass may have to be registered manually in the `register_plugins` function in
236+
`clippy_lints/src/lib.rs`:
229237

230238
```rust
231-
store.register_early_pass(|| box foo_functions::FooFunctions);
239+
store.register_early_pass(|| Box::new(foo_functions::FooFunctions));
232240
```
233241

234242
As one may expect, there is a corresponding `register_late_pass` method
235243
available as well. Without a call to one of `register_early_pass` or
236244
`register_late_pass`, the lint pass in question will not be run.
237245

238-
One reason that `cargo dev` does not automate this step is that multiple lints
239-
can use the same lint pass, so registering the lint pass may already be done
240-
when adding a new lint. Another reason that this step is not automated is that
241-
the order that the passes are registered determines the order the passes
242-
actually run, which in turn affects the order that any emitted lints are output
243-
in.
244-
245-
[declare_clippy_lint]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/lib.rs#L60
246-
[example_lint_page]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
247-
[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints
248-
[category_level_mapping]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/lib.rs#L110
246+
One reason that `cargo dev update_lints` does not automate this step is that
247+
multiple lints can use the same lint pass, so registering the lint pass may
248+
already be done when adding a new lint. Another reason that this step is not
249+
automated is that the order that the passes are registered determines the order
250+
the passes actually run, which in turn affects the order that any emitted lints
251+
are output in.
249252

250253
## Lint passes
251254

0 commit comments

Comments
 (0)