Skip to content

Commit 604b7b0

Browse files
Extend cargo dev rename_lint (#14633)
Based on #14616 Extends `rename_lint` to handle more cases and makes `update_lints` sort `deprecated_lints.rs`. changelog: None
2 parents 94cfebb + 13b070e commit 604b7b0

12 files changed

+1005
-795
lines changed

clippy_dev/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ version = "0.0.1"
55
edition = "2024"
66

77
[dependencies]
8-
aho-corasick = "1.0"
98
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
109
clap = { version = "4.4", features = ["derive"] }
1110
indoc = "1.0"

clippy_dev/src/deprecate_lint.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::update_lints::{
2-
DeprecatedLint, DeprecatedLints, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints,
3-
};
1+
use crate::update_lints::{DeprecatedLint, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints};
42
use crate::utils::{UpdateMode, Version};
53
use std::ffi::OsStr;
64
use std::path::{Path, PathBuf};
@@ -16,28 +14,34 @@ use std::{fs, io};
1614
///
1715
/// If a file path could not read from or written to
1816
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
19-
let prefixed_name = if name.starts_with("clippy::") {
20-
name.to_owned()
21-
} else {
22-
format!("clippy::{name}")
23-
};
24-
let stripped_name = &prefixed_name[8..];
17+
if let Some((prefix, _)) = name.split_once("::") {
18+
panic!("`{name}` should not contain the `{prefix}` prefix");
19+
}
2520

2621
let mut lints = find_lint_decls();
27-
let DeprecatedLints {
28-
renamed: renamed_lints,
29-
deprecated: mut deprecated_lints,
30-
file: mut deprecated_file,
31-
contents: mut deprecated_contents,
32-
deprecated_end,
33-
..
34-
} = read_deprecated_lints();
35-
36-
let Some(lint) = lints.iter().find(|l| l.name == stripped_name) else {
22+
let (mut deprecated_lints, renamed_lints) = read_deprecated_lints();
23+
24+
let Some(lint) = lints.iter().find(|l| l.name == name) else {
3725
eprintln!("error: failed to find lint `{name}`");
3826
return;
3927
};
4028

29+
let prefixed_name = String::from_iter(["clippy::", name]);
30+
match deprecated_lints.binary_search_by(|x| x.name.cmp(&prefixed_name)) {
31+
Ok(_) => {
32+
println!("`{name}` is already deprecated");
33+
return;
34+
},
35+
Err(idx) => deprecated_lints.insert(
36+
idx,
37+
DeprecatedLint {
38+
name: prefixed_name,
39+
reason: reason.into(),
40+
version: clippy_version.rust_display().to_string(),
41+
},
42+
),
43+
}
44+
4145
let mod_path = {
4246
let mut mod_path = PathBuf::from(format!("clippy_lints/src/{}", lint.module));
4347
if mod_path.is_dir() {
@@ -48,24 +52,7 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
4852
mod_path
4953
};
5054

51-
if remove_lint_declaration(stripped_name, &mod_path, &mut lints).unwrap_or(false) {
52-
deprecated_contents.insert_str(
53-
deprecated_end as usize,
54-
&format!(
55-
" #[clippy::version = \"{}\"]\n (\"{}\", \"{}\"),\n",
56-
clippy_version.rust_display(),
57-
prefixed_name,
58-
reason,
59-
),
60-
);
61-
deprecated_file.replace_contents(deprecated_contents.as_bytes());
62-
drop(deprecated_file);
63-
64-
deprecated_lints.push(DeprecatedLint {
65-
name: prefixed_name,
66-
reason: reason.into(),
67-
});
68-
55+
if remove_lint_declaration(name, &mod_path, &mut lints).unwrap_or(false) {
6956
generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
7057
println!("info: `{name}` has successfully been deprecated");
7158
println!("note: you must run `cargo uitest` to update the test results");

clippy_dev/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
#![feature(rustc_private, if_let_guard, let_chains)]
1+
#![feature(
2+
rustc_private,
3+
if_let_guard,
4+
let_chains,
5+
os_str_slice,
6+
os_string_truncate,
7+
slice_split_once
8+
)]
29
#![warn(
310
trivial_casts,
411
trivial_numeric_casts,

0 commit comments

Comments
 (0)