Skip to content

Commit 0eb8a65

Browse files
committed
clippy_dev: When renaming a lint better handle the case where the renamed lint is a prefix of another lint name.
1 parent 53ca7d8 commit 0eb8a65

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

clippy_dev/src/edit_lints.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ pub fn rename<'cx, 'env: 'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_nam
173173
},
174174
}
175175

176-
// Some tests are named `lint_name_suffix` which should also be renamed,
177-
// but we can't do that if the renamed lint's name overlaps with another
178-
// lint. e.g. renaming 'foo' to 'bar' when a lint 'foo_bar' also exists.
179-
let change_prefixed_tests = lints.get(lint_idx + 1).is_none_or(|l| !l.name.starts_with(old_name));
180-
181176
let mut mod_edit = ModEdit::None;
182177
if lints.binary_search_by(|x| x.name.cmp(new_name)).is_err() {
183178
let lint = &mut lints[lint_idx];
@@ -199,7 +194,16 @@ pub fn rename<'cx, 'env: 'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_nam
199194
cx.arena.alloc_str(buf)
200195
});
201196
}
202-
rename_test_files(old_name, new_name, change_prefixed_tests);
197+
198+
rename_test_files(
199+
old_name,
200+
new_name,
201+
&lints[lint_idx + 1..]
202+
.iter()
203+
.map(|l| l.name)
204+
.take_while(|&n| n.starts_with(old_name))
205+
.collect::<Vec<_>>(),
206+
);
203207
lints[lint_idx].name = new_name;
204208
lints.sort_by(|lhs, rhs| lhs.name.cmp(rhs.name));
205209
} else {
@@ -329,44 +333,45 @@ enum ModEdit {
329333
Rename,
330334
}
331335

332-
fn collect_ui_test_names(lint: &str, rename_prefixed: bool, dst: &mut Vec<(OsString, bool)>) {
336+
fn collect_ui_test_names(lint: &str, ignored_prefixes: &[&str], dst: &mut Vec<(OsString, bool)>) {
333337
for e in fs::read_dir("tests/ui").expect("error reading `tests/ui`") {
334338
let e = e.expect("error reading `tests/ui`");
335339
let name = e.file_name();
336-
if let Some((name_only, _)) = name.as_encoded_bytes().split_once(|&x| x == b'.') {
337-
if name_only.starts_with(lint.as_bytes()) && (rename_prefixed || name_only.len() == lint.len()) {
338-
dst.push((name, true));
339-
}
340-
} else if name.as_encoded_bytes().starts_with(lint.as_bytes()) && (rename_prefixed || name.len() == lint.len())
340+
if name.as_encoded_bytes().starts_with(lint.as_bytes())
341+
&& !ignored_prefixes
342+
.iter()
343+
.any(|&pre| name.as_encoded_bytes().starts_with(pre.as_bytes()))
344+
&& let Ok(ty) = e.file_type()
345+
&& (ty.is_file() || ty.is_dir())
341346
{
342-
dst.push((name, false));
347+
dst.push((name, ty.is_file()));
343348
}
344349
}
345350
}
346351

347-
fn collect_ui_toml_test_names(lint: &str, rename_prefixed: bool, dst: &mut Vec<(OsString, bool)>) {
348-
if rename_prefixed {
349-
for e in fs::read_dir("tests/ui-toml").expect("error reading `tests/ui-toml`") {
350-
let e = e.expect("error reading `tests/ui-toml`");
351-
let name = e.file_name();
352-
if name.as_encoded_bytes().starts_with(lint.as_bytes()) && e.file_type().is_ok_and(|ty| ty.is_dir()) {
353-
dst.push((name, false));
354-
}
352+
fn collect_ui_toml_test_names(lint: &str, ignored_prefixes: &[&str], dst: &mut Vec<(OsString, bool)>) {
353+
for e in fs::read_dir("tests/ui-toml").expect("error reading `tests/ui-toml`") {
354+
let e = e.expect("error reading `tests/ui-toml`");
355+
let name = e.file_name();
356+
if name.as_encoded_bytes().starts_with(lint.as_bytes())
357+
&& !ignored_prefixes
358+
.iter()
359+
.any(|&pre| name.as_encoded_bytes().starts_with(pre.as_bytes()))
360+
&& e.file_type().is_ok_and(|ty| ty.is_dir())
361+
{
362+
dst.push((name, false));
355363
}
356-
} else {
357-
dst.push((lint.into(), false));
358364
}
359365
}
360366

361-
/// Renames all test files for the given lint.
362-
///
363-
/// If `rename_prefixed` is `true` this will also rename tests which have the lint name as a prefix.
364-
fn rename_test_files(old_name: &str, new_name: &str, rename_prefixed: bool) {
365-
let mut tests = Vec::new();
367+
/// Renames all test files for the given lint where the file name does not start with any
368+
/// of the given prefixes.
369+
fn rename_test_files(old_name: &str, new_name: &str, ignored_prefixes: &[&str]) {
370+
let mut tests: Vec<(OsString, bool)> = Vec::new();
366371

367372
let mut old_buf = OsString::from("tests/ui/");
368373
let mut new_buf = OsString::from("tests/ui/");
369-
collect_ui_test_names(old_name, rename_prefixed, &mut tests);
374+
collect_ui_test_names(old_name, ignored_prefixes, &mut tests);
370375
for &(ref name, is_file) in &tests {
371376
old_buf.push(name);
372377
new_buf.extend([new_name.as_ref(), name.slice_encoded_bytes(old_name.len()..)]);
@@ -384,7 +389,7 @@ fn rename_test_files(old_name: &str, new_name: &str, rename_prefixed: bool) {
384389
new_buf.truncate("tests/ui".len());
385390
old_buf.push("-toml/");
386391
new_buf.push("-toml/");
387-
collect_ui_toml_test_names(old_name, rename_prefixed, &mut tests);
392+
collect_ui_toml_test_names(old_name, ignored_prefixes, &mut tests);
388393
for (name, _) in &tests {
389394
old_buf.push(name);
390395
new_buf.extend([new_name.as_ref(), name.slice_encoded_bytes(old_name.len()..)]);
@@ -394,11 +399,13 @@ fn rename_test_files(old_name: &str, new_name: &str, rename_prefixed: bool) {
394399
}
395400
}
396401

397-
fn delete_test_files(lint: &str, rename_prefixed: bool) {
402+
/// Deletes all test files for the given lint where the file name does not start with any
403+
/// of the given prefixes.
404+
fn delete_test_files(lint: &str, ignored_prefixes: &[&str]) {
398405
let mut tests = Vec::new();
399406

400407
let mut buf = OsString::from("tests/ui/");
401-
collect_ui_test_names(lint, rename_prefixed, &mut tests);
408+
collect_ui_test_names(lint, ignored_prefixes, &mut tests);
402409
for &(ref name, is_file) in &tests {
403410
buf.push(name);
404411
if is_file {
@@ -413,7 +420,7 @@ fn delete_test_files(lint: &str, rename_prefixed: bool) {
413420
buf.push("-toml/");
414421

415422
tests.clear();
416-
collect_ui_toml_test_names(lint, rename_prefixed, &mut tests);
423+
collect_ui_toml_test_names(lint, ignored_prefixes, &mut tests);
417424
for (name, _) in &tests {
418425
buf.push(name);
419426
delete_dir_if_exists(buf.as_ref());

0 commit comments

Comments
 (0)