|
| 1 | +use crate::update_lints::{ |
| 2 | + DeprecatedLint, DeprecatedLints, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints, |
| 3 | +}; |
| 4 | +use crate::utils::{UpdateMode, Version}; |
| 5 | +use std::ffi::OsStr; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::{fs, io}; |
| 8 | + |
| 9 | +/// Runs the `deprecate` command |
| 10 | +/// |
| 11 | +/// This does the following: |
| 12 | +/// * Adds an entry to `deprecated_lints.rs`. |
| 13 | +/// * Removes the lint declaration (and the entire file if applicable) |
| 14 | +/// |
| 15 | +/// # Panics |
| 16 | +/// |
| 17 | +/// If a file path could not read from or written to |
| 18 | +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..]; |
| 25 | + |
| 26 | + 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 { |
| 37 | + eprintln!("error: failed to find lint `{name}`"); |
| 38 | + return; |
| 39 | + }; |
| 40 | + |
| 41 | + let mod_path = { |
| 42 | + let mut mod_path = PathBuf::from(format!("clippy_lints/src/{}", lint.module)); |
| 43 | + if mod_path.is_dir() { |
| 44 | + mod_path = mod_path.join("mod"); |
| 45 | + } |
| 46 | + |
| 47 | + mod_path.set_extension("rs"); |
| 48 | + mod_path |
| 49 | + }; |
| 50 | + |
| 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 | + |
| 69 | + generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints); |
| 70 | + println!("info: `{name}` has successfully been deprecated"); |
| 71 | + println!("note: you must run `cargo uitest` to update the test results"); |
| 72 | + } else { |
| 73 | + eprintln!("error: lint not found"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn remove_lint_declaration(name: &str, path: &Path, lints: &mut Vec<Lint>) -> io::Result<bool> { |
| 78 | + fn remove_lint(name: &str, lints: &mut Vec<Lint>) { |
| 79 | + lints.iter().position(|l| l.name == name).map(|pos| lints.remove(pos)); |
| 80 | + } |
| 81 | + |
| 82 | + fn remove_test_assets(name: &str) { |
| 83 | + let test_file_stem = format!("tests/ui/{name}"); |
| 84 | + let path = Path::new(&test_file_stem); |
| 85 | + |
| 86 | + // Some lints have their own directories, delete them |
| 87 | + if path.is_dir() { |
| 88 | + let _ = fs::remove_dir_all(path); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // Remove all related test files |
| 93 | + let _ = fs::remove_file(path.with_extension("rs")); |
| 94 | + let _ = fs::remove_file(path.with_extension("stderr")); |
| 95 | + let _ = fs::remove_file(path.with_extension("fixed")); |
| 96 | + } |
| 97 | + |
| 98 | + fn remove_impl_lint_pass(lint_name_upper: &str, content: &mut String) { |
| 99 | + let impl_lint_pass_start = content.find("impl_lint_pass!").unwrap_or_else(|| { |
| 100 | + content |
| 101 | + .find("declare_lint_pass!") |
| 102 | + .unwrap_or_else(|| panic!("failed to find `impl_lint_pass`")) |
| 103 | + }); |
| 104 | + let mut impl_lint_pass_end = content[impl_lint_pass_start..] |
| 105 | + .find(']') |
| 106 | + .expect("failed to find `impl_lint_pass` terminator"); |
| 107 | + |
| 108 | + impl_lint_pass_end += impl_lint_pass_start; |
| 109 | + if let Some(lint_name_pos) = content[impl_lint_pass_start..impl_lint_pass_end].find(lint_name_upper) { |
| 110 | + let mut lint_name_end = impl_lint_pass_start + (lint_name_pos + lint_name_upper.len()); |
| 111 | + for c in content[lint_name_end..impl_lint_pass_end].chars() { |
| 112 | + // Remove trailing whitespace |
| 113 | + if c == ',' || c.is_whitespace() { |
| 114 | + lint_name_end += 1; |
| 115 | + } else { |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + content.replace_range(impl_lint_pass_start + lint_name_pos..lint_name_end, ""); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if path.exists() |
| 125 | + && let Some(lint) = lints.iter().find(|l| l.name == name) |
| 126 | + { |
| 127 | + if lint.module == name { |
| 128 | + // The lint name is the same as the file, we can just delete the entire file |
| 129 | + fs::remove_file(path)?; |
| 130 | + } else { |
| 131 | + // We can't delete the entire file, just remove the declaration |
| 132 | + |
| 133 | + if let Some(Some("mod.rs")) = path.file_name().map(OsStr::to_str) { |
| 134 | + // Remove clippy_lints/src/some_mod/some_lint.rs |
| 135 | + let mut lint_mod_path = path.to_path_buf(); |
| 136 | + lint_mod_path.set_file_name(name); |
| 137 | + lint_mod_path.set_extension("rs"); |
| 138 | + |
| 139 | + let _ = fs::remove_file(lint_mod_path); |
| 140 | + } |
| 141 | + |
| 142 | + let mut content = |
| 143 | + fs::read_to_string(path).unwrap_or_else(|_| panic!("failed to read `{}`", path.to_string_lossy())); |
| 144 | + |
| 145 | + eprintln!( |
| 146 | + "warn: you will have to manually remove any code related to `{name}` from `{}`", |
| 147 | + path.display() |
| 148 | + ); |
| 149 | + |
| 150 | + assert!( |
| 151 | + content[lint.declaration_range.clone()].contains(&name.to_uppercase()), |
| 152 | + "error: `{}` does not contain lint `{}`'s declaration", |
| 153 | + path.display(), |
| 154 | + lint.name |
| 155 | + ); |
| 156 | + |
| 157 | + // Remove lint declaration (declare_clippy_lint!) |
| 158 | + content.replace_range(lint.declaration_range.clone(), ""); |
| 159 | + |
| 160 | + // Remove the module declaration (mod xyz;) |
| 161 | + let mod_decl = format!("\nmod {name};"); |
| 162 | + content = content.replacen(&mod_decl, "", 1); |
| 163 | + |
| 164 | + remove_impl_lint_pass(&lint.name.to_uppercase(), &mut content); |
| 165 | + fs::write(path, content).unwrap_or_else(|_| panic!("failed to write to `{}`", path.to_string_lossy())); |
| 166 | + } |
| 167 | + |
| 168 | + remove_test_assets(name); |
| 169 | + remove_lint(name, lints); |
| 170 | + return Ok(true); |
| 171 | + } |
| 172 | + |
| 173 | + Ok(false) |
| 174 | +} |
0 commit comments