|
| 1 | +use crate::update_lints::{ |
| 2 | + RenamedLint, clippy_lints_src_files, gather_all, gen_renamed_lints_test, generate_lint_files, |
| 3 | +}; |
| 4 | +use crate::utils::{ |
| 5 | + UpdateMode, Version, insert_at_marker, replace_ident_like, rewrite_file, try_rename_file, write_file, |
| 6 | +}; |
| 7 | +use std::ffi::OsStr; |
| 8 | +use std::path::Path; |
| 9 | +use walkdir::WalkDir; |
| 10 | + |
| 11 | +/// Runs the `rename_lint` command. |
| 12 | +/// |
| 13 | +/// This does the following: |
| 14 | +/// * Adds an entry to `renamed_lints.rs`. |
| 15 | +/// * Renames all lint attributes to the new name (e.g. `#[allow(clippy::lint_name)]`). |
| 16 | +/// * Renames the lint struct to the new name. |
| 17 | +/// * Renames the module containing the lint struct to the new name if it shares a name with the |
| 18 | +/// lint. |
| 19 | +/// |
| 20 | +/// # Panics |
| 21 | +/// Panics for the following conditions: |
| 22 | +/// * If a file path could not read from or then written to |
| 23 | +/// * If either lint name has a prefix |
| 24 | +/// * If `old_name` doesn't name an existing lint. |
| 25 | +/// * If `old_name` names a deprecated or renamed lint. |
| 26 | +#[allow(clippy::too_many_lines)] |
| 27 | +pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) { |
| 28 | + if let Some((prefix, _)) = old_name.split_once("::") { |
| 29 | + panic!("`{old_name}` should not contain the `{prefix}` prefix"); |
| 30 | + } |
| 31 | + if let Some((prefix, _)) = new_name.split_once("::") { |
| 32 | + panic!("`{new_name}` should not contain the `{prefix}` prefix"); |
| 33 | + } |
| 34 | + |
| 35 | + let (mut lints, deprecated_lints, mut renamed_lints) = gather_all(); |
| 36 | + let mut old_lint_index = None; |
| 37 | + let mut found_new_name = false; |
| 38 | + for (i, lint) in lints.iter().enumerate() { |
| 39 | + if lint.name == old_name { |
| 40 | + old_lint_index = Some(i); |
| 41 | + } else if lint.name == new_name { |
| 42 | + found_new_name = true; |
| 43 | + } |
| 44 | + } |
| 45 | + let old_lint_index = old_lint_index.unwrap_or_else(|| panic!("could not find lint `{old_name}`")); |
| 46 | + |
| 47 | + let lint = RenamedLint { |
| 48 | + old_name: format!("clippy::{old_name}"), |
| 49 | + new_name: if uplift { |
| 50 | + new_name.into() |
| 51 | + } else { |
| 52 | + format!("clippy::{new_name}") |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + // Renamed lints and deprecated lints shouldn't have been found in the lint list, but check just in |
| 57 | + // case. |
| 58 | + assert!( |
| 59 | + !renamed_lints.iter().any(|l| lint.old_name == l.old_name), |
| 60 | + "`{old_name}` has already been renamed" |
| 61 | + ); |
| 62 | + assert!( |
| 63 | + !deprecated_lints.iter().any(|l| lint.old_name == l.name), |
| 64 | + "`{old_name}` has already been deprecated" |
| 65 | + ); |
| 66 | + |
| 67 | + // Update all lint level attributes. (`clippy::lint_name`) |
| 68 | + for file in WalkDir::new(".").into_iter().map(Result::unwrap).filter(|f| { |
| 69 | + let name = f.path().file_name(); |
| 70 | + let ext = f.path().extension(); |
| 71 | + (ext == Some(OsStr::new("rs")) || ext == Some(OsStr::new("fixed"))) |
| 72 | + && name != Some(OsStr::new("rename.rs")) |
| 73 | + && name != Some(OsStr::new("deprecated_lints.rs")) |
| 74 | + }) { |
| 75 | + rewrite_file(file.path(), |s| { |
| 76 | + replace_ident_like(s, &[(&lint.old_name, &lint.new_name)]) |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + rewrite_file(Path::new("clippy_lints/src/deprecated_lints.rs"), |s| { |
| 81 | + insert_at_marker( |
| 82 | + s, |
| 83 | + "// end renamed lints. used by `cargo dev rename_lint`", |
| 84 | + &format!( |
| 85 | + "#[clippy::version = \"{}\"]\n \ |
| 86 | + (\"{}\", \"{}\"),\n ", |
| 87 | + clippy_version.rust_display(), |
| 88 | + lint.old_name, |
| 89 | + lint.new_name, |
| 90 | + ), |
| 91 | + ) |
| 92 | + }); |
| 93 | + |
| 94 | + renamed_lints.push(lint); |
| 95 | + renamed_lints.sort_by(|lhs, rhs| { |
| 96 | + lhs.new_name |
| 97 | + .starts_with("clippy::") |
| 98 | + .cmp(&rhs.new_name.starts_with("clippy::")) |
| 99 | + .reverse() |
| 100 | + .then_with(|| lhs.old_name.cmp(&rhs.old_name)) |
| 101 | + }); |
| 102 | + |
| 103 | + if uplift { |
| 104 | + write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints)); |
| 105 | + println!( |
| 106 | + "`{old_name}` has be uplifted. All the code inside `clippy_lints` related to it needs to be removed manually." |
| 107 | + ); |
| 108 | + } else if found_new_name { |
| 109 | + write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints)); |
| 110 | + println!( |
| 111 | + "`{new_name}` is already defined. The old linting code inside `clippy_lints` needs to be updated/removed manually." |
| 112 | + ); |
| 113 | + } else { |
| 114 | + // Rename the lint struct and source files sharing a name with the lint. |
| 115 | + let lint = &mut lints[old_lint_index]; |
| 116 | + let old_name_upper = old_name.to_uppercase(); |
| 117 | + let new_name_upper = new_name.to_uppercase(); |
| 118 | + lint.name = new_name.into(); |
| 119 | + |
| 120 | + // Rename test files. only rename `.stderr` and `.fixed` files if the new test name doesn't exist. |
| 121 | + if try_rename_file( |
| 122 | + Path::new(&format!("tests/ui/{old_name}.rs")), |
| 123 | + Path::new(&format!("tests/ui/{new_name}.rs")), |
| 124 | + ) { |
| 125 | + try_rename_file( |
| 126 | + Path::new(&format!("tests/ui/{old_name}.stderr")), |
| 127 | + Path::new(&format!("tests/ui/{new_name}.stderr")), |
| 128 | + ); |
| 129 | + try_rename_file( |
| 130 | + Path::new(&format!("tests/ui/{old_name}.fixed")), |
| 131 | + Path::new(&format!("tests/ui/{new_name}.fixed")), |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + // Try to rename the file containing the lint if the file name matches the lint's name. |
| 136 | + let replacements; |
| 137 | + let replacements = if lint.module == old_name |
| 138 | + && try_rename_file( |
| 139 | + Path::new(&format!("clippy_lints/src/{old_name}.rs")), |
| 140 | + Path::new(&format!("clippy_lints/src/{new_name}.rs")), |
| 141 | + ) { |
| 142 | + // Edit the module name in the lint list. Note there could be multiple lints. |
| 143 | + for lint in lints.iter_mut().filter(|l| l.module == old_name) { |
| 144 | + lint.module = new_name.into(); |
| 145 | + } |
| 146 | + replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)]; |
| 147 | + replacements.as_slice() |
| 148 | + } else if !lint.module.contains("::") |
| 149 | + // Catch cases like `methods/lint_name.rs` where the lint is stored in `methods/mod.rs` |
| 150 | + && try_rename_file( |
| 151 | + Path::new(&format!("clippy_lints/src/{}/{old_name}.rs", lint.module)), |
| 152 | + Path::new(&format!("clippy_lints/src/{}/{new_name}.rs", lint.module)), |
| 153 | + ) |
| 154 | + { |
| 155 | + // Edit the module name in the lint list. Note there could be multiple lints, or none. |
| 156 | + let renamed_mod = format!("{}::{old_name}", lint.module); |
| 157 | + for lint in lints.iter_mut().filter(|l| l.module == renamed_mod) { |
| 158 | + lint.module = format!("{}::{new_name}", lint.module); |
| 159 | + } |
| 160 | + replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)]; |
| 161 | + replacements.as_slice() |
| 162 | + } else { |
| 163 | + replacements = [(&*old_name_upper, &*new_name_upper), ("", "")]; |
| 164 | + &replacements[0..1] |
| 165 | + }; |
| 166 | + |
| 167 | + // Don't change `clippy_utils/src/renamed_lints.rs` here as it would try to edit the lint being |
| 168 | + // renamed. |
| 169 | + for file in clippy_lints_src_files() { |
| 170 | + if file |
| 171 | + .path() |
| 172 | + .as_os_str() |
| 173 | + .to_str() |
| 174 | + .is_none_or(|x| x["clippy_lints/src/".len()..] != *"deprecated_lints.rs") |
| 175 | + { |
| 176 | + rewrite_file(file.path(), |s| replace_ident_like(s, replacements)); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints); |
| 181 | + println!("{old_name} has been successfully renamed"); |
| 182 | + } |
| 183 | + |
| 184 | + println!("note: `cargo uitest` still needs to be run to update the test results"); |
| 185 | +} |
0 commit comments