Skip to content

Commit cdca7f3

Browse files
committed
Create new lints with #[clippy::version = "nightly"]
1 parent 95d7eda commit cdca7f3

17 files changed

+127
-175
lines changed

book/src/development/adding_lints.md

Lines changed: 2 additions & 2 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"
@@ -587,7 +587,7 @@ declare_clippy_lint! {
587587
/// ```rust,ignore
588588
/// // A short example of improved code that doesn't trigger the lint
589589
/// ```
590-
#[clippy::version = "1.29.0"]
590+
#[clippy::version = "nightly"]
591591
pub FOO_FUNCTIONS,
592592
pedantic,
593593
"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. "A function has an 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
@@ -111,8 +111,10 @@ that label in the changelog. If you can, remove the `beta-accepted` labels
111111
112112
### 4. Update `clippy::version` attributes
113113

114-
Next, make sure to check that the `#[clippy::version]` attributes for the added
115-
lints contain the correct version.
114+
Next, make sure to check that the `#[clippy::version]` attributes for the newly
115+
added and deprecated lints contain the version of the release you're writing the
116+
changelog for.
117+
116118
In order to find lints that need a version update, go through the lints in the
117119
"New Lints" section and run the following command for each lint name:
118120

@@ -123,6 +125,9 @@ grep -rB1 "pub $LINT_NAME" .
123125
The version shown should match the version of the release the changelog is
124126
written for. If not, update the version to the changelog version.
125127

128+
Newly created lints will have `#[clippy::version = "nightly"]` and be handled
129+
during the sync, but many existing PRs will still have an incorrect version.
130+
126131
[changelog]: https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md
127132
[forge]: https://forge.rust-lang.org/
128133
[rust_master_tools]: https://github.com/rust-lang/rust/tree/master/src/tools/clippy

clippy_dev/src/deprecate_lint.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::update_lints::{
22
DeprecatedLint, DeprecatedLints, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints,
33
};
4-
use crate::utils::{UpdateMode, Version};
4+
use crate::utils::UpdateMode;
55
use std::ffi::OsStr;
66
use std::path::{Path, PathBuf};
77
use std::{fs, io};
@@ -15,7 +15,7 @@ use std::{fs, io};
1515
/// # Panics
1616
///
1717
/// If a file path could not read from or written to
18-
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
18+
pub fn deprecate(name: &str, reason: &str) {
1919
let prefixed_name = if name.starts_with("clippy::") {
2020
name.to_owned()
2121
} else {
@@ -51,12 +51,7 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
5151
if remove_lint_declaration(stripped_name, &mod_path, &mut lints).unwrap_or(false) {
5252
deprecated_contents.insert_str(
5353
deprecated_end as usize,
54-
&format!(
55-
" #[clippy::version = \"{}\"]\n (\"{}\", \"{}\"),\n",
56-
clippy_version.rust_display(),
57-
prefixed_name,
58-
reason,
59-
),
54+
&format!(" #[clippy::version = \"nightly\"]\n (\"{prefixed_name}\", \"{reason}\"),\n"),
6055
);
6156
deprecated_file.replace_contents(deprecated_contents.as_bytes());
6257
drop(deprecated_file);

clippy_dev/src/main.rs

Lines changed: 3 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(()) => update_lints::update(utils::UpdateMode::Change),
3939
Err(e) => eprintln!("Unable to create lint: {e}"),
4040
},
@@ -78,13 +78,8 @@ fn main() {
7878
old_name,
7979
new_name,
8080
uplift,
81-
} => rename_lint::rename(
82-
clippy.version,
83-
&old_name,
84-
new_name.as_ref().unwrap_or(&old_name),
85-
uplift,
86-
),
87-
DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(clippy.version, &name, &reason),
81+
} => rename_lint::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
82+
DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(&name, &reason),
8883
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
8984
SyncSubcommand::UpdateNightly => sync::update_nightly(),
9085
},

clippy_dev/src/new_lint.rs

Lines changed: 8 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,12 @@ 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+
}}
351+
"#,
365352
)
366353
}
367354

@@ -460,10 +447,7 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
460447
// Add the lint declaration to `mod.rs`
461448
file_contents.insert_str(
462449
lint_decl_end,
463-
&format!(
464-
"\n\n{}",
465-
get_lint_declaration(lint.clippy_version, &lint_name_upper, lint.category)
466-
),
450+
&format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
467451
);
468452

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

clippy_dev/src/rename_lint.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::update_lints::{
22
DeprecatedLints, RenamedLint, find_lint_decls, gen_renamed_lints_test_fn, generate_lint_files,
33
read_deprecated_lints,
44
};
5-
use crate::utils::{FileUpdater, StringReplacer, UpdateMode, Version, try_rename_file};
5+
use crate::utils::{FileUpdater, StringReplacer, UpdateMode, try_rename_file};
66
use std::ffi::OsStr;
77
use std::path::Path;
88
use walkdir::WalkDir;
@@ -23,7 +23,7 @@ use walkdir::WalkDir;
2323
/// * If `old_name` doesn't name an existing lint.
2424
/// * If `old_name` names a deprecated or renamed lint.
2525
#[allow(clippy::too_many_lines)]
26-
pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) {
26+
pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
2727
if let Some((prefix, _)) = old_name.split_once("::") {
2828
panic!("`{old_name}` should not contain the `{prefix}` prefix");
2929
}
@@ -89,10 +89,8 @@ pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: b
8989
deprecated_contents.insert_str(
9090
renamed_end as usize,
9191
&format!(
92-
" #[clippy::version = \"{}\"]\n (\"{}\", \"{}\"),\n",
93-
clippy_version.rust_display(),
94-
lint.old_name,
95-
lint.new_name,
92+
" #[clippy::version = \"nightly\"]\n (\"{}\", \"{}\"),\n",
93+
lint.old_name, lint.new_name,
9694
),
9795
);
9896
deprecated_file.replace_contents(deprecated_contents.as_bytes());

clippy_dev/src/update_lints.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,16 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
244244
#[allow(clippy::enum_glob_use)]
245245
use Token::*;
246246
#[rustfmt::skip]
247-
static DECL_TOKENS: &[Token] = &[
247+
static VERSIONED_DECL: &[Token] = &[
248248
// #[clippy::version = "version"]
249249
Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, LitStr, CloseBracket,
250250
// ("first", "second"),
251251
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
252252
];
253253
#[rustfmt::skip]
254-
static DEPRECATED_TOKENS: &[Token] = &[
255-
// !{ DEPRECATED(DEPRECATED_VERSION) = [
256-
Bang, OpenBrace, Ident("DEPRECATED"), OpenParen, Ident("DEPRECATED_VERSION"), CloseParen, Eq, OpenBracket,
257-
];
258-
#[rustfmt::skip]
259-
static RENAMED_TOKENS: &[Token] = &[
260-
// !{ RENAMED(RENAMED_VERSION) = [
261-
Bang, OpenBrace, Ident("RENAMED"), OpenParen, Ident("RENAMED_VERSION"), CloseParen, Eq, OpenBracket,
254+
static UNVERSIONED_DECL: &[Token] = &[
255+
// ("first", "second"),
256+
OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
262257
];
263258

264259
let path = "clippy_lints/src/deprecated_lints.rs";
@@ -276,14 +271,14 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
276271

277272
// First instance is the macro definition.
278273
assert!(
279-
searcher.find_token(Ident("declare_with_version")),
274+
searcher.find_token(Ident("deprecated")),
280275
"error reading deprecated lints"
281276
);
282277

283-
if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(DEPRECATED_TOKENS, &mut []) {
278+
if searcher.find_token(Ident("deprecated")) && searcher.match_tokens(&[Bang, OpenBracket], &mut []) {
284279
let mut name = "";
285280
let mut reason = "";
286-
while searcher.match_tokens(DECL_TOKENS, &mut [&mut name, &mut reason]) {
281+
while searcher.match_tokens(VERSIONED_DECL, &mut [&mut name, &mut reason]) {
287282
res.deprecated.push(DeprecatedLint {
288283
name: parse_str_single_line(path.as_ref(), name),
289284
reason: parse_str_single_line(path.as_ref(), reason),
@@ -292,13 +287,13 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
292287
} else {
293288
panic!("error reading deprecated lints");
294289
}
295-
// position of the closing `]}` of `declare_with_version`
290+
// position of the closing `];` of `deprecated`
296291
res.deprecated_end = searcher.pos();
297292

298-
if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(RENAMED_TOKENS, &mut []) {
293+
if searcher.find_token(Ident("RENAMED")) && searcher.find_token(Eq) && searcher.find_token(OpenBracket) {
299294
let mut old_name = "";
300295
let mut new_name = "";
301-
while searcher.match_tokens(DECL_TOKENS, &mut [&mut old_name, &mut new_name]) {
296+
while searcher.match_tokens(UNVERSIONED_DECL, &mut [&mut old_name, &mut new_name]) {
302297
res.renamed.push(RenamedLint {
303298
old_name: parse_str_single_line(path.as_ref(), old_name),
304299
new_name: parse_str_single_line(path.as_ref(), new_name),
@@ -307,7 +302,7 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
307302
} else {
308303
panic!("error reading renamed lints");
309304
}
310-
// position of the closing `]}` of `declare_with_version`
305+
// position of the closing `];` of `RENAMED`
311306
res.renamed_end = searcher.pos();
312307

313308
res

0 commit comments

Comments
 (0)