Skip to content

Commit 9b02ff9

Browse files
committed
Create new lints with #[clippy::version = "nightly"]
1 parent 3e218d1 commit 9b02ff9

File tree

17 files changed

+132
-186
lines changed

17 files changed

+132
-186
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 = "nightly"]
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 `"nightly"` 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 = "nightly"]
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 = "nightly"]
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 = "nightly"]` 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/master/src/tools/clippy

clippy_dev/src/deprecate_lint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::update_lints::{DeprecatedLint, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints};
2-
use crate::utils::{UpdateMode, Version};
2+
use crate::utils::UpdateMode;
33
use std::ffi::OsStr;
44
use std::path::{Path, PathBuf};
55
use std::{fs, io};
@@ -13,7 +13,7 @@ use std::{fs, io};
1313
/// # Panics
1414
///
1515
/// If a file path could not read from or written to
16-
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
16+
pub fn deprecate(name: &str, reason: &str) {
1717
if let Some((prefix, _)) = name.split_once("::") {
1818
panic!("`{name}` should not contain the `{prefix}` prefix");
1919
}
@@ -37,7 +37,7 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
3737
DeprecatedLint {
3838
name: prefixed_name,
3939
reason: reason.into(),
40-
version: clippy_version.rust_display().to_string(),
40+
version: "nightly".to_string(),
4141
},
4242
),
4343
}

clippy_dev/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
category,
3636
r#type,
3737
msrv,
38-
} => match new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv) {
38+
} => match new_lint::create(pass, &name, &category, r#type.as_deref(), msrv) {
3939
Ok(()) => update_lints::update(UpdateMode::Change),
4040
Err(e) => eprintln!("Unable to create lint: {e}"),
4141
},
@@ -79,13 +79,8 @@ fn main() {
7979
old_name,
8080
new_name,
8181
uplift,
82-
} => rename_lint::rename(
83-
clippy.version,
84-
&old_name,
85-
new_name.as_ref().unwrap_or(&old_name),
86-
uplift,
87-
),
88-
DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(clippy.version, &name, &reason),
82+
} => rename_lint::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
83+
DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(&name, &reason),
8984
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
9085
SyncSubcommand::UpdateNightly => sync::update_nightly(),
9186
},

clippy_dev/src/new_lint.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{RustSearcher, Token, Version};
1+
use crate::utils::{RustSearcher, Token};
22
use clap::ValueEnum;
33
use indoc::{formatdoc, writedoc};
44
use std::fmt::{self, Write as _};
@@ -22,7 +22,6 @@ impl fmt::Display for Pass {
2222
}
2323

2424
struct LintData<'a> {
25-
clippy_version: Version,
2625
pass: Pass,
2726
name: &'a str,
2827
category: &'a str,
@@ -50,21 +49,13 @@ impl<T> Context for io::Result<T> {
5049
/// # Errors
5150
///
5251
/// This function errors out if the files couldn't be created or written to.
53-
pub fn create(
54-
clippy_version: Version,
55-
pass: Pass,
56-
name: &str,
57-
category: &str,
58-
mut ty: Option<&str>,
59-
msrv: bool,
60-
) -> io::Result<()> {
52+
pub fn create(pass: Pass, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> {
6153
if category == "cargo" && ty.is_none() {
6254
// `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
6355
ty = Some("cargo");
6456
}
6557

6658
let lint = LintData {
67-
clippy_version,
6859
pass,
6960
name,
7061
category,
@@ -293,11 +284,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
293284
);
294285
}
295286

296-
let _: fmt::Result = writeln!(
297-
result,
298-
"{}",
299-
get_lint_declaration(lint.clippy_version, &name_upper, category)
300-
);
287+
let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));
301288

302289
if enable_msrv {
303290
let _: fmt::Result = writedoc!(
@@ -335,7 +322,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
335322
result
336323
}
337324

338-
fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> String {
325+
fn get_lint_declaration(name_upper: &str, category: &str) -> String {
339326
let justification_heading = if category == "restriction" {
340327
"Why restrict this?"
341328
} else {
@@ -356,12 +343,11 @@ fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> S
356343
/// ```no_run
357344
/// // example code which does not raise clippy warning
358345
/// ```
359-
#[clippy::version = "{}"]
346+
#[clippy::version = "nightly"]
360347
pub {name_upper},
361348
{category},
362349
"default lint description"
363-
}}"#,
364-
version.rust_display(),
350+
}}"#
365351
)
366352
}
367353

@@ -460,10 +446,7 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
460446
// Add the lint declaration to `mod.rs`
461447
file_contents.insert_str(
462448
lint_decl_end,
463-
&format!(
464-
"\n\n{}",
465-
get_lint_declaration(lint.clippy_version, &lint_name_upper, lint.category)
466-
),
449+
&format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
467450
);
468451

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

clippy_dev/src/rename_lint.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::update_lints::{RenamedLint, find_lint_decls, generate_lint_files, read_deprecated_lints};
22
use crate::utils::{
3-
ErrAction, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, Version, delete_dir_if_exists,
4-
delete_file_if_exists, expect_action, try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
3+
ErrAction, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, delete_dir_if_exists, delete_file_if_exists,
4+
expect_action, try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
55
};
66
use rustc_lexer::TokenKind;
77
use std::ffi::OsString;
@@ -24,7 +24,7 @@ use std::path::Path;
2424
/// * If `old_name` doesn't name an existing lint.
2525
/// * If `old_name` names a deprecated or renamed lint.
2626
#[expect(clippy::too_many_lines)]
27-
pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) {
27+
pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
2828
if let Some((prefix, _)) = old_name.split_once("::") {
2929
panic!("`{old_name}` should not contain the `{prefix}` prefix");
3030
}
@@ -68,7 +68,6 @@ pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: b
6868
} else {
6969
String::from_iter(["clippy::", new_name])
7070
},
71-
version: clippy_version.rust_display().to_string(),
7271
},
7372
);
7473
},

clippy_dev/src/update_lints.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ pub fn generate_lint_files(
8181
&mut |_, src, dst| {
8282
let mut searcher = RustSearcher::new(src);
8383
assert!(
84-
searcher.find_token(Token::Ident("declare_with_version"))
85-
&& searcher.find_token(Token::Ident("declare_with_version")),
84+
searcher.find_token(Token::Ident("deprecated")) && searcher.find_token(Token::Ident("deprecated")),
8685
"error reading deprecated lints"
8786
);
8887
dst.push_str(&src[..searcher.pos() as usize]);
89-
dst.push_str("! { DEPRECATED(DEPRECATED_VERSION) = [\n");
88+
dst.push_str("![\n");
9089
for lint in deprecated {
9190
write!(
9291
dst,
@@ -96,20 +95,15 @@ pub fn generate_lint_files(
9695
.unwrap();
9796
}
9897
dst.push_str(
99-
"]}\n\n\
98+
"];\n\n\
10099
#[rustfmt::skip]\n\
101-
declare_with_version! { RENAMED(RENAMED_VERSION) = [\n\
100+
pub const RENAMED: &[(&str, &str)] = &[\n\
102101
",
103102
);
104103
for lint in renamed {
105-
write!(
106-
dst,
107-
" #[clippy::version = \"{}\"]\n (\"{}\", \"{}\"),\n",
108-
lint.version, lint.old_name, lint.new_name,
109-
)
110-
.unwrap();
104+
writeln!(dst, " (\"{}\", \"{}\"),", lint.old_name, lint.new_name).unwrap();
111105
}
112-
dst.push_str("]}\n");
106+
dst.push_str("];\n");
113107
UpdateStatus::from_changed(src != dst)
114108
},
115109
);
@@ -220,7 +214,6 @@ pub struct DeprecatedLint {
220214
pub struct RenamedLint {
221215
pub old_name: String,
222216
pub new_name: String,
223-
pub version: String,
224217
}
225218

226219
/// Finds all lint declarations (`declare_clippy_lint!`)
@@ -318,21 +311,14 @@ pub fn read_deprecated_lints() -> (Vec<DeprecatedLint>, Vec<RenamedLint>) {
318311
#[allow(clippy::enum_glob_use)]
319312
use Token::*;
320313
#[rustfmt::skip]
321-
static DECL_TOKENS: &[Token<'_>] = &[
314+
static VERSION_TOKENS: &[Token<'_>] = &[
322315
// #[clippy::version = "version"]
323316
Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, CaptureLitStr, CloseBracket,
324-
// ("first", "second"),
325-
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
326-
];
327-
#[rustfmt::skip]
328-
static DEPRECATED_TOKENS: &[Token<'_>] = &[
329-
// !{ DEPRECATED(DEPRECATED_VERSION) = [
330-
Bang, OpenBrace, Ident("DEPRECATED"), OpenParen, Ident("DEPRECATED_VERSION"), CloseParen, Eq, OpenBracket,
331317
];
332318
#[rustfmt::skip]
333-
static RENAMED_TOKENS: &[Token<'_>] = &[
334-
// !{ RENAMED(RENAMED_VERSION) = [
335-
Bang, OpenBrace, Ident("RENAMED"), OpenParen, Ident("RENAMED_VERSION"), CloseParen, Eq, OpenBracket,
319+
static DECL_TOKENS: &[Token<'_>] = &[
320+
// ("first", "second"),
321+
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
336322
];
337323

338324
let path = "clippy_lints/src/deprecated_lints.rs";
@@ -345,15 +331,16 @@ pub fn read_deprecated_lints() -> (Vec<DeprecatedLint>, Vec<RenamedLint>) {
345331

346332
// First instance is the macro definition.
347333
assert!(
348-
searcher.find_token(Ident("declare_with_version")),
334+
searcher.find_token(Ident("deprecated")),
349335
"error reading deprecated lints"
350336
);
351337

352-
if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(DEPRECATED_TOKENS, &mut []) {
338+
if searcher.find_token(Ident("deprecated")) && searcher.match_tokens(&[Bang, OpenBracket], &mut []) {
353339
let mut version = "";
354340
let mut name = "";
355341
let mut reason = "";
356-
while searcher.match_tokens(DECL_TOKENS, &mut [&mut version, &mut name, &mut reason]) {
342+
while searcher.match_tokens(VERSION_TOKENS, &mut [&mut version]) {
343+
assert!(searcher.match_tokens(DECL_TOKENS, &mut [&mut name, &mut reason]));
357344
deprecated.push(DeprecatedLint {
358345
name: parse_str_single_line(path.as_ref(), name),
359346
reason: parse_str_single_line(path.as_ref(), reason),
@@ -364,15 +351,15 @@ pub fn read_deprecated_lints() -> (Vec<DeprecatedLint>, Vec<RenamedLint>) {
364351
panic!("error reading deprecated lints");
365352
}
366353

367-
if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(RENAMED_TOKENS, &mut []) {
368-
let mut version = "";
354+
// pub const RENAMED: &[(&str, &str)] = &[
355+
// ^^^^^^^ ^ ^
356+
if searcher.find_token(Ident("RENAMED")) && searcher.find_token(Eq) && searcher.find_token(OpenBracket) {
369357
let mut old_name = "";
370358
let mut new_name = "";
371-
while searcher.match_tokens(DECL_TOKENS, &mut [&mut version, &mut old_name, &mut new_name]) {
359+
while searcher.match_tokens(DECL_TOKENS, &mut [&mut old_name, &mut new_name]) {
372360
renamed.push(RenamedLint {
373361
old_name: parse_str_single_line(path.as_ref(), old_name),
374362
new_name: parse_str_single_line(path.as_ref(), new_name),
375-
version: parse_str_single_line(path.as_ref(), version),
376363
});
377364
}
378365
} else {

0 commit comments

Comments
 (0)