Skip to content

Commit 8b0f882

Browse files
committed
Create new lints with #[clippy::version = "dev"]
1 parent 4180830 commit 8b0f882

File tree

17 files changed

+128
-183
lines changed

17 files changed

+128
-183
lines changed

book/src/development/adding_lints.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ declare_clippy_lint! {
233233
/// ```rust
234234
/// // example code
235235
/// ```
236-
#[clippy::version = "1.29.0"]
236+
#[clippy::version = "dev"]
237237
pub FOO_FUNCTIONS,
238238
pedantic,
239239
"function named `foo`, which is not a descriptive name"
@@ -245,10 +245,8 @@ declare_clippy_lint! {
245245
this][example_lint_page]. To render and open this documentation locally in a
246246
browser, run `cargo dev serve`.
247247
* The `#[clippy::version]` attribute will be rendered as part of the lint
248-
documentation. The value should be set to the current Rust version that the
249-
lint is developed in, it can be retrieved by running `rustc -vV` in the
250-
rust-clippy directory. The version is listed under *release*. (Use the version
251-
without the `-nightly`) suffix.
248+
documentation. The value of `"dev"` will be replaced automatically
249+
as part of the release process after the lint is merged.
252250
* `FOO_FUNCTIONS` is the name of our lint. Be sure to follow the [lint naming
253251
guidelines][lint_naming] here when naming your lint. In short, the name should
254252
state the thing that is being checked for and read well when used with
@@ -587,7 +585,7 @@ declare_clippy_lint! {
587585
/// ```rust,ignore
588586
/// // A short example of improved code that doesn't trigger the lint
589587
/// ```
590-
#[clippy::version = "1.29.0"]
588+
#[clippy::version = "dev"]
591589
pub FOO_FUNCTIONS,
592590
pedantic,
593591
"function named `foo`, which is not a descriptive name"

book/src/development/defining_lints.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ declare_clippy_lint! {
168168
/// ```rust
169169
/// // example code which does not raise Clippy warning
170170
/// ```
171-
#[clippy::version = "1.70.0"] // <- In which version was this implemented, keep it up to date!
171+
#[clippy::version = "dev"]
172172
pub LINT_NAME, // <- The lint name IN_ALL_CAPS
173173
pedantic, // <- The lint group
174-
"default lint description" // <- A lint description, e.g. "A function has an unit return type."
174+
"default lint description" // <- A lint description, e.g. "function with the unit return type"
175175
}
176176
```
177177

book/src/development/infrastructure/changelog_update.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ that label in the changelog. If you can, remove the `beta-accepted` labels
9898
9999
### 5. Update `clippy::version` attributes
100100

101-
Next, make sure to check that the `#[clippy::version]` attributes for the added
102-
lints contain the correct version.
101+
Next, make sure to check that the `#[clippy::version]` attributes for the newly
102+
added and deprecated lints contain the version of the release you're writing the
103+
changelog for.
104+
103105
In order to find lints that need a version update, go through the lints in the
104106
"New Lints" section and run the following command for each lint name:
105107

@@ -110,6 +112,9 @@ grep -rB1 "pub $LINT_NAME" .
110112
The version shown should match the version of the release the changelog is
111113
written for. If not, update the version to the changelog version.
112114

115+
Newly created lints will have `#[clippy::version = "dev"]` and be handled
116+
during the sync, but many existing PRs will still have an incorrect version.
117+
113118
[changelog]: https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md
114119
[forge]: https://forge.rust-lang.org/
115120
[rust_master_tools]: https://github.com/rust-lang/rust/tree/HEAD/src/tools/clippy

clippy_dev/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn main() {
3434
category,
3535
r#type,
3636
msrv,
37-
} => match new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv) {
37+
} => match new_lint::create(pass, &name, &category, r#type.as_deref(), msrv) {
3838
Ok(()) => new_parse_cx(|cx| update_lints::update(cx, UpdateMode::Change)),
3939
Err(e) => eprintln!("Unable to create lint: {e}"),
4040
},
@@ -79,13 +79,7 @@ fn main() {
7979
new_name,
8080
uplift,
8181
} => new_parse_cx(|cx| {
82-
rename_lint::rename(
83-
cx,
84-
clippy.version,
85-
&old_name,
86-
new_name.as_ref().unwrap_or(&old_name),
87-
uplift,
88-
);
82+
rename_lint::rename(cx, &old_name, new_name.as_ref().unwrap_or(&old_name), uplift);
8983
}),
9084
DevCommand::Deprecate { name, reason } => {
9185
new_parse_cx(|cx| deprecate_lint::deprecate(cx, clippy.version, &name, &reason));

clippy_dev/src/new_lint.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::parse::cursor::{self, Capture, Cursor};
2-
use crate::utils::Version;
32
use clap::ValueEnum;
43
use indoc::{formatdoc, writedoc};
54
use std::fmt::{self, Write as _};
@@ -23,7 +22,6 @@ impl fmt::Display for Pass {
2322
}
2423

2524
struct LintData<'a> {
26-
clippy_version: Version,
2725
pass: Pass,
2826
name: &'a str,
2927
category: &'a str,
@@ -51,21 +49,13 @@ impl<T> Context for io::Result<T> {
5149
/// # Errors
5250
///
5351
/// This function errors out if the files couldn't be created or written to.
54-
pub fn create(
55-
clippy_version: Version,
56-
pass: Pass,
57-
name: &str,
58-
category: &str,
59-
mut ty: Option<&str>,
60-
msrv: bool,
61-
) -> io::Result<()> {
52+
pub fn create(pass: Pass, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> {
6253
if category == "cargo" && ty.is_none() {
6354
// `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
6455
ty = Some("cargo");
6556
}
6657

6758
let lint = LintData {
68-
clippy_version,
6959
pass,
7060
name,
7161
category,
@@ -295,11 +285,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
295285
);
296286
}
297287

298-
let _: fmt::Result = writeln!(
299-
result,
300-
"{}",
301-
get_lint_declaration(lint.clippy_version, &name_upper, category)
302-
);
288+
let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));
303289

304290
if enable_msrv {
305291
let _: fmt::Result = writedoc!(
@@ -337,7 +323,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
337323
result
338324
}
339325

340-
fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> String {
326+
fn get_lint_declaration(name_upper: &str, category: &str) -> String {
341327
let justification_heading = if category == "restriction" {
342328
"Why restrict this?"
343329
} else {
@@ -358,12 +344,11 @@ fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> S
358344
/// ```no_run
359345
/// // example code which does not raise clippy warning
360346
/// ```
361-
#[clippy::version = "{}"]
347+
#[clippy::version = "dev"]
362348
pub {name_upper},
363349
{category},
364350
"default lint description"
365-
}}"#,
366-
version.rust_display(),
351+
}}"#
367352
)
368353
}
369354

@@ -461,10 +446,7 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
461446
// Add the lint declaration to `mod.rs`
462447
file_contents.insert_str(
463448
lint_decl_end,
464-
&format!(
465-
"\n\n{}",
466-
get_lint_declaration(lint.clippy_version, &lint_name_upper, lint.category)
467-
),
449+
&format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
468450
);
469451

470452
// Add the lint to `impl_lint_pass`/`declare_lint_pass`

clippy_dev/src/parse.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ pub struct DeprecatedLint<'cx> {
9898
pub struct RenamedLint<'cx> {
9999
pub old_name: &'cx str,
100100
pub new_name: &'cx str,
101-
pub version: &'cx str,
102101
}
103102

104103
impl<'cx> ParseCxImpl<'cx> {
@@ -186,21 +185,16 @@ impl<'cx> ParseCxImpl<'cx> {
186185
#[allow(clippy::enum_glob_use)]
187186
use cursor::Pat::*;
188187
#[rustfmt::skip]
189-
static DECL_TOKENS: &[cursor::Pat<'_>] = &[
188+
static VERSIONED_DECL: &[cursor::Pat<'_>] = &[
190189
// #[clippy::version = "version"]
191190
Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, CaptureLitStr, CloseBracket,
192191
// ("first", "second"),
193192
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
194193
];
195194
#[rustfmt::skip]
196-
static DEPRECATED_TOKENS: &[cursor::Pat<'_>] = &[
197-
// !{ DEPRECATED(DEPRECATED_VERSION) = [
198-
Bang, OpenBrace, Ident("DEPRECATED"), OpenParen, Ident("DEPRECATED_VERSION"), CloseParen, Eq, OpenBracket,
199-
];
200-
#[rustfmt::skip]
201-
static RENAMED_TOKENS: &[cursor::Pat<'_>] = &[
202-
// !{ RENAMED(RENAMED_VERSION) = [
203-
Bang, OpenBrace, Ident("RENAMED"), OpenParen, Ident("RENAMED_VERSION"), CloseParen, Eq, OpenBracket,
195+
static UNVERSIONED_DECL: &[cursor::Pat<'_>] = &[
196+
// ("first", "second"),
197+
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
204198
];
205199

206200
let path = "clippy_lints/src/deprecated_lints.rs";
@@ -214,12 +208,12 @@ impl<'cx> ParseCxImpl<'cx> {
214208

215209
// First instance is the macro definition.
216210
assert!(
217-
cursor.find_ident("declare_with_version").is_some(),
211+
cursor.find_ident("deprecated").is_some(),
218212
"error reading deprecated lints"
219213
);
220214

221-
if cursor.find_ident("declare_with_version").is_some() && cursor.match_all(DEPRECATED_TOKENS, &mut []) {
222-
while cursor.match_all(DECL_TOKENS, &mut captures) {
215+
if cursor.find_ident("deprecated").is_some() && cursor.match_all(&[Bang, OpenBracket], &mut []) {
216+
while cursor.match_all(VERSIONED_DECL, &mut captures) {
223217
deprecated.push(DeprecatedLint {
224218
name: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[1])),
225219
reason: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[2])),
@@ -230,12 +224,13 @@ impl<'cx> ParseCxImpl<'cx> {
230224
panic!("error reading deprecated lints");
231225
}
232226

233-
if cursor.find_ident("declare_with_version").is_some() && cursor.match_all(RENAMED_TOKENS, &mut []) {
234-
while cursor.match_all(DECL_TOKENS, &mut captures) {
227+
// pub const RENAMED: &[(&str, &str)] = &[
228+
// ^^^^^^^ ^ ^
229+
if cursor.find_ident("RENAMED").is_some() && cursor.find_pat(Eq) && cursor.find_pat(OpenBracket) {
230+
while cursor.match_all(UNVERSIONED_DECL, &mut captures) {
235231
renamed.push(RenamedLint {
236-
old_name: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[1])),
237-
new_name: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[2])),
238-
version: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[0])),
232+
old_name: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[0])),
233+
new_name: self.parse_str_single_line(path.as_ref(), cursor.get_text(captures[1])),
239234
});
240235
}
241236
} else {

clippy_dev/src/rename_lint.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::parse::cursor::{self, Capture, Cursor};
22
use crate::parse::{ParseCx, RenamedLint};
33
use crate::update_lints::generate_lint_files;
44
use crate::utils::{
5-
ErrAction, FileUpdater, UpdateMode, UpdateStatus, Version, delete_dir_if_exists, delete_file_if_exists,
6-
expect_action, try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
5+
ErrAction, FileUpdater, UpdateMode, UpdateStatus, delete_dir_if_exists, delete_file_if_exists, expect_action,
6+
try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
77
};
88
use rustc_lexer::TokenKind;
99
use std::ffi::OsString;
@@ -26,7 +26,7 @@ use std::path::Path;
2626
/// * If `old_name` doesn't name an existing lint.
2727
/// * If `old_name` names a deprecated or renamed lint.
2828
#[expect(clippy::too_many_lines)]
29-
pub fn rename<'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_name: &'cx str, new_name: &'cx str, uplift: bool) {
29+
pub fn rename<'cx>(cx: ParseCx<'cx>, old_name: &'cx str, new_name: &'cx str, uplift: bool) {
3030
let mut updater = FileUpdater::default();
3131
let mut lints = cx.find_lint_decls();
3232
let (deprecated_lints, mut renamed_lints) = cx.read_deprecated_lints();
@@ -65,7 +65,6 @@ pub fn rename<'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_name: &'cx str
6565
RenamedLint {
6666
old_name: old_name_prefixed,
6767
new_name: new_name_prefixed,
68-
version: cx.str_buf.alloc_display(cx.arena, clippy_version.rust_display()),
6968
},
7069
);
7170
},

clippy_dev/src/update_lints.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ pub fn generate_lint_files(
7878
&mut |_, src, dst| {
7979
let mut cursor = Cursor::new(src);
8080
assert!(
81-
cursor.find_ident("declare_with_version").is_some()
82-
&& cursor.find_ident("declare_with_version").is_some(),
81+
cursor.find_ident("deprecated").is_some() && cursor.find_ident("deprecated").is_some(),
8382
"error reading deprecated lints"
8483
);
8584
dst.push_str(&src[..cursor.pos() as usize]);
86-
dst.push_str("! { DEPRECATED(DEPRECATED_VERSION) = [\n");
85+
dst.push_str("![\n");
8786
for lint in deprecated {
8887
write!(
8988
dst,
@@ -93,20 +92,15 @@ pub fn generate_lint_files(
9392
.unwrap();
9493
}
9594
dst.push_str(
96-
"]}\n\n\
95+
"];\n\n\
9796
#[rustfmt::skip]\n\
98-
declare_with_version! { RENAMED(RENAMED_VERSION) = [\n\
97+
pub const RENAMED: &[(&str, &str)] = &[\n\
9998
",
10099
);
101100
for lint in renamed {
102-
write!(
103-
dst,
104-
" #[clippy::version = \"{}\"]\n (\"{}\", \"{}\"),\n",
105-
lint.version, lint.old_name, lint.new_name,
106-
)
107-
.unwrap();
101+
writeln!(dst, " (\"{}\", \"{}\"),", lint.old_name, lint.new_name).unwrap();
108102
}
109-
dst.push_str("]}\n");
103+
dst.push_str("];\n");
110104
UpdateStatus::from_changed(src != dst)
111105
},
112106
);

0 commit comments

Comments
 (0)