Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions clippy_dev/src/edit_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use core::mem;
use rustc_lexer::TokenKind;
use std::collections::hash_map::Entry;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::{fs, path};

/// Runs the `deprecate` command
///
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn rename<'cx, 'env: 'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_nam
eprintln!("error: failed to find lint `{old_name}`");
return;
};
let Lint::Active(mut prev_lint) = mem::replace(
let Lint::Active(prev_lint) = mem::replace(
lint.get_mut(),
Lint::Renamed(RenamedLint {
new_name: LintName::new_clippy(new_name),
Expand All @@ -116,26 +116,19 @@ pub fn rename<'cx, 'env: 'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_nam

let mut rename_mod = false;
if let Entry::Vacant(e) = data.lints.entry(new_name) {
if prev_lint.module.ends_with(old_name)
&& prev_lint
.path
.file_stem()
.is_some_and(|x| x.as_encoded_bytes() == old_name.as_bytes())
if let Some((path, file)) = prev_lint.file.path.get().rsplit_once(path::MAIN_SEPARATOR)
&& let Some(file) = file.strip_suffix(".rs")
&& file == old_name
{
let mut new_path = prev_lint.path.with_file_name(new_name).into_os_string();
new_path.push(".rs");
if try_rename_file(prev_lint.path.as_ref(), new_path.as_ref()) {
let new_path = cx
.str_buf
.alloc_display(cx.arena, format_args!("{path}{}{new_name}.rs", path::MAIN_SEPARATOR));
if try_rename_file(prev_lint.file.path.get(), new_path) {
rename_mod = true;
prev_lint.file.path.set(new_path);
}

prev_lint.module = cx.str_buf.with(|buf| {
buf.push_str(&prev_lint.module[..prev_lint.module.len() - old_name.len()]);
buf.push_str(new_name);
cx.arena.alloc_str(buf)
});
}
e.insert(Lint::Active(prev_lint));

rename_test_files(old_name, new_name, &create_ignored_prefixes(old_name, &data));
} else {
println!("Renamed `{old_name}` to `{new_name}`");
Expand Down Expand Up @@ -163,14 +156,14 @@ pub fn rename<'cx, 'env: 'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_nam
fn remove_lint_declaration(name: &str, lint: &ActiveLint<'_>, data: &LintData<'_>, updater: &mut FileUpdater) -> bool {
let delete_mod = if data.lints.iter().all(|(_, l)| {
if let Lint::Active(l) = l {
l.module != lint.module
l.file != lint.file
} else {
true
}
}) {
delete_file_if_exists(lint.path.as_ref())
delete_file_if_exists(lint.file.path.get())
} else {
updater.update_file(&lint.path, &mut |_, src, dst| -> UpdateStatus {
updater.update_file(lint.file.path.get(), &mut |_, src, dst| -> UpdateStatus {
let mut start = &src[..lint.declaration_range.start as usize];
if start.ends_with("\n\n") {
start = &start[..start.len() - 1];
Expand Down Expand Up @@ -257,9 +250,9 @@ fn rename_test_files(old_name: &str, new_name: &str, ignored_prefixes: &[&str])
old_buf.push(name);
new_buf.extend([new_name.as_ref(), name.slice_encoded_bytes(old_name.len()..)]);
if is_file {
try_rename_file(old_buf.as_ref(), new_buf.as_ref());
try_rename_file(&old_buf, &new_buf);
} else {
try_rename_dir(old_buf.as_ref(), new_buf.as_ref());
try_rename_dir(&old_buf, &new_buf);
}
old_buf.truncate("tests/ui/".len());
new_buf.truncate("tests/ui/".len());
Expand All @@ -274,7 +267,7 @@ fn rename_test_files(old_name: &str, new_name: &str, ignored_prefixes: &[&str])
for (name, _) in &tests {
old_buf.push(name);
new_buf.extend([new_name.as_ref(), name.slice_encoded_bytes(old_name.len()..)]);
try_rename_dir(old_buf.as_ref(), new_buf.as_ref());
try_rename_dir(&old_buf, &new_buf);
old_buf.truncate("tests/ui/".len());
new_buf.truncate("tests/ui/".len());
}
Expand All @@ -290,9 +283,9 @@ fn delete_test_files(lint: &str, ignored_prefixes: &[&str]) {
for &(ref name, is_file) in &tests {
buf.push(name);
if is_file {
delete_file_if_exists(buf.as_ref());
delete_file_if_exists(&buf);
} else {
delete_dir_if_exists(buf.as_ref());
delete_dir_if_exists(&buf);
}
buf.truncate("tests/ui/".len());
}
Expand All @@ -304,7 +297,7 @@ fn delete_test_files(lint: &str, ignored_prefixes: &[&str]) {
collect_ui_toml_test_names(lint, ignored_prefixes, &mut tests);
for (name, _) in &tests {
buf.push(name);
delete_dir_if_exists(buf.as_ref());
delete_dir_if_exists(&buf);
buf.truncate("tests/ui/".len());
}
}
Expand Down
19 changes: 10 additions & 9 deletions clippy_dev/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,23 @@ pub fn run(update_mode: UpdateMode) {

new_parse_cx(|cx| {
let mut data = cx.parse_lint_decls();
let (mut lints, passes) = data.split_by_lint_file();

let mut updater = FileUpdater::default();
let mut ranges = VecBuf::with_capacity(256);

for passes in passes {
let path = passes[0].path.clone();
let mut lints = lints.remove(&*path);
#[expect(clippy::mutable_key_type)]
let mut lints = data.mk_file_to_lint_decl_map();
let mut ranges = VecBuf::with_capacity(256);
for passes in data.iter_passes_by_file_mut() {
let file = passes[0].file;
let mut lints = lints.remove(file);
let lints = lints.as_deref_mut().unwrap_or_default();
updater.update_file_checked("cargo dev fmt", update_mode, &path, &mut |_, src, dst| {
updater.update_loaded_file_checked("cargo dev fmt", update_mode, file, &mut |_, src, dst| {
gen_sorted_lints_file(src, dst, lints, passes, &mut ranges);
UpdateStatus::from_changed(src != dst)
});
}

for (&path, lints) in &mut lints {
updater.update_file_checked("cargo dev fmt", update_mode, path, &mut |_, src, dst| {
for (&file, lints) in &mut lints {
updater.update_loaded_file_checked("cargo dev fmt", update_mode, file, &mut |_, src, dst| {
gen_sorted_lints_file(src, dst, lints, &mut [], &mut ranges);
UpdateStatus::from_changed(src != dst)
});
Expand Down
50 changes: 24 additions & 26 deletions clippy_dev/src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parse::cursor::Cursor;
use crate::parse::{Lint, LintData, LintPass, VecBuf};
use crate::utils::{FileUpdater, UpdateMode, UpdateStatus, update_text_region_fn};
use crate::utils::{FileUpdater, UpdateMode, UpdateStatus, slice_groups, update_text_region_fn};
use core::range::Range;
use itertools::Itertools;
use std::collections::HashSet;
Expand Down Expand Up @@ -40,12 +40,12 @@ impl LintData<'_> {
let mut renamed = Vec::with_capacity(lints.len() / 8);
for &(name, lint) in &lints {
match lint {
Lint::Active(lint) => active.push((name, lint)),
Lint::Active(lint) => active.push((name, lint.file.path_as_krate_mod())),
Lint::Deprecated(lint) => deprecated.push((name, lint)),
Lint::Renamed(lint) => renamed.push((name, lint)),
}
}
active.sort_by_key(|&(_, lint)| lint.module);
active.sort_by_key(|&(_, path)| path);

// Round to avoid updating the readme every time a lint is added/deprecated.
let lint_count = active.len() / 50 * 50;
Expand All @@ -66,10 +66,10 @@ impl LintData<'_> {
}),
);

updater.update_file_checked(
updater.update_loaded_file_checked(
"cargo dev update_lints",
update_mode,
"clippy_lints/src/deprecated_lints.rs",
self.deprecated_file,
&mut |_, src, dst| {
let mut cursor = Cursor::new(src);
assert!(
Expand Down Expand Up @@ -138,29 +138,25 @@ impl LintData<'_> {
UpdateStatus::from_changed(src != dst)
},
);
for (crate_name, lints) in active.iter().copied().into_group_map_by(|&(_, lint)| {
let Some(path::Component::Normal(name)) = lint.path.components().next() else {
// All paths should start with `{crate_name}/src` when parsed from `find_lint_decls`
panic!(
"internal error: can't read crate name from path `{}`",
lint.path.display()
);
};
name

for lints in slice_groups(&active, |(_, (head, _)), tail| {
tail.iter().take_while(|(_, (x, _))| head == x).count()
}) {
let (_, (krate, _)) = lints[0];
updater.update_file_checked(
"cargo dev update_lints",
update_mode,
Path::new(crate_name).join("src/lib.rs"),
Path::new(krate).join("src/lib.rs"),
&mut update_text_region_fn(
"// begin lints modules, do not remove this comment, it's used in `update_lints`\n",
"// end lints modules, do not remove this comment, it's used in `update_lints`",
|dst| {
let mut prev = "";
for &(_, lint) in &lints {
if lint.module != prev {
writeln!(dst, "mod {};", lint.module).unwrap();
prev = lint.module;
for &(_, (_, mod_path)) in lints {
let module = mod_path.split_once(path::MAIN_SEPARATOR).map_or(mod_path, |(x, _)| x);
if module != prev {
writeln!(dst, "mod {module};").unwrap();
prev = module;
}
}
},
Expand All @@ -169,20 +165,21 @@ impl LintData<'_> {
updater.update_file_checked(
"cargo dev update_lints",
update_mode,
Path::new(crate_name).join("src/declared_lints.rs"),
Path::new(krate).join("src/declared_lints.rs"),
&mut |_, src, dst| {
dst.push_str(GENERATED_FILE_COMMENT);
dst.push_str("pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[\n");
let mut buf = String::new();
for &(name, lint) in &lints {
for &(name, (_, mod_path)) in lints {
dst.push_str(" crate::");
for part in mod_path.split(path::MAIN_SEPARATOR) {
dst.push_str(part);
dst.push_str("::");
}
buf.clear();
buf.push_str(name);
buf.make_ascii_uppercase();
if lint.module.is_empty() {
writeln!(dst, " crate::{buf}_INFO,").unwrap();
} else {
writeln!(dst, " crate::{}::{buf}_INFO,", lint.module).unwrap();
}
let _ = writeln!(dst, "{buf}_INFO,");
}
dst.push_str("];\n");
UpdateStatus::from_changed(src != dst)
Expand Down Expand Up @@ -283,6 +280,7 @@ pub fn gen_sorted_lints_file(
dst.push_str("\n\n");
}
for pass in passes {
pass.lints.sort_unstable();
pass.gen_mac(dst);
dst.push_str("\n\n");
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
unused_lifetimes,
unused_qualifications
)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::case_sensitive_file_extension_comparisons, clippy::missing_panics_doc)]

extern crate rustc_arena;
extern crate rustc_data_structures;
Expand Down
Loading
Loading