about summary refs log tree commit diff
path: root/clippy_dev
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2025-05-15 19:28:39 +0200
committerPhilipp Krones <hello@philkrones.com>2025-05-15 19:28:39 +0200
commit93bd4d893122417b9265563c037f11a158a8e37c (patch)
tree1e09642005b908b1638e385ca8ceec1dfc9e9e54 /clippy_dev
parent303c4ecfdd0c571d80c5bc151243aee1900cebfd (diff)
downloadrust-93bd4d893122417b9265563c037f11a158a8e37c.tar.gz
rust-93bd4d893122417b9265563c037f11a158a8e37c.zip
Merge commit '0450db33a5d8587f7c1d4b6d233dac963605766b' into clippy-subtree-update
Diffstat (limited to 'clippy_dev')
-rw-r--r--clippy_dev/src/deprecate_lint.rs174
-rw-r--r--clippy_dev/src/dogfood.rs5
-rw-r--r--clippy_dev/src/fmt.rs32
-rw-r--r--clippy_dev/src/lib.rs5
-rw-r--r--clippy_dev/src/main.rs37
-rw-r--r--clippy_dev/src/new_lint.rs165
-rw-r--r--clippy_dev/src/release.rs32
-rw-r--r--clippy_dev/src/rename_lint.rs194
-rw-r--r--clippy_dev/src/sync.rs31
-rw-r--r--clippy_dev/src/update_lints.rs1110
-rw-r--r--clippy_dev/src/utils.rs650
11 files changed, 1337 insertions, 1098 deletions
diff --git a/clippy_dev/src/deprecate_lint.rs b/clippy_dev/src/deprecate_lint.rs
new file mode 100644
index 00000000000..bf0e7771046
--- /dev/null
+++ b/clippy_dev/src/deprecate_lint.rs
@@ -0,0 +1,174 @@
+use crate::update_lints::{
+    DeprecatedLint, DeprecatedLints, Lint, find_lint_decls, generate_lint_files, read_deprecated_lints,
+};
+use crate::utils::{UpdateMode, Version};
+use std::ffi::OsStr;
+use std::path::{Path, PathBuf};
+use std::{fs, io};
+
+/// Runs the `deprecate` command
+///
+/// This does the following:
+/// * Adds an entry to `deprecated_lints.rs`.
+/// * Removes the lint declaration (and the entire file if applicable)
+///
+/// # Panics
+///
+/// If a file path could not read from or written to
+pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
+    let prefixed_name = if name.starts_with("clippy::") {
+        name.to_owned()
+    } else {
+        format!("clippy::{name}")
+    };
+    let stripped_name = &prefixed_name[8..];
+
+    let mut lints = find_lint_decls();
+    let DeprecatedLints {
+        renamed: renamed_lints,
+        deprecated: mut deprecated_lints,
+        file: mut deprecated_file,
+        contents: mut deprecated_contents,
+        deprecated_end,
+        ..
+    } = read_deprecated_lints();
+
+    let Some(lint) = lints.iter().find(|l| l.name == stripped_name) else {
+        eprintln!("error: failed to find lint `{name}`");
+        return;
+    };
+
+    let mod_path = {
+        let mut mod_path = PathBuf::from(format!("clippy_lints/src/{}", lint.module));
+        if mod_path.is_dir() {
+            mod_path = mod_path.join("mod");
+        }
+
+        mod_path.set_extension("rs");
+        mod_path
+    };
+
+    if remove_lint_declaration(stripped_name, &mod_path, &mut lints).unwrap_or(false) {
+        deprecated_contents.insert_str(
+            deprecated_end as usize,
+            &format!(
+                "    #[clippy::version = \"{}\"]\n    (\"{}\", \"{}\"),\n",
+                clippy_version.rust_display(),
+                prefixed_name,
+                reason,
+            ),
+        );
+        deprecated_file.replace_contents(deprecated_contents.as_bytes());
+        drop(deprecated_file);
+
+        deprecated_lints.push(DeprecatedLint {
+            name: prefixed_name,
+            reason: reason.into(),
+        });
+
+        generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
+        println!("info: `{name}` has successfully been deprecated");
+        println!("note: you must run `cargo uitest` to update the test results");
+    } else {
+        eprintln!("error: lint not found");
+    }
+}
+
+fn remove_lint_declaration(name: &str, path: &Path, lints: &mut Vec<Lint>) -> io::Result<bool> {
+    fn remove_lint(name: &str, lints: &mut Vec<Lint>) {
+        lints.iter().position(|l| l.name == name).map(|pos| lints.remove(pos));
+    }
+
+    fn remove_test_assets(name: &str) {
+        let test_file_stem = format!("tests/ui/{name}");
+        let path = Path::new(&test_file_stem);
+
+        // Some lints have their own directories, delete them
+        if path.is_dir() {
+            let _ = fs::remove_dir_all(path);
+            return;
+        }
+
+        // Remove all related test files
+        let _ = fs::remove_file(path.with_extension("rs"));
+        let _ = fs::remove_file(path.with_extension("stderr"));
+        let _ = fs::remove_file(path.with_extension("fixed"));
+    }
+
+    fn remove_impl_lint_pass(lint_name_upper: &str, content: &mut String) {
+        let impl_lint_pass_start = content.find("impl_lint_pass!").unwrap_or_else(|| {
+            content
+                .find("declare_lint_pass!")
+                .unwrap_or_else(|| panic!("failed to find `impl_lint_pass`"))
+        });
+        let mut impl_lint_pass_end = content[impl_lint_pass_start..]
+            .find(']')
+            .expect("failed to find `impl_lint_pass` terminator");
+
+        impl_lint_pass_end += impl_lint_pass_start;
+        if let Some(lint_name_pos) = content[impl_lint_pass_start..impl_lint_pass_end].find(lint_name_upper) {
+            let mut lint_name_end = impl_lint_pass_start + (lint_name_pos + lint_name_upper.len());
+            for c in content[lint_name_end..impl_lint_pass_end].chars() {
+                // Remove trailing whitespace
+                if c == ',' || c.is_whitespace() {
+                    lint_name_end += 1;
+                } else {
+                    break;
+                }
+            }
+
+            content.replace_range(impl_lint_pass_start + lint_name_pos..lint_name_end, "");
+        }
+    }
+
+    if path.exists()
+        && let Some(lint) = lints.iter().find(|l| l.name == name)
+    {
+        if lint.module == name {
+            // The lint name is the same as the file, we can just delete the entire file
+            fs::remove_file(path)?;
+        } else {
+            // We can't delete the entire file, just remove the declaration
+
+            if let Some(Some("mod.rs")) = path.file_name().map(OsStr::to_str) {
+                // Remove clippy_lints/src/some_mod/some_lint.rs
+                let mut lint_mod_path = path.to_path_buf();
+                lint_mod_path.set_file_name(name);
+                lint_mod_path.set_extension("rs");
+
+                let _ = fs::remove_file(lint_mod_path);
+            }
+
+            let mut content =
+                fs::read_to_string(path).unwrap_or_else(|_| panic!("failed to read `{}`", path.to_string_lossy()));
+
+            eprintln!(
+                "warn: you will have to manually remove any code related to `{name}` from `{}`",
+                path.display()
+            );
+
+            assert!(
+                content[lint.declaration_range.clone()].contains(&name.to_uppercase()),
+                "error: `{}` does not contain lint `{}`'s declaration",
+                path.display(),
+                lint.name
+            );
+
+            // Remove lint declaration (declare_clippy_lint!)
+            content.replace_range(lint.declaration_range.clone(), "");
+
+            // Remove the module declaration (mod xyz;)
+            let mod_decl = format!("\nmod {name};");
+            content = content.replacen(&mod_decl, "", 1);
+
+            remove_impl_lint_pass(&lint.name.to_uppercase(), &mut content);
+            fs::write(path, content).unwrap_or_else(|_| panic!("failed to write to `{}`", path.to_string_lossy()));
+        }
+
+        remove_test_assets(name);
+        remove_lint(name, lints);
+        return Ok(true);
+    }
+
+    Ok(false)
+}
diff --git a/clippy_dev/src/dogfood.rs b/clippy_dev/src/dogfood.rs
index 05fa24d8d4e..7e9d92458d0 100644
--- a/clippy_dev/src/dogfood.rs
+++ b/clippy_dev/src/dogfood.rs
@@ -1,4 +1,4 @@
-use crate::utils::{clippy_project_root, exit_if_err};
+use crate::utils::exit_if_err;
 use std::process::Command;
 
 /// # Panics
@@ -8,8 +8,7 @@ use std::process::Command;
 pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) {
     let mut cmd = Command::new("cargo");
 
-    cmd.current_dir(clippy_project_root())
-        .args(["test", "--test", "dogfood"])
+    cmd.args(["test", "--test", "dogfood"])
         .args(["--features", "internal"])
         .args(["--", "dogfood_clippy", "--nocapture"]);
 
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index bdddf46a2cb..b4c13213f55 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -1,4 +1,3 @@
-use crate::utils::clippy_project_root;
 use itertools::Itertools;
 use rustc_lexer::{TokenKind, tokenize};
 use shell_escape::escape;
@@ -104,15 +103,8 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
         Field,
     }
 
-    let path: PathBuf = [
-        clippy_project_root().as_path(),
-        "clippy_config".as_ref(),
-        "src".as_ref(),
-        "conf.rs".as_ref(),
-    ]
-    .into_iter()
-    .collect();
-    let text = fs::read_to_string(&path)?;
+    let path = "clippy_config/src/conf.rs";
+    let text = fs::read_to_string(path)?;
 
     let (pre, conf) = text
         .split_once("define_Conf! {\n")
@@ -203,7 +195,7 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
             | (State::Lints, TokenKind::Comma | TokenKind::OpenParen | TokenKind::CloseParen) => {},
             _ => {
                 return Err(Error::Parse(
-                    path,
+                    PathBuf::from(path),
                     offset_to_line(&text, conf_offset + i),
                     format!("unexpected token `{}`", &conf[i..i + t.len as usize]),
                 ));
@@ -213,7 +205,7 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
 
     if !matches!(state, State::Field) {
         return Err(Error::Parse(
-            path,
+            PathBuf::from(path),
             offset_to_line(&text, conf_offset + conf.len()),
             "incomplete field".into(),
         ));
@@ -260,18 +252,16 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
         if check {
             return Err(Error::CheckFailed);
         }
-        fs::write(&path, new_text.as_bytes())?;
+        fs::write(path, new_text.as_bytes())?;
     }
     Ok(())
 }
 
 fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
-    let project_root = clippy_project_root();
-
     // if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
     // format because rustfmt would also format the entire rustc repo as it is a local
     // dependency
-    if fs::read_to_string(project_root.join("Cargo.toml"))
+    if fs::read_to_string("Cargo.toml")
         .expect("Failed to read clippy Cargo.toml")
         .contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
     {
@@ -280,12 +270,12 @@ fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
 
     check_for_rustfmt(context)?;
 
-    cargo_fmt(context, project_root.as_path())?;
-    cargo_fmt(context, &project_root.join("clippy_dev"))?;
-    cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
-    cargo_fmt(context, &project_root.join("lintcheck"))?;
+    cargo_fmt(context, ".".as_ref())?;
+    cargo_fmt(context, "clippy_dev".as_ref())?;
+    cargo_fmt(context, "rustc_tools_util".as_ref())?;
+    cargo_fmt(context, "lintcheck".as_ref())?;
 
-    let chunks = WalkDir::new(project_root.join("tests"))
+    let chunks = WalkDir::new("tests")
         .into_iter()
         .filter_map(|entry| {
             let entry = entry.expect("failed to find tests");
diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs
index c1ffaf269c6..e237a05b253 100644
--- a/clippy_dev/src/lib.rs
+++ b/clippy_dev/src/lib.rs
@@ -1,5 +1,4 @@
-#![feature(let_chains)]
-#![feature(rustc_private)]
+#![feature(rustc_private, if_let_guard, let_chains)]
 #![warn(
     trivial_casts,
     trivial_numeric_casts,
@@ -15,11 +14,13 @@ extern crate rustc_driver;
 extern crate rustc_lexer;
 extern crate rustc_literal_escaper;
 
+pub mod deprecate_lint;
 pub mod dogfood;
 pub mod fmt;
 pub mod lint;
 pub mod new_lint;
 pub mod release;
+pub mod rename_lint;
 pub mod serve;
 pub mod setup;
 pub mod sync;
diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs
index 83f8e66b334..5dce0be742b 100644
--- a/clippy_dev/src/main.rs
+++ b/clippy_dev/src/main.rs
@@ -3,11 +3,18 @@
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
 use clap::{Args, Parser, Subcommand};
-use clippy_dev::{dogfood, fmt, lint, new_lint, release, serve, setup, sync, update_lints, utils};
+use clippy_dev::{
+    deprecate_lint, dogfood, fmt, lint, new_lint, release, rename_lint, serve, setup, sync, update_lints, utils,
+};
 use std::convert::Infallible;
+use std::env;
 
 fn main() {
     let dev = Dev::parse();
+    let clippy = utils::ClippyInfo::search_for_manifest();
+    if let Err(e) = env::set_current_dir(&clippy.path) {
+        panic!("error setting current directory to `{}`: {e}", clippy.path.display());
+    }
 
     match dev.command {
         DevCommand::Bless => {
@@ -20,22 +27,14 @@ fn main() {
             allow_no_vcs,
         } => dogfood::dogfood(fix, allow_dirty, allow_staged, allow_no_vcs),
         DevCommand::Fmt { check, verbose } => fmt::run(check, verbose),
-        DevCommand::UpdateLints { print_only, check } => {
-            if print_only {
-                update_lints::print_lints();
-            } else if check {
-                update_lints::update(utils::UpdateMode::Check);
-            } else {
-                update_lints::update(utils::UpdateMode::Change);
-            }
-        },
+        DevCommand::UpdateLints { check } => update_lints::update(utils::UpdateMode::from_check(check)),
         DevCommand::NewLint {
             pass,
             name,
             category,
             r#type,
             msrv,
-        } => match new_lint::create(pass, &name, &category, r#type.as_deref(), msrv) {
+        } => match new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv) {
             Ok(()) => update_lints::update(utils::UpdateMode::Change),
             Err(e) => eprintln!("Unable to create lint: {e}"),
         },
@@ -79,13 +78,18 @@ fn main() {
             old_name,
             new_name,
             uplift,
-        } => update_lints::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
-        DevCommand::Deprecate { name, reason } => update_lints::deprecate(&name, &reason),
+        } => rename_lint::rename(
+            clippy.version,
+            &old_name,
+            new_name.as_ref().unwrap_or(&old_name),
+            uplift,
+        ),
+        DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(clippy.version, &name, &reason),
         DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
             SyncSubcommand::UpdateNightly => sync::update_nightly(),
         },
         DevCommand::Release(ReleaseCommand { subcommand }) => match subcommand {
-            ReleaseSubcommand::BumpVersion => release::bump_version(),
+            ReleaseSubcommand::BumpVersion => release::bump_version(clippy.version),
         },
     }
 }
@@ -136,11 +140,6 @@ enum DevCommand {
     /// * all lints are registered in the lint store
     UpdateLints {
         #[arg(long)]
-        /// Print a table of lints to STDOUT
-        ///
-        /// This does not include deprecated and internal lints. (Does not modify any files)
-        print_only: bool,
-        #[arg(long)]
         /// Checks that `cargo dev update_lints` has been run. Used on CI.
         check: bool,
     },
diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs
index 96e12706c9e..4121daa85e6 100644
--- a/clippy_dev/src/new_lint.rs
+++ b/clippy_dev/src/new_lint.rs
@@ -1,4 +1,4 @@
-use crate::utils::{clippy_project_root, clippy_version};
+use crate::utils::{RustSearcher, Token, Version};
 use clap::ValueEnum;
 use indoc::{formatdoc, writedoc};
 use std::fmt::{self, Write as _};
@@ -22,11 +22,11 @@ impl fmt::Display for Pass {
 }
 
 struct LintData<'a> {
+    clippy_version: Version,
     pass: Pass,
     name: &'a str,
     category: &'a str,
     ty: Option<&'a str>,
-    project_root: PathBuf,
 }
 
 trait Context {
@@ -50,18 +50,25 @@ impl<T> Context for io::Result<T> {
 /// # Errors
 ///
 /// This function errors out if the files couldn't be created or written to.
-pub fn create(pass: Pass, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> {
+pub fn create(
+    clippy_version: Version,
+    pass: Pass,
+    name: &str,
+    category: &str,
+    mut ty: Option<&str>,
+    msrv: bool,
+) -> io::Result<()> {
     if category == "cargo" && ty.is_none() {
         // `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
         ty = Some("cargo");
     }
 
     let lint = LintData {
+        clippy_version,
         pass,
         name,
         category,
         ty,
-        project_root: clippy_project_root(),
     };
 
     create_lint(&lint, msrv).context("Unable to create lint implementation")?;
@@ -88,7 +95,7 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
     } else {
         let lint_contents = get_lint_file_contents(lint, enable_msrv);
         let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
-        write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())?;
+        write_file(&lint_path, lint_contents.as_bytes())?;
         println!("Generated lint file: `{lint_path}`");
 
         Ok(())
@@ -115,8 +122,7 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
     }
 
     if lint.category == "cargo" {
-        let relative_test_dir = format!("tests/ui-cargo/{}", lint.name);
-        let test_dir = lint.project_root.join(&relative_test_dir);
+        let test_dir = format!("tests/ui-cargo/{}", lint.name);
         fs::create_dir(&test_dir)?;
 
         create_project_layout(
@@ -134,11 +140,11 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
             false,
         )?;
 
-        println!("Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`");
+        println!("Generated test directories: `{test_dir}/pass`, `{test_dir}/fail`");
     } else {
         let test_path = format!("tests/ui/{}.rs", lint.name);
         let test_contents = get_test_file_contents(lint.name, msrv);
-        write_file(lint.project_root.join(&test_path), test_contents)?;
+        write_file(&test_path, test_contents)?;
 
         println!("Generated test file: `{test_path}`");
     }
@@ -193,11 +199,6 @@ fn to_camel_case(name: &str) -> String {
         .collect()
 }
 
-pub(crate) fn get_stabilization_version() -> String {
-    let (minor, patch) = clippy_version();
-    format!("{minor}.{patch}.0")
-}
-
 fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
     let mut test = formatdoc!(
         r"
@@ -292,7 +293,11 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
         );
     }
 
-    let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));
+    let _: fmt::Result = writeln!(
+        result,
+        "{}",
+        get_lint_declaration(lint.clippy_version, &name_upper, category)
+    );
 
     if enable_msrv {
         let _: fmt::Result = writedoc!(
@@ -330,7 +335,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
     result
 }
 
-fn get_lint_declaration(name_upper: &str, category: &str) -> String {
+fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> String {
     let justification_heading = if category == "restriction" {
         "Why restrict this?"
     } else {
@@ -355,9 +360,8 @@ fn get_lint_declaration(name_upper: &str, category: &str) -> String {
                 pub {name_upper},
                 {category},
                 "default lint description"
-            }}
-        "#,
-        get_stabilization_version(),
+            }}"#,
+        version.rust_display(),
     )
 }
 
@@ -371,7 +375,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
         _ => {},
     }
 
-    let ty_dir = lint.project_root.join(format!("clippy_lints/src/{ty}"));
+    let ty_dir = PathBuf::from(format!("clippy_lints/src/{ty}"));
     assert!(
         ty_dir.exists() && ty_dir.is_dir(),
         "Directory `{}` does not exist!",
@@ -441,95 +445,25 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
 
 #[allow(clippy::too_many_lines)]
 fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str> {
-    use super::update_lints::{LintDeclSearchResult, match_tokens};
-    use rustc_lexer::TokenKind;
-
     let lint_name_upper = lint.name.to_uppercase();
 
     let mut file_contents = fs::read_to_string(path)?;
     assert!(
-        !file_contents.contains(&lint_name_upper),
+        !file_contents.contains(&format!("pub {lint_name_upper},")),
         "Lint `{}` already defined in `{}`",
         lint.name,
         path.display()
     );
 
-    let mut offset = 0usize;
-    let mut last_decl_curly_offset = None;
-    let mut lint_context = None;
-
-    let mut iter = rustc_lexer::tokenize(&file_contents).map(|t| {
-        let range = offset..offset + t.len as usize;
-        offset = range.end;
-
-        LintDeclSearchResult {
-            token_kind: t.kind,
-            content: &file_contents[range.clone()],
-            range,
-        }
-    });
-
-    // Find both the last lint declaration (declare_clippy_lint!) and the lint pass impl
-    while let Some(LintDeclSearchResult { content, .. }) = iter.find(|result| result.token_kind == TokenKind::Ident) {
-        let mut iter = iter
-            .by_ref()
-            .filter(|t| !matches!(t.token_kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
-
-        match content {
-            "declare_clippy_lint" => {
-                // matches `!{`
-                match_tokens!(iter, Bang OpenBrace);
-                if let Some(LintDeclSearchResult { range, .. }) =
-                    iter.find(|result| result.token_kind == TokenKind::CloseBrace)
-                {
-                    last_decl_curly_offset = Some(range.end);
-                }
-            },
-            "impl" => {
-                let mut token = iter.next();
-                match token {
-                    // matches <'foo>
-                    Some(LintDeclSearchResult {
-                        token_kind: TokenKind::Lt,
-                        ..
-                    }) => {
-                        match_tokens!(iter, Lifetime { .. } Gt);
-                        token = iter.next();
-                    },
-                    None => break,
-                    _ => {},
-                }
-
-                if let Some(LintDeclSearchResult {
-                    token_kind: TokenKind::Ident,
-                    content,
-                    ..
-                }) = token
-                {
-                    // Get the appropriate lint context struct
-                    lint_context = match content {
-                        "LateLintPass" => Some("LateContext"),
-                        "EarlyLintPass" => Some("EarlyContext"),
-                        _ => continue,
-                    };
-                }
-            },
-            _ => {},
-        }
-    }
-
-    drop(iter);
-
-    let last_decl_curly_offset =
-        last_decl_curly_offset.unwrap_or_else(|| panic!("No lint declarations found in `{}`", path.display()));
-    let lint_context =
-        lint_context.unwrap_or_else(|| panic!("No lint pass implementation found in `{}`", path.display()));
+    let (lint_context, lint_decl_end) = parse_mod_file(path, &file_contents);
 
     // Add the lint declaration to `mod.rs`
-    file_contents.replace_range(
-        // Remove the trailing newline, which should always be present
-        last_decl_curly_offset..=last_decl_curly_offset,
-        &format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
+    file_contents.insert_str(
+        lint_decl_end,
+        &format!(
+            "\n\n{}",
+            get_lint_declaration(lint.clippy_version, &lint_name_upper, lint.category)
+        ),
     );
 
     // Add the lint to `impl_lint_pass`/`declare_lint_pass`
@@ -580,6 +514,41 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
     Ok(lint_context)
 }
 
+// Find both the last lint declaration (declare_clippy_lint!) and the lint pass impl
+fn parse_mod_file(path: &Path, contents: &str) -> (&'static str, usize) {
+    #[allow(clippy::enum_glob_use)]
+    use Token::*;
+
+    let mut context = None;
+    let mut decl_end = None;
+    let mut searcher = RustSearcher::new(contents);
+    while let Some(name) = searcher.find_capture_token(CaptureIdent) {
+        match name {
+            "declare_clippy_lint" => {
+                if searcher.match_tokens(&[Bang, OpenBrace], &mut []) && searcher.find_token(CloseBrace) {
+                    decl_end = Some(searcher.pos());
+                }
+            },
+            "impl" => {
+                let mut capture = "";
+                if searcher.match_tokens(&[Lt, Lifetime, Gt, CaptureIdent], &mut [&mut capture]) {
+                    match capture {
+                        "LateLintPass" => context = Some("LateContext"),
+                        "EarlyLintPass" => context = Some("EarlyContext"),
+                        _ => {},
+                    }
+                }
+            },
+            _ => {},
+        }
+    }
+
+    (
+        context.unwrap_or_else(|| panic!("No lint pass implementation found in `{}`", path.display())),
+        decl_end.unwrap_or_else(|| panic!("No lint declarations found in `{}`", path.display())) as usize,
+    )
+}
+
 #[test]
 fn test_camel_case() {
     let s = "a_lint";
diff --git a/clippy_dev/src/release.rs b/clippy_dev/src/release.rs
index ac755168701..d3b1a7ff320 100644
--- a/clippy_dev/src/release.rs
+++ b/clippy_dev/src/release.rs
@@ -1,27 +1,27 @@
+use crate::utils::{FileUpdater, Version, update_text_region_fn};
 use std::fmt::Write;
-use std::path::Path;
 
-use crate::utils::{UpdateMode, clippy_version, replace_region_in_file};
-
-const CARGO_TOML_FILES: [&str; 4] = [
+static CARGO_TOML_FILES: &[&str] = &[
     "clippy_config/Cargo.toml",
     "clippy_lints/Cargo.toml",
     "clippy_utils/Cargo.toml",
     "Cargo.toml",
 ];
 
-pub fn bump_version() {
-    let (minor, mut patch) = clippy_version();
-    patch += 1;
-    for file in &CARGO_TOML_FILES {
-        replace_region_in_file(
-            UpdateMode::Change,
-            Path::new(file),
-            "# begin autogenerated version\n",
-            "# end autogenerated version",
-            |res| {
-                writeln!(res, "version = \"0.{minor}.{patch}\"").unwrap();
-            },
+pub fn bump_version(mut version: Version) {
+    version.minor += 1;
+
+    let mut updater = FileUpdater::default();
+    for file in CARGO_TOML_FILES {
+        updater.update_file(
+            file,
+            &mut update_text_region_fn(
+                "# begin autogenerated version\n",
+                "# end autogenerated version",
+                |dst| {
+                    writeln!(dst, "version = \"{}\"", version.toml_display()).unwrap();
+                },
+            ),
         );
     }
 }
diff --git a/clippy_dev/src/rename_lint.rs b/clippy_dev/src/rename_lint.rs
new file mode 100644
index 00000000000..9e7e5d97f02
--- /dev/null
+++ b/clippy_dev/src/rename_lint.rs
@@ -0,0 +1,194 @@
+use crate::update_lints::{
+    DeprecatedLints, RenamedLint, find_lint_decls, gen_renamed_lints_test_fn, generate_lint_files,
+    read_deprecated_lints,
+};
+use crate::utils::{FileUpdater, StringReplacer, UpdateMode, Version, try_rename_file};
+use std::ffi::OsStr;
+use std::path::Path;
+use walkdir::WalkDir;
+
+/// Runs the `rename_lint` command.
+///
+/// This does the following:
+/// * Adds an entry to `renamed_lints.rs`.
+/// * Renames all lint attributes to the new name (e.g. `#[allow(clippy::lint_name)]`).
+/// * Renames the lint struct to the new name.
+/// * Renames the module containing the lint struct to the new name if it shares a name with the
+///   lint.
+///
+/// # Panics
+/// Panics for the following conditions:
+/// * If a file path could not read from or then written to
+/// * If either lint name has a prefix
+/// * If `old_name` doesn't name an existing lint.
+/// * If `old_name` names a deprecated or renamed lint.
+#[allow(clippy::too_many_lines)]
+pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) {
+    if let Some((prefix, _)) = old_name.split_once("::") {
+        panic!("`{old_name}` should not contain the `{prefix}` prefix");
+    }
+    if let Some((prefix, _)) = new_name.split_once("::") {
+        panic!("`{new_name}` should not contain the `{prefix}` prefix");
+    }
+
+    let mut updater = FileUpdater::default();
+    let mut lints = find_lint_decls();
+    let DeprecatedLints {
+        renamed: mut renamed_lints,
+        deprecated: deprecated_lints,
+        file: mut deprecated_file,
+        contents: mut deprecated_contents,
+        renamed_end,
+        ..
+    } = read_deprecated_lints();
+
+    let mut old_lint_index = None;
+    let mut found_new_name = false;
+    for (i, lint) in lints.iter().enumerate() {
+        if lint.name == old_name {
+            old_lint_index = Some(i);
+        } else if lint.name == new_name {
+            found_new_name = true;
+        }
+    }
+    let old_lint_index = old_lint_index.unwrap_or_else(|| panic!("could not find lint `{old_name}`"));
+
+    let lint = RenamedLint {
+        old_name: format!("clippy::{old_name}"),
+        new_name: if uplift {
+            new_name.into()
+        } else {
+            format!("clippy::{new_name}")
+        },
+    };
+
+    // Renamed lints and deprecated lints shouldn't have been found in the lint list, but check just in
+    // case.
+    assert!(
+        !renamed_lints.iter().any(|l| lint.old_name == l.old_name),
+        "`{old_name}` has already been renamed"
+    );
+    assert!(
+        !deprecated_lints.iter().any(|l| lint.old_name == l.name),
+        "`{old_name}` has already been deprecated"
+    );
+
+    // Update all lint level attributes. (`clippy::lint_name`)
+    let replacements = &[(&*lint.old_name, &*lint.new_name)];
+    let replacer = StringReplacer::new(replacements);
+    for file in WalkDir::new(".").into_iter().map(Result::unwrap).filter(|f| {
+        let name = f.path().file_name();
+        let ext = f.path().extension();
+        (ext == Some(OsStr::new("rs")) || ext == Some(OsStr::new("fixed")))
+            && name != Some(OsStr::new("rename.rs"))
+            && name != Some(OsStr::new("deprecated_lints.rs"))
+    }) {
+        updater.update_file(file.path(), &mut replacer.replace_ident_fn());
+    }
+
+    deprecated_contents.insert_str(
+        renamed_end as usize,
+        &format!(
+            "    #[clippy::version = \"{}\"]\n    (\"{}\", \"{}\"),\n",
+            clippy_version.rust_display(),
+            lint.old_name,
+            lint.new_name,
+        ),
+    );
+    deprecated_file.replace_contents(deprecated_contents.as_bytes());
+    drop(deprecated_file);
+
+    renamed_lints.push(lint);
+    renamed_lints.sort_by(|lhs, rhs| {
+        lhs.new_name
+            .starts_with("clippy::")
+            .cmp(&rhs.new_name.starts_with("clippy::"))
+            .reverse()
+            .then_with(|| lhs.old_name.cmp(&rhs.old_name))
+    });
+
+    if uplift {
+        updater.update_file("tests/ui/rename.rs", &mut gen_renamed_lints_test_fn(&renamed_lints));
+        println!(
+            "`{old_name}` has be uplifted. All the code inside `clippy_lints` related to it needs to be removed manually."
+        );
+    } else if found_new_name {
+        updater.update_file("tests/ui/rename.rs", &mut gen_renamed_lints_test_fn(&renamed_lints));
+        println!(
+            "`{new_name}` is already defined. The old linting code inside `clippy_lints` needs to be updated/removed manually."
+        );
+    } else {
+        // Rename the lint struct and source files sharing a name with the lint.
+        let lint = &mut lints[old_lint_index];
+        let old_name_upper = old_name.to_uppercase();
+        let new_name_upper = new_name.to_uppercase();
+        lint.name = new_name.into();
+
+        // Rename test files. only rename `.stderr` and `.fixed` files if the new test name doesn't exist.
+        if try_rename_file(
+            Path::new(&format!("tests/ui/{old_name}.rs")),
+            Path::new(&format!("tests/ui/{new_name}.rs")),
+        ) {
+            try_rename_file(
+                Path::new(&format!("tests/ui/{old_name}.stderr")),
+                Path::new(&format!("tests/ui/{new_name}.stderr")),
+            );
+            try_rename_file(
+                Path::new(&format!("tests/ui/{old_name}.fixed")),
+                Path::new(&format!("tests/ui/{new_name}.fixed")),
+            );
+        }
+
+        // Try to rename the file containing the lint if the file name matches the lint's name.
+        let replacements;
+        let replacements = if lint.module == old_name
+            && try_rename_file(
+                Path::new(&format!("clippy_lints/src/{old_name}.rs")),
+                Path::new(&format!("clippy_lints/src/{new_name}.rs")),
+            ) {
+            // Edit the module name in the lint list. Note there could be multiple lints.
+            for lint in lints.iter_mut().filter(|l| l.module == old_name) {
+                lint.module = new_name.into();
+            }
+            replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
+            replacements.as_slice()
+        } else if !lint.module.contains("::")
+            // Catch cases like `methods/lint_name.rs` where the lint is stored in `methods/mod.rs`
+            && try_rename_file(
+                Path::new(&format!("clippy_lints/src/{}/{old_name}.rs", lint.module)),
+                Path::new(&format!("clippy_lints/src/{}/{new_name}.rs", lint.module)),
+            )
+        {
+            // Edit the module name in the lint list. Note there could be multiple lints, or none.
+            let renamed_mod = format!("{}::{old_name}", lint.module);
+            for lint in lints.iter_mut().filter(|l| l.module == renamed_mod) {
+                lint.module = format!("{}::{new_name}", lint.module);
+            }
+            replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
+            replacements.as_slice()
+        } else {
+            replacements = [(&*old_name_upper, &*new_name_upper), ("", "")];
+            &replacements[0..1]
+        };
+
+        // Don't change `clippy_utils/src/renamed_lints.rs` here as it would try to edit the lint being
+        // renamed.
+        let replacer = StringReplacer::new(replacements);
+        for file in WalkDir::new("clippy_lints/src") {
+            let file = file.expect("error reading `clippy_lints/src`");
+            if file
+                .path()
+                .as_os_str()
+                .to_str()
+                .is_some_and(|x| x.ends_with("*.rs") && x["clippy_lints/src/".len()..] != *"deprecated_lints.rs")
+            {
+                updater.update_file(file.path(), &mut replacer.replace_ident_fn());
+            }
+        }
+
+        generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
+        println!("{old_name} has been successfully renamed");
+    }
+
+    println!("note: `cargo uitest` still needs to be run to update the test results");
+}
diff --git a/clippy_dev/src/sync.rs b/clippy_dev/src/sync.rs
index a6b65e561c2..c699b0d7b95 100644
--- a/clippy_dev/src/sync.rs
+++ b/clippy_dev/src/sync.rs
@@ -1,33 +1,18 @@
-use std::fmt::Write;
-use std::path::Path;
-
+use crate::utils::{FileUpdater, update_text_region_fn};
 use chrono::offset::Utc;
-
-use crate::utils::{UpdateMode, replace_region_in_file};
+use std::fmt::Write;
 
 pub fn update_nightly() {
-    // Update rust-toolchain nightly version
     let date = Utc::now().format("%Y-%m-%d").to_string();
-    replace_region_in_file(
-        UpdateMode::Change,
-        Path::new("rust-toolchain.toml"),
+    let update = &mut update_text_region_fn(
         "# begin autogenerated nightly\n",
         "# end autogenerated nightly",
-        |res| {
-            writeln!(res, "channel = \"nightly-{date}\"").unwrap();
+        |dst| {
+            writeln!(dst, "channel = \"nightly-{date}\"").unwrap();
         },
     );
 
-    // Update clippy_utils nightly version
-    replace_region_in_file(
-        UpdateMode::Change,
-        Path::new("clippy_utils/README.md"),
-        "<!-- begin autogenerated nightly -->\n",
-        "<!-- end autogenerated nightly -->",
-        |res| {
-            writeln!(res, "```").unwrap();
-            writeln!(res, "nightly-{date}").unwrap();
-            writeln!(res, "```").unwrap();
-        },
-    );
+    let mut updater = FileUpdater::default();
+    updater.update_file("rust-toolchain.toml", update);
+    updater.update_file("clippy_utils/README.md", update);
 }
diff --git a/clippy_dev/src/update_lints.rs b/clippy_dev/src/update_lints.rs
index d848a97f86d..0c861b72935 100644
--- a/clippy_dev/src/update_lints.rs
+++ b/clippy_dev/src/update_lints.rs
@@ -1,15 +1,12 @@
-use crate::utils::{UpdateMode, clippy_project_root, exit_with_failure, replace_region_in_file};
-use aho_corasick::AhoCorasickBuilder;
+use crate::utils::{
+    File, FileAction, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, panic_file, update_text_region_fn,
+};
 use itertools::Itertools;
-use rustc_lexer::{LiteralKind, TokenKind, tokenize};
-use rustc_literal_escaper::{Mode, unescape_unicode};
-use std::collections::{HashMap, HashSet};
-use std::ffi::OsStr;
-use std::fmt::{self, Write};
-use std::fs::{self, OpenOptions};
-use std::io::{self, Read, Seek, Write as _};
+use std::collections::HashSet;
+use std::fmt::Write;
+use std::fs::OpenOptions;
 use std::ops::Range;
-use std::path::{Path, PathBuf};
+use std::path::Path;
 use walkdir::{DirEntry, WalkDir};
 
 const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev update_lints`.\n\
@@ -28,839 +25,322 @@ const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.ht
 ///
 /// Panics if a file path could not read from or then written to
 pub fn update(update_mode: UpdateMode) {
-    let (lints, deprecated_lints, renamed_lints) = gather_all();
-    generate_lint_files(update_mode, &lints, &deprecated_lints, &renamed_lints);
+    let lints = find_lint_decls();
+    let DeprecatedLints {
+        renamed, deprecated, ..
+    } = read_deprecated_lints();
+    generate_lint_files(update_mode, &lints, &deprecated, &renamed);
 }
 
-fn generate_lint_files(
+pub fn generate_lint_files(
     update_mode: UpdateMode,
     lints: &[Lint],
-    deprecated_lints: &[DeprecatedLint],
-    renamed_lints: &[RenamedLint],
+    deprecated: &[DeprecatedLint],
+    renamed: &[RenamedLint],
 ) {
-    let mut lints = lints.to_owned();
-    lints.sort_by_key(|lint| lint.name.clone());
-
-    replace_region_in_file(
-        update_mode,
-        Path::new("README.md"),
-        "[There are over ",
-        " lints included in this crate!]",
-        |res| {
-            write!(res, "{}", round_to_fifty(lints.len())).unwrap();
-        },
-    );
-
-    replace_region_in_file(
-        update_mode,
-        Path::new("book/src/README.md"),
-        "[There are over ",
-        " lints included in this crate!]",
-        |res| {
-            write!(res, "{}", round_to_fifty(lints.len())).unwrap();
-        },
-    );
-
-    replace_region_in_file(
-        update_mode,
-        Path::new("CHANGELOG.md"),
-        "<!-- begin autogenerated links to lint list -->\n",
-        "<!-- end autogenerated links to lint list -->",
-        |res| {
-            for lint in lints
-                .iter()
-                .map(|l| &*l.name)
-                .chain(deprecated_lints.iter().filter_map(|l| l.name.strip_prefix("clippy::")))
-                .chain(renamed_lints.iter().filter_map(|l| l.old_name.strip_prefix("clippy::")))
-                .sorted()
-            {
-                writeln!(res, "[`{lint}`]: {DOCS_LINK}#{lint}").unwrap();
-            }
-        },
-    );
-
-    // This has to be in lib.rs, otherwise rustfmt doesn't work
-    replace_region_in_file(
-        update_mode,
-        Path::new("clippy_lints/src/lib.rs"),
-        "// 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`",
-        |res| {
-            for lint_mod in lints.iter().map(|l| &l.module).unique().sorted() {
-                writeln!(res, "mod {lint_mod};").unwrap();
-            }
-        },
-    );
-
-    process_file(
-        "clippy_lints/src/declared_lints.rs",
+    FileUpdater::default().update_files_checked(
+        "cargo dev update_lints",
         update_mode,
-        &gen_declared_lints(lints.iter()),
-    );
-
-    let content = gen_deprecated_lints_test(deprecated_lints);
-    process_file("tests/ui/deprecated.rs", update_mode, &content);
-
-    let content = gen_renamed_lints_test(renamed_lints);
-    process_file("tests/ui/rename.rs", update_mode, &content);
-}
-
-pub fn print_lints() {
-    let (lints, _, _) = gather_all();
-    let lint_count = lints.len();
-    let grouped_by_lint_group = Lint::by_lint_group(lints.into_iter());
-
-    for (lint_group, mut lints) in grouped_by_lint_group {
-        println!("\n## {lint_group}");
-
-        lints.sort_by_key(|l| l.name.clone());
-
-        for lint in lints {
-            println!("* [{}]({DOCS_LINK}#{}) ({})", lint.name, lint.name, lint.desc);
-        }
-    }
-
-    println!("there are {lint_count} lints");
-}
-
-/// Runs the `rename_lint` command.
-///
-/// This does the following:
-/// * Adds an entry to `renamed_lints.rs`.
-/// * Renames all lint attributes to the new name (e.g. `#[allow(clippy::lint_name)]`).
-/// * Renames the lint struct to the new name.
-/// * Renames the module containing the lint struct to the new name if it shares a name with the
-///   lint.
-///
-/// # Panics
-/// Panics for the following conditions:
-/// * If a file path could not read from or then written to
-/// * If either lint name has a prefix
-/// * If `old_name` doesn't name an existing lint.
-/// * If `old_name` names a deprecated or renamed lint.
-#[allow(clippy::too_many_lines)]
-pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
-    if let Some((prefix, _)) = old_name.split_once("::") {
-        panic!("`{old_name}` should not contain the `{prefix}` prefix");
-    }
-    if let Some((prefix, _)) = new_name.split_once("::") {
-        panic!("`{new_name}` should not contain the `{prefix}` prefix");
-    }
-
-    let (mut lints, deprecated_lints, mut renamed_lints) = gather_all();
-    let mut old_lint_index = None;
-    let mut found_new_name = false;
-    for (i, lint) in lints.iter().enumerate() {
-        if lint.name == old_name {
-            old_lint_index = Some(i);
-        } else if lint.name == new_name {
-            found_new_name = true;
-        }
-    }
-    let old_lint_index = old_lint_index.unwrap_or_else(|| panic!("could not find lint `{old_name}`"));
-
-    let lint = RenamedLint {
-        old_name: format!("clippy::{old_name}"),
-        new_name: if uplift {
-            new_name.into()
-        } else {
-            format!("clippy::{new_name}")
-        },
-    };
-
-    // Renamed lints and deprecated lints shouldn't have been found in the lint list, but check just in
-    // case.
-    assert!(
-        !renamed_lints.iter().any(|l| lint.old_name == l.old_name),
-        "`{old_name}` has already been renamed"
-    );
-    assert!(
-        !deprecated_lints.iter().any(|l| lint.old_name == l.name),
-        "`{old_name}` has already been deprecated"
-    );
-
-    // Update all lint level attributes. (`clippy::lint_name`)
-    for file in WalkDir::new(clippy_project_root())
-        .into_iter()
-        .map(Result::unwrap)
-        .filter(|f| {
-            let name = f.path().file_name();
-            let ext = f.path().extension();
-            (ext == Some(OsStr::new("rs")) || ext == Some(OsStr::new("fixed")))
-                && name != Some(OsStr::new("rename.rs"))
-                && name != Some(OsStr::new("deprecated_lints.rs"))
-        })
-    {
-        rewrite_file(file.path(), |s| {
-            replace_ident_like(s, &[(&lint.old_name, &lint.new_name)])
-        });
-    }
-
-    let version = crate::new_lint::get_stabilization_version();
-    rewrite_file(Path::new("clippy_lints/src/deprecated_lints.rs"), |s| {
-        insert_at_marker(
-            s,
-            "// end renamed lints. used by `cargo dev rename_lint`",
-            &format!(
-                "#[clippy::version = \"{version}\"]\n    \
-                (\"{}\", \"{}\"),\n    ",
-                lint.old_name, lint.new_name,
+        &mut [
+            (
+                "README.md",
+                &mut update_text_region_fn("[There are over ", " lints included in this crate!]", |dst| {
+                    write!(dst, "{}", round_to_fifty(lints.len())).unwrap();
+                }),
             ),
-        )
-    });
-
-    renamed_lints.push(lint);
-    renamed_lints.sort_by(|lhs, rhs| {
-        lhs.new_name
-            .starts_with("clippy::")
-            .cmp(&rhs.new_name.starts_with("clippy::"))
-            .reverse()
-            .then_with(|| lhs.old_name.cmp(&rhs.old_name))
-    });
-
-    if uplift {
-        write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints));
-        println!(
-            "`{old_name}` has be uplifted. All the code inside `clippy_lints` related to it needs to be removed manually."
-        );
-    } else if found_new_name {
-        write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints));
-        println!(
-            "`{new_name}` is already defined. The old linting code inside `clippy_lints` needs to be updated/removed manually."
-        );
-    } else {
-        // Rename the lint struct and source files sharing a name with the lint.
-        let lint = &mut lints[old_lint_index];
-        let old_name_upper = old_name.to_uppercase();
-        let new_name_upper = new_name.to_uppercase();
-        lint.name = new_name.into();
-
-        // Rename test files. only rename `.stderr` and `.fixed` files if the new test name doesn't exist.
-        if try_rename_file(
-            Path::new(&format!("tests/ui/{old_name}.rs")),
-            Path::new(&format!("tests/ui/{new_name}.rs")),
-        ) {
-            try_rename_file(
-                Path::new(&format!("tests/ui/{old_name}.stderr")),
-                Path::new(&format!("tests/ui/{new_name}.stderr")),
-            );
-            try_rename_file(
-                Path::new(&format!("tests/ui/{old_name}.fixed")),
-                Path::new(&format!("tests/ui/{new_name}.fixed")),
-            );
-        }
-
-        // Try to rename the file containing the lint if the file name matches the lint's name.
-        let replacements;
-        let replacements = if lint.module == old_name
-            && try_rename_file(
-                Path::new(&format!("clippy_lints/src/{old_name}.rs")),
-                Path::new(&format!("clippy_lints/src/{new_name}.rs")),
-            ) {
-            // Edit the module name in the lint list. Note there could be multiple lints.
-            for lint in lints.iter_mut().filter(|l| l.module == old_name) {
-                lint.module = new_name.into();
-            }
-            replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
-            replacements.as_slice()
-        } else if !lint.module.contains("::")
-            // Catch cases like `methods/lint_name.rs` where the lint is stored in `methods/mod.rs`
-            && try_rename_file(
-                Path::new(&format!("clippy_lints/src/{}/{old_name}.rs", lint.module)),
-                Path::new(&format!("clippy_lints/src/{}/{new_name}.rs", lint.module)),
-            )
-        {
-            // Edit the module name in the lint list. Note there could be multiple lints, or none.
-            let renamed_mod = format!("{}::{old_name}", lint.module);
-            for lint in lints.iter_mut().filter(|l| l.module == renamed_mod) {
-                lint.module = format!("{}::{new_name}", lint.module);
-            }
-            replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
-            replacements.as_slice()
-        } else {
-            replacements = [(&*old_name_upper, &*new_name_upper), ("", "")];
-            &replacements[0..1]
-        };
-
-        // Don't change `clippy_utils/src/renamed_lints.rs` here as it would try to edit the lint being
-        // renamed.
-        for (_, file) in clippy_lints_src_files().filter(|(rel_path, _)| rel_path != OsStr::new("deprecated_lints.rs"))
-        {
-            rewrite_file(file.path(), |s| replace_ident_like(s, replacements));
-        }
-
-        generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
-        println!("{old_name} has been successfully renamed");
-    }
-
-    println!("note: `cargo uitest` still needs to be run to update the test results");
-}
-
-/// Runs the `deprecate` command
-///
-/// This does the following:
-/// * Adds an entry to `deprecated_lints.rs`.
-/// * Removes the lint declaration (and the entire file if applicable)
-///
-/// # Panics
-///
-/// If a file path could not read from or written to
-pub fn deprecate(name: &str, reason: &str) {
-    let prefixed_name = if name.starts_with("clippy::") {
-        name.to_owned()
-    } else {
-        format!("clippy::{name}")
-    };
-    let stripped_name = &prefixed_name[8..];
-
-    let (mut lints, mut deprecated_lints, renamed_lints) = gather_all();
-    let Some(lint) = lints.iter().find(|l| l.name == stripped_name) else {
-        eprintln!("error: failed to find lint `{name}`");
-        return;
-    };
-
-    let mod_path = {
-        let mut mod_path = PathBuf::from(format!("clippy_lints/src/{}", lint.module));
-        if mod_path.is_dir() {
-            mod_path = mod_path.join("mod");
-        }
-
-        mod_path.set_extension("rs");
-        mod_path
-    };
-
-    let deprecated_lints_path = &*clippy_project_root().join("clippy_lints/src/deprecated_lints.rs");
-
-    if remove_lint_declaration(stripped_name, &mod_path, &mut lints).unwrap_or(false) {
-        let version = crate::new_lint::get_stabilization_version();
-        rewrite_file(deprecated_lints_path, |s| {
-            insert_at_marker(
-                s,
-                "// end deprecated lints. used by `cargo dev deprecate_lint`",
-                &format!("#[clippy::version = \"{version}\"]\n    (\"{prefixed_name}\", \"{reason}\"),\n    ",),
-            )
-        });
-
-        deprecated_lints.push(DeprecatedLint {
-            name: prefixed_name,
-            reason: reason.into(),
-        });
-
-        generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
-        println!("info: `{name}` has successfully been deprecated");
-        println!("note: you must run `cargo uitest` to update the test results");
-    } else {
-        eprintln!("error: lint not found");
-    }
-}
-
-fn remove_lint_declaration(name: &str, path: &Path, lints: &mut Vec<Lint>) -> io::Result<bool> {
-    fn remove_lint(name: &str, lints: &mut Vec<Lint>) {
-        lints.iter().position(|l| l.name == name).map(|pos| lints.remove(pos));
-    }
-
-    fn remove_test_assets(name: &str) {
-        let test_file_stem = format!("tests/ui/{name}");
-        let path = Path::new(&test_file_stem);
-
-        // Some lints have their own directories, delete them
-        if path.is_dir() {
-            let _ = fs::remove_dir_all(path);
-            return;
-        }
-
-        // Remove all related test files
-        let _ = fs::remove_file(path.with_extension("rs"));
-        let _ = fs::remove_file(path.with_extension("stderr"));
-        let _ = fs::remove_file(path.with_extension("fixed"));
-    }
-
-    fn remove_impl_lint_pass(lint_name_upper: &str, content: &mut String) {
-        let impl_lint_pass_start = content.find("impl_lint_pass!").unwrap_or_else(|| {
-            content
-                .find("declare_lint_pass!")
-                .unwrap_or_else(|| panic!("failed to find `impl_lint_pass`"))
-        });
-        let mut impl_lint_pass_end = content[impl_lint_pass_start..]
-            .find(']')
-            .expect("failed to find `impl_lint_pass` terminator");
-
-        impl_lint_pass_end += impl_lint_pass_start;
-        if let Some(lint_name_pos) = content[impl_lint_pass_start..impl_lint_pass_end].find(lint_name_upper) {
-            let mut lint_name_end = impl_lint_pass_start + (lint_name_pos + lint_name_upper.len());
-            for c in content[lint_name_end..impl_lint_pass_end].chars() {
-                // Remove trailing whitespace
-                if c == ',' || c.is_whitespace() {
-                    lint_name_end += 1;
-                } else {
-                    break;
+            (
+                "book/src/README.md",
+                &mut update_text_region_fn("[There are over ", " lints included in this crate!]", |dst| {
+                    write!(dst, "{}", round_to_fifty(lints.len())).unwrap();
+                }),
+            ),
+            (
+                "CHANGELOG.md",
+                &mut update_text_region_fn(
+                    "<!-- begin autogenerated links to lint list -->\n",
+                    "<!-- end autogenerated links to lint list -->",
+                    |dst| {
+                        for lint in lints
+                            .iter()
+                            .map(|l| &*l.name)
+                            .chain(deprecated.iter().filter_map(|l| l.name.strip_prefix("clippy::")))
+                            .chain(renamed.iter().filter_map(|l| l.old_name.strip_prefix("clippy::")))
+                            .sorted()
+                        {
+                            writeln!(dst, "[`{lint}`]: {DOCS_LINK}#{lint}").unwrap();
+                        }
+                    },
+                ),
+            ),
+            (
+                "clippy_lints/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| {
+                        for lint_mod in lints.iter().map(|l| &l.module).sorted().dedup() {
+                            writeln!(dst, "mod {lint_mod};").unwrap();
+                        }
+                    },
+                ),
+            ),
+            ("clippy_lints/src/declared_lints.rs", &mut |_, src, dst| {
+                dst.push_str(GENERATED_FILE_COMMENT);
+                dst.push_str("pub static LINTS: &[&crate::LintInfo] = &[\n");
+                for (module_name, lint_name) in lints.iter().map(|l| (&l.module, l.name.to_uppercase())).sorted() {
+                    writeln!(dst, "    crate::{module_name}::{lint_name}_INFO,").unwrap();
                 }
-            }
-
-            content.replace_range(impl_lint_pass_start + lint_name_pos..lint_name_end, "");
-        }
-    }
-
-    if path.exists()
-        && let Some(lint) = lints.iter().find(|l| l.name == name)
-    {
-        if lint.module == name {
-            // The lint name is the same as the file, we can just delete the entire file
-            fs::remove_file(path)?;
-        } else {
-            // We can't delete the entire file, just remove the declaration
-
-            if let Some(Some("mod.rs")) = path.file_name().map(OsStr::to_str) {
-                // Remove clippy_lints/src/some_mod/some_lint.rs
-                let mut lint_mod_path = path.to_path_buf();
-                lint_mod_path.set_file_name(name);
-                lint_mod_path.set_extension("rs");
-
-                let _ = fs::remove_file(lint_mod_path);
-            }
-
-            let mut content =
-                fs::read_to_string(path).unwrap_or_else(|_| panic!("failed to read `{}`", path.to_string_lossy()));
-
-            eprintln!(
-                "warn: you will have to manually remove any code related to `{name}` from `{}`",
-                path.display()
-            );
-
-            assert!(
-                content[lint.declaration_range.clone()].contains(&name.to_uppercase()),
-                "error: `{}` does not contain lint `{}`'s declaration",
-                path.display(),
-                lint.name
-            );
-
-            // Remove lint declaration (declare_clippy_lint!)
-            content.replace_range(lint.declaration_range.clone(), "");
-
-            // Remove the module declaration (mod xyz;)
-            let mod_decl = format!("\nmod {name};");
-            content = content.replacen(&mod_decl, "", 1);
-
-            remove_impl_lint_pass(&lint.name.to_uppercase(), &mut content);
-            fs::write(path, content).unwrap_or_else(|_| panic!("failed to write to `{}`", path.to_string_lossy()));
-        }
-
-        remove_test_assets(name);
-        remove_lint(name, lints);
-        return Ok(true);
-    }
-
-    Ok(false)
-}
-
-/// Replace substrings if they aren't bordered by identifier characters. Returns `None` if there
-/// were no replacements.
-fn replace_ident_like(contents: &str, replacements: &[(&str, &str)]) -> Option<String> {
-    fn is_ident_char(c: u8) -> bool {
-        matches!(c, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_')
-    }
-
-    let searcher = AhoCorasickBuilder::new()
-        .match_kind(aho_corasick::MatchKind::LeftmostLongest)
-        .build(replacements.iter().map(|&(x, _)| x.as_bytes()))
-        .unwrap();
-
-    let mut result = String::with_capacity(contents.len() + 1024);
-    let mut pos = 0;
-    let mut edited = false;
-    for m in searcher.find_iter(contents) {
-        let (old, new) = replacements[m.pattern()];
-        result.push_str(&contents[pos..m.start()]);
-        result.push_str(
-            if !is_ident_char(contents.as_bytes().get(m.start().wrapping_sub(1)).copied().unwrap_or(0))
-                && !is_ident_char(contents.as_bytes().get(m.end()).copied().unwrap_or(0))
-            {
-                edited = true;
-                new
-            } else {
-                old
-            },
-        );
-        pos = m.end();
-    }
-    result.push_str(&contents[pos..]);
-    edited.then_some(result)
+                dst.push_str("];\n");
+                UpdateStatus::from_changed(src != dst)
+            }),
+            ("tests/ui/deprecated.rs", &mut |_, src, dst| {
+                dst.push_str(GENERATED_FILE_COMMENT);
+                for lint in deprecated {
+                    writeln!(dst, "#![warn({})] //~ ERROR: lint `{}`", lint.name, lint.name).unwrap();
+                }
+                dst.push_str("\nfn main() {}\n");
+                UpdateStatus::from_changed(src != dst)
+            }),
+            ("tests/ui/rename.rs", &mut gen_renamed_lints_test_fn(renamed)),
+        ],
+    );
 }
 
 fn round_to_fifty(count: usize) -> usize {
     count / 50 * 50
 }
 
-fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str) {
-    if update_mode == UpdateMode::Check {
-        let old_content =
-            fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {e}", path.as_ref().display()));
-        if content != old_content {
-            exit_with_failure();
-        }
-    } else {
-        fs::write(&path, content.as_bytes())
-            .unwrap_or_else(|e| panic!("Cannot write to {}: {e}", path.as_ref().display()));
-    }
-}
-
 /// Lint data parsed from the Clippy source code.
 #[derive(Clone, PartialEq, Eq, Debug)]
-struct Lint {
-    name: String,
-    group: String,
-    desc: String,
-    module: String,
-    declaration_range: Range<usize>,
-}
-
-impl Lint {
-    #[must_use]
-    fn new(name: &str, group: &str, desc: &str, module: &str, declaration_range: Range<usize>) -> Self {
-        Self {
-            name: name.to_lowercase(),
-            group: group.into(),
-            desc: remove_line_splices(desc),
-            module: module.into(),
-            declaration_range,
-        }
-    }
-
-    /// Returns the lints in a `HashMap`, grouped by the different lint groups
-    #[must_use]
-    fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
-        lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
-    }
+pub struct Lint {
+    pub name: String,
+    pub group: String,
+    pub module: String,
+    pub declaration_range: Range<usize>,
 }
 
 #[derive(Clone, PartialEq, Eq, Debug)]
-struct DeprecatedLint {
-    name: String,
-    reason: String,
-}
-impl DeprecatedLint {
-    fn new(name: &str, reason: &str) -> Self {
-        Self {
-            name: remove_line_splices(name),
-            reason: remove_line_splices(reason),
-        }
-    }
+pub struct DeprecatedLint {
+    pub name: String,
+    pub reason: String,
 }
 
-struct RenamedLint {
-    old_name: String,
-    new_name: String,
-}
-impl RenamedLint {
-    fn new(old_name: &str, new_name: &str) -> Self {
-        Self {
-            old_name: remove_line_splices(old_name),
-            new_name: remove_line_splices(new_name),
-        }
-    }
+pub struct RenamedLint {
+    pub old_name: String,
+    pub new_name: String,
 }
 
-/// Generates the code for registering lints
-#[must_use]
-fn gen_declared_lints<'a>(lints: impl Iterator<Item = &'a Lint>) -> String {
-    let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect();
-    details.sort_unstable();
-
-    let mut output = GENERATED_FILE_COMMENT.to_string();
-    output.push_str("pub static LINTS: &[&crate::LintInfo] = &[\n");
-
-    for (module_name, lint_name) in details {
-        let _: fmt::Result = writeln!(output, "    crate::{module_name}::{lint_name}_INFO,");
-    }
-    output.push_str("];\n");
-
-    output
-}
-
-fn gen_deprecated_lints_test(lints: &[DeprecatedLint]) -> String {
-    let mut res: String = GENERATED_FILE_COMMENT.into();
-    for lint in lints {
-        writeln!(res, "#![warn({})] //~ ERROR: lint `{}`", lint.name, lint.name).unwrap();
-    }
-    res.push_str("\nfn main() {}\n");
-    res
-}
-
-fn gen_renamed_lints_test(lints: &[RenamedLint]) -> String {
-    let mut seen_lints = HashSet::new();
-    let mut res: String = GENERATED_FILE_COMMENT.into();
-
-    res.push_str("#![allow(clippy::duplicated_attributes)]\n");
-    for lint in lints {
-        if seen_lints.insert(&lint.new_name) {
-            writeln!(res, "#![allow({})]", lint.new_name).unwrap();
+pub fn gen_renamed_lints_test_fn(lints: &[RenamedLint]) -> impl Fn(&Path, &str, &mut String) -> UpdateStatus {
+    move |_, src, dst| {
+        let mut seen_lints = HashSet::new();
+        dst.push_str(GENERATED_FILE_COMMENT);
+        dst.push_str("#![allow(clippy::duplicated_attributes)]\n");
+        for lint in lints {
+            if seen_lints.insert(&lint.new_name) {
+                writeln!(dst, "#![allow({})]", lint.new_name).unwrap();
+            }
         }
-    }
-    seen_lints.clear();
-    for lint in lints {
-        if seen_lints.insert(&lint.old_name) {
-            writeln!(res, "#![warn({})] //~ ERROR: lint `{}`", lint.old_name, lint.old_name).unwrap();
+        seen_lints.clear();
+        for lint in lints {
+            if seen_lints.insert(&lint.old_name) {
+                writeln!(dst, "#![warn({})] //~ ERROR: lint `{}`", lint.old_name, lint.old_name).unwrap();
+            }
         }
+        dst.push_str("\nfn main() {}\n");
+        UpdateStatus::from_changed(src != dst)
     }
-    res.push_str("\nfn main() {}\n");
-    res
 }
 
-/// Gathers all lints defined in `clippy_lints/src`
-fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>, Vec<RenamedLint>) {
+/// Finds all lint declarations (`declare_clippy_lint!`)
+#[must_use]
+pub fn find_lint_decls() -> Vec<Lint> {
     let mut lints = Vec::with_capacity(1000);
-    let mut deprecated_lints = Vec::with_capacity(50);
-    let mut renamed_lints = Vec::with_capacity(50);
-
-    for (rel_path, file) in clippy_lints_src_files() {
-        let path = file.path();
-        let contents =
-            fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {e}", path.display()));
-        let module = rel_path
-            .components()
-            .map(|c| c.as_os_str().to_str().unwrap())
-            .collect::<Vec<_>>()
-            .join("::");
+    let mut contents = String::new();
+    for (file, module) in read_src_with_module("clippy_lints/src".as_ref()) {
+        parse_clippy_lint_decls(
+            File::open_read_to_cleared_string(file.path(), &mut contents),
+            &module,
+            &mut lints,
+        );
+    }
+    lints.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
+    lints
+}
 
-        // If the lints are stored in mod.rs, we get the module name from
-        // the containing directory:
-        let module = if let Some(module) = module.strip_suffix("::mod.rs") {
-            module
-        } else {
-            module.strip_suffix(".rs").unwrap_or(&module)
+/// Reads the source files from the given root directory
+fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirEntry, String)> {
+    WalkDir::new(src_root).into_iter().filter_map(move |e| {
+        let e = match e {
+            Ok(e) => e,
+            Err(ref e) => panic_file(e, FileAction::Read, src_root),
         };
-
-        if module == "deprecated_lints" {
-            parse_deprecated_contents(&contents, &mut deprecated_lints, &mut renamed_lints);
+        let path = e.path().as_os_str().as_encoded_bytes();
+        if let Some(path) = path.strip_suffix(b".rs")
+            && let Some(path) = path.get("clippy_lints/src/".len()..)
+        {
+            if path == b"lib" {
+                Some((e, String::new()))
+            } else {
+                let path = if let Some(path) = path.strip_suffix(b"mod")
+                    && let Some(path) = path.strip_suffix(b"/").or_else(|| path.strip_suffix(b"\\"))
+                {
+                    path
+                } else {
+                    path
+                };
+                if let Ok(path) = str::from_utf8(path) {
+                    let path = path.replace(['/', '\\'], "::");
+                    Some((e, path))
+                } else {
+                    None
+                }
+            }
         } else {
-            parse_contents(&contents, module, &mut lints);
+            None
         }
-    }
-    (lints, deprecated_lints, renamed_lints)
-}
-
-fn clippy_lints_src_files() -> impl Iterator<Item = (PathBuf, DirEntry)> {
-    let root_path = clippy_project_root().join("clippy_lints/src");
-    let iter = WalkDir::new(&root_path).into_iter();
-    iter.map(Result::unwrap)
-        .filter(|f| f.path().extension() == Some(OsStr::new("rs")))
-        .map(move |f| (f.path().strip_prefix(&root_path).unwrap().to_path_buf(), f))
+    })
 }
 
-macro_rules! match_tokens {
-    ($iter:ident, $($token:ident $({$($fields:tt)*})? $(($capture:ident))?)*) => {
-         {
-            $(#[allow(clippy::redundant_pattern)] let Some(LintDeclSearchResult {
-                    token_kind: TokenKind::$token $({$($fields)*})?,
-                    content: $($capture @)? _,
-                    ..
-            }) = $iter.next() else {
-                continue;
-            };)*
-            #[allow(clippy::unused_unit)]
-            { ($($($capture,)?)*) }
+/// Parse a source file looking for `declare_clippy_lint` macro invocations.
+fn parse_clippy_lint_decls(contents: &str, module: &str, lints: &mut Vec<Lint>) {
+    #[allow(clippy::enum_glob_use)]
+    use Token::*;
+    #[rustfmt::skip]
+    static DECL_TOKENS: &[Token] = &[
+        // !{ /// docs
+        Bang, OpenBrace, AnyDoc,
+        // #[clippy::version = "version"]
+        Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, LitStr, CloseBracket,
+        // pub NAME, GROUP,
+        Ident("pub"), CaptureIdent, Comma, CaptureIdent, Comma,
+    ];
+
+    let mut searcher = RustSearcher::new(contents);
+    while searcher.find_token(Ident("declare_clippy_lint")) {
+        let start = searcher.pos() as usize - "declare_clippy_lint".len();
+        let (mut name, mut group) = ("", "");
+        if searcher.match_tokens(DECL_TOKENS, &mut [&mut name, &mut group]) && searcher.find_token(CloseBrace) {
+            lints.push(Lint {
+                name: name.to_lowercase(),
+                group: group.into(),
+                module: module.into(),
+                declaration_range: start..searcher.pos() as usize,
+            });
         }
     }
 }
 
-pub(crate) use match_tokens;
-
-pub(crate) struct LintDeclSearchResult<'a> {
-    pub token_kind: TokenKind,
-    pub content: &'a str,
-    pub range: Range<usize>,
+pub struct DeprecatedLints {
+    pub file: File<'static>,
+    pub contents: String,
+    pub deprecated: Vec<DeprecatedLint>,
+    pub renamed: Vec<RenamedLint>,
+    pub deprecated_end: u32,
+    pub renamed_end: u32,
 }
 
-/// Parse a source file looking for `declare_clippy_lint` macro invocations.
-fn parse_contents(contents: &str, module: &str, lints: &mut Vec<Lint>) {
-    let mut offset = 0usize;
-    let mut iter = tokenize(contents).map(|t| {
-        let range = offset..offset + t.len as usize;
-        offset = range.end;
-
-        LintDeclSearchResult {
-            token_kind: t.kind,
-            content: &contents[range.clone()],
-            range,
-        }
-    });
-
-    while let Some(LintDeclSearchResult { range, .. }) = iter.find(
-        |LintDeclSearchResult {
-             token_kind, content, ..
-         }| token_kind == &TokenKind::Ident && *content == "declare_clippy_lint",
-    ) {
-        let start = range.start;
-        let mut iter = iter
-            .by_ref()
-            .filter(|t| !matches!(t.token_kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
-        // matches `!{`
-        match_tokens!(iter, Bang OpenBrace);
-        match iter.next() {
-            // #[clippy::version = "version"] pub
-            Some(LintDeclSearchResult {
-                token_kind: TokenKind::Pound,
-                ..
-            }) => {
-                match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident);
-            },
-            // pub
-            Some(LintDeclSearchResult {
-                token_kind: TokenKind::Ident,
-                ..
-            }) => (),
-            _ => continue,
-        }
-
-        let (name, group, desc) = match_tokens!(
-            iter,
-            // LINT_NAME
-            Ident(name) Comma
-            // group,
-            Ident(group) Comma
-            // "description"
-            Literal{..}(desc)
-        );
-
-        if let Some(end) = iter.find_map(|t| {
-            if let LintDeclSearchResult {
-                token_kind: TokenKind::CloseBrace,
-                range,
-                ..
-            } = t
-            {
-                Some(range.end)
-            } else {
-                None
-            }
-        }) {
-            lints.push(Lint::new(name, group, desc, module, start..end));
-        }
-    }
-}
-
-/// Parse a source file looking for `declare_deprecated_lint` macro invocations.
-fn parse_deprecated_contents(contents: &str, deprecated: &mut Vec<DeprecatedLint>, renamed: &mut Vec<RenamedLint>) {
-    let Some((_, contents)) = contents.split_once("\ndeclare_with_version! { DEPRECATED") else {
-        return;
-    };
-    let Some((deprecated_src, renamed_src)) = contents.split_once("\ndeclare_with_version! { RENAMED") else {
-        return;
+#[must_use]
+pub fn read_deprecated_lints() -> DeprecatedLints {
+    #[allow(clippy::enum_glob_use)]
+    use Token::*;
+    #[rustfmt::skip]
+    static DECL_TOKENS: &[Token] = &[
+        // #[clippy::version = "version"]
+        Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, LitStr, CloseBracket,
+        // ("first", "second"),
+        OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma,
+    ];
+    #[rustfmt::skip]
+    static DEPRECATED_TOKENS: &[Token] = &[
+        // !{ DEPRECATED(DEPRECATED_VERSION) = [
+        Bang, OpenBrace, Ident("DEPRECATED"), OpenParen, Ident("DEPRECATED_VERSION"), CloseParen, Eq, OpenBracket,
+    ];
+    #[rustfmt::skip]
+    static RENAMED_TOKENS: &[Token] = &[
+        // !{ RENAMED(RENAMED_VERSION) = [
+        Bang, OpenBrace, Ident("RENAMED"), OpenParen, Ident("RENAMED_VERSION"), CloseParen, Eq, OpenBracket,
+    ];
+
+    let path = "clippy_lints/src/deprecated_lints.rs";
+    let mut res = DeprecatedLints {
+        file: File::open(path, OpenOptions::new().read(true).write(true)),
+        contents: String::new(),
+        deprecated: Vec::with_capacity(30),
+        renamed: Vec::with_capacity(80),
+        deprecated_end: 0,
+        renamed_end: 0,
     };
 
-    for line in deprecated_src.lines() {
-        let mut offset = 0usize;
-        let mut iter = tokenize(line).map(|t| {
-            let range = offset..offset + t.len as usize;
-            offset = range.end;
+    res.file.read_append_to_string(&mut res.contents);
+    let mut searcher = RustSearcher::new(&res.contents);
 
-            LintDeclSearchResult {
-                token_kind: t.kind,
-                content: &line[range.clone()],
-                range,
-            }
-        });
+    // First instance is the macro definition.
+    assert!(
+        searcher.find_token(Ident("declare_with_version")),
+        "error reading deprecated lints"
+    );
 
-        let (name, reason) = match_tokens!(
-            iter,
-            // ("old_name",
-            Whitespace OpenParen Literal{kind: LiteralKind::Str{..},..}(name) Comma
-            // "new_name"),
-            Whitespace Literal{kind: LiteralKind::Str{..},..}(reason) CloseParen Comma
-        );
-        deprecated.push(DeprecatedLint::new(name, reason));
+    if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(DEPRECATED_TOKENS, &mut []) {
+        let mut name = "";
+        let mut reason = "";
+        while searcher.match_tokens(DECL_TOKENS, &mut [&mut name, &mut reason]) {
+            res.deprecated.push(DeprecatedLint {
+                name: parse_str_single_line(path.as_ref(), name),
+                reason: parse_str_single_line(path.as_ref(), reason),
+            });
+        }
+    } else {
+        panic!("error reading deprecated lints");
+    }
+    // position of the closing `]}` of `declare_with_version`
+    res.deprecated_end = searcher.pos();
+
+    if searcher.find_token(Ident("declare_with_version")) && searcher.match_tokens(RENAMED_TOKENS, &mut []) {
+        let mut old_name = "";
+        let mut new_name = "";
+        while searcher.match_tokens(DECL_TOKENS, &mut [&mut old_name, &mut new_name]) {
+            res.renamed.push(RenamedLint {
+                old_name: parse_str_single_line(path.as_ref(), old_name),
+                new_name: parse_str_single_line(path.as_ref(), new_name),
+            });
+        }
+    } else {
+        panic!("error reading renamed lints");
     }
-    for line in renamed_src.lines() {
-        let mut offset = 0usize;
-        let mut iter = tokenize(line).map(|t| {
-            let range = offset..offset + t.len as usize;
-            offset = range.end;
-
-            LintDeclSearchResult {
-                token_kind: t.kind,
-                content: &line[range.clone()],
-                range,
-            }
-        });
+    // position of the closing `]}` of `declare_with_version`
+    res.renamed_end = searcher.pos();
 
-        let (old_name, new_name) = match_tokens!(
-            iter,
-            // ("old_name",
-            Whitespace OpenParen Literal{kind: LiteralKind::Str{..},..}(old_name) Comma
-            // "new_name"),
-            Whitespace Literal{kind: LiteralKind::Str{..},..}(new_name) CloseParen Comma
-        );
-        renamed.push(RenamedLint::new(old_name, new_name));
-    }
+    res
 }
 
 /// Removes the line splices and surrounding quotes from a string literal
-fn remove_line_splices(s: &str) -> String {
+fn parse_str_lit(s: &str) -> String {
+    let (s, mode) = if let Some(s) = s.strip_prefix("r") {
+        (s.trim_matches('#'), rustc_literal_escaper::Mode::RawStr)
+    } else {
+        (s, rustc_literal_escaper::Mode::Str)
+    };
     let s = s
-        .strip_prefix('r')
-        .unwrap_or(s)
-        .trim_matches('#')
         .strip_prefix('"')
         .and_then(|s| s.strip_suffix('"'))
         .unwrap_or_else(|| panic!("expected quoted string, found `{s}`"));
     let mut res = String::with_capacity(s.len());
-    unescape_unicode(s, Mode::Str, &mut |range, ch| {
-        if ch.is_ok() {
-            res.push_str(&s[range]);
+    rustc_literal_escaper::unescape_unicode(s, mode, &mut |_, ch| {
+        if let Ok(ch) = ch {
+            res.push(ch);
         }
     });
     res
 }
-fn try_rename_file(old_name: &Path, new_name: &Path) -> bool {
-    match OpenOptions::new().create_new(true).write(true).open(new_name) {
-        Ok(file) => drop(file),
-        Err(e) if matches!(e.kind(), io::ErrorKind::AlreadyExists | io::ErrorKind::NotFound) => return false,
-        Err(e) => panic_file(e, new_name, "create"),
-    }
-    match fs::rename(old_name, new_name) {
-        Ok(()) => true,
-        Err(e) => {
-            drop(fs::remove_file(new_name));
-            if e.kind() == io::ErrorKind::NotFound {
-                false
-            } else {
-                panic_file(e, old_name, "rename");
-            }
-        },
-    }
-}
-
-#[allow(clippy::needless_pass_by_value)]
-fn panic_file(error: io::Error, name: &Path, action: &str) -> ! {
-    panic!("failed to {action} file `{}`: {error}", name.display())
-}
-
-fn insert_at_marker(text: &str, marker: &str, new_text: &str) -> Option<String> {
-    let i = text.find(marker)?;
-    let (pre, post) = text.split_at(i);
-    Some([pre, new_text, post].into_iter().collect())
-}
-
-fn rewrite_file(path: &Path, f: impl FnOnce(&str) -> Option<String>) {
-    let mut file = OpenOptions::new()
-        .write(true)
-        .read(true)
-        .open(path)
-        .unwrap_or_else(|e| panic_file(e, path, "open"));
-    let mut buf = String::new();
-    file.read_to_string(&mut buf)
-        .unwrap_or_else(|e| panic_file(e, path, "read"));
-    if let Some(new_contents) = f(&buf) {
-        file.rewind().unwrap_or_else(|e| panic_file(e, path, "write"));
-        file.write_all(new_contents.as_bytes())
-            .unwrap_or_else(|e| panic_file(e, path, "write"));
-        file.set_len(new_contents.len() as u64)
-            .unwrap_or_else(|e| panic_file(e, path, "write"));
-    }
-}
 
-fn write_file(path: &Path, contents: &str) {
-    fs::write(path, contents).unwrap_or_else(|e| panic_file(e, path, "write"));
+fn parse_str_single_line(path: &Path, s: &str) -> String {
+    let value = parse_str_lit(s);
+    assert!(
+        !value.contains('\n'),
+        "error parsing `{}`: `{s}` should be a single line string",
+        path.display(),
+    );
+    value
 }
 
 #[cfg(test)]
@@ -868,7 +348,7 @@ mod tests {
     use super::*;
 
     #[test]
-    fn test_parse_contents() {
+    fn test_parse_clippy_lint_decls() {
         static CONTENTS: &str = r#"
             declare_clippy_lint! {
                 #[clippy::version = "Hello Clippy!"]
@@ -886,61 +366,25 @@ mod tests {
             }
         "#;
         let mut result = Vec::new();
-        parse_contents(CONTENTS, "module_name", &mut result);
+        parse_clippy_lint_decls(CONTENTS, "module_name", &mut result);
         for r in &mut result {
             r.declaration_range = Range::default();
         }
 
         let expected = vec![
-            Lint::new(
-                "ptr_arg",
-                "style",
-                "\"really long text\"",
-                "module_name",
-                Range::default(),
-            ),
-            Lint::new(
-                "doc_markdown",
-                "pedantic",
-                "\"single line\"",
-                "module_name",
-                Range::default(),
-            ),
+            Lint {
+                name: "ptr_arg".into(),
+                group: "style".into(),
+                module: "module_name".into(),
+                declaration_range: Range::default(),
+            },
+            Lint {
+                name: "doc_markdown".into(),
+                group: "pedantic".into(),
+                module: "module_name".into(),
+                declaration_range: Range::default(),
+            },
         ];
         assert_eq!(expected, result);
     }
-
-    #[test]
-    fn test_by_lint_group() {
-        let lints = vec![
-            Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()),
-            Lint::new(
-                "should_assert_eq2",
-                "group2",
-                "\"abc\"",
-                "module_name",
-                Range::default(),
-            ),
-            Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()),
-        ];
-        let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
-        expected.insert(
-            "group1".to_string(),
-            vec![
-                Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()),
-                Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()),
-            ],
-        );
-        expected.insert(
-            "group2".to_string(),
-            vec![Lint::new(
-                "should_assert_eq2",
-                "group2",
-                "\"abc\"",
-                "module_name",
-                Range::default(),
-            )],
-        );
-        assert_eq!(expected, Lint::by_lint_group(lints.into_iter()));
-    }
 }
diff --git a/clippy_dev/src/utils.rs b/clippy_dev/src/utils.rs
index 206816398f5..ae2eabc45dd 100644
--- a/clippy_dev/src/utils.rs
+++ b/clippy_dev/src/utils.rs
@@ -1,12 +1,113 @@
+use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
+use core::fmt::{self, Display};
+use core::slice;
+use core::str::FromStr;
+use rustc_lexer::{self as lexer, FrontmatterAllowed};
+use std::env;
+use std::fs::{self, OpenOptions};
+use std::io::{self, Read as _, Seek as _, SeekFrom, Write};
 use std::path::{Path, PathBuf};
 use std::process::{self, ExitStatus};
-use std::{fs, io};
 
 #[cfg(not(windows))]
 static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
 #[cfg(windows)]
 static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
 
+#[derive(Clone, Copy)]
+pub enum FileAction {
+    Open,
+    Read,
+    Write,
+    Create,
+    Rename,
+}
+impl FileAction {
+    fn as_str(self) -> &'static str {
+        match self {
+            Self::Open => "opening",
+            Self::Read => "reading",
+            Self::Write => "writing",
+            Self::Create => "creating",
+            Self::Rename => "renaming",
+        }
+    }
+}
+
+#[cold]
+#[track_caller]
+pub fn panic_file(err: &impl Display, action: FileAction, path: &Path) -> ! {
+    panic!("error {} `{}`: {}", action.as_str(), path.display(), *err)
+}
+
+/// Wrapper around `std::fs::File` which panics with a path on failure.
+pub struct File<'a> {
+    pub inner: fs::File,
+    pub path: &'a Path,
+}
+impl<'a> File<'a> {
+    /// Opens a file panicking on failure.
+    #[track_caller]
+    pub fn open(path: &'a (impl AsRef<Path> + ?Sized), options: &mut OpenOptions) -> Self {
+        let path = path.as_ref();
+        match options.open(path) {
+            Ok(inner) => Self { inner, path },
+            Err(e) => panic_file(&e, FileAction::Open, path),
+        }
+    }
+
+    /// Opens a file if it exists, panicking on any other failure.
+    #[track_caller]
+    pub fn open_if_exists(path: &'a (impl AsRef<Path> + ?Sized), options: &mut OpenOptions) -> Option<Self> {
+        let path = path.as_ref();
+        match options.open(path) {
+            Ok(inner) => Some(Self { inner, path }),
+            Err(e) if e.kind() == io::ErrorKind::NotFound => None,
+            Err(e) => panic_file(&e, FileAction::Open, path),
+        }
+    }
+
+    /// Opens and reads a file into a string, panicking of failure.
+    #[track_caller]
+    pub fn open_read_to_cleared_string<'dst>(
+        path: &'a (impl AsRef<Path> + ?Sized),
+        dst: &'dst mut String,
+    ) -> &'dst mut String {
+        Self::open(path, OpenOptions::new().read(true)).read_to_cleared_string(dst)
+    }
+
+    /// Read the entire contents of a file to the given buffer.
+    #[track_caller]
+    pub fn read_append_to_string<'dst>(&mut self, dst: &'dst mut String) -> &'dst mut String {
+        match self.inner.read_to_string(dst) {
+            Ok(_) => {},
+            Err(e) => panic_file(&e, FileAction::Read, self.path),
+        }
+        dst
+    }
+
+    #[track_caller]
+    pub fn read_to_cleared_string<'dst>(&mut self, dst: &'dst mut String) -> &'dst mut String {
+        dst.clear();
+        self.read_append_to_string(dst)
+    }
+
+    /// Replaces the entire contents of a file.
+    #[track_caller]
+    pub fn replace_contents(&mut self, data: &[u8]) {
+        let res = match self.inner.seek(SeekFrom::Start(0)) {
+            Ok(_) => match self.inner.write_all(data) {
+                Ok(()) => self.inner.set_len(data.len() as u64),
+                Err(e) => Err(e),
+            },
+            Err(e) => Err(e),
+        };
+        if let Err(e) = res {
+            panic_file(&e, FileAction::Write, self.path);
+        }
+    }
+}
+
 /// Returns the path to the `cargo-clippy` binary
 ///
 /// # Panics
@@ -14,34 +115,107 @@ static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
 /// Panics if the path of current executable could not be retrieved.
 #[must_use]
 pub fn cargo_clippy_path() -> PathBuf {
-    let mut path = std::env::current_exe().expect("failed to get current executable name");
+    let mut path = env::current_exe().expect("failed to get current executable name");
     path.set_file_name(CARGO_CLIPPY_EXE);
     path
 }
 
-/// Returns the path to the Clippy project directory
-///
-/// # Panics
-///
-/// Panics if the current directory could not be retrieved, there was an error reading any of the
-/// Cargo.toml files or ancestor directory is the clippy root directory
-#[must_use]
-pub fn clippy_project_root() -> PathBuf {
-    let current_dir = std::env::current_dir().unwrap();
-    for path in current_dir.ancestors() {
-        let result = fs::read_to_string(path.join("Cargo.toml"));
-        if let Err(err) = &result
-            && err.kind() == io::ErrorKind::NotFound
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub struct Version {
+    pub major: u16,
+    pub minor: u16,
+}
+impl FromStr for Version {
+    type Err = ();
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if let Some(s) = s.strip_prefix("0.")
+            && let Some((major, minor)) = s.split_once('.')
+            && let Ok(major) = major.parse()
+            && let Ok(minor) = minor.parse()
         {
-            continue;
+            Ok(Self { major, minor })
+        } else {
+            Err(())
+        }
+    }
+}
+impl Version {
+    /// Displays the version as a rust version. i.e. `x.y.0`
+    #[must_use]
+    pub fn rust_display(self) -> impl Display {
+        struct X(Version);
+        impl Display for X {
+            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+                write!(f, "{}.{}.0", self.0.major, self.0.minor)
+            }
+        }
+        X(self)
+    }
+
+    /// Displays the version as it should appear in clippy's toml files. i.e. `0.x.y`
+    #[must_use]
+    pub fn toml_display(self) -> impl Display {
+        struct X(Version);
+        impl Display for X {
+            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+                write!(f, "0.{}.{}", self.0.major, self.0.minor)
+            }
         }
+        X(self)
+    }
+}
+
+pub struct ClippyInfo {
+    pub path: PathBuf,
+    pub version: Version,
+}
+impl ClippyInfo {
+    #[must_use]
+    pub fn search_for_manifest() -> Self {
+        let mut path = env::current_dir().expect("error reading the working directory");
+        let mut buf = String::new();
+        loop {
+            path.push("Cargo.toml");
+            if let Some(mut file) = File::open_if_exists(&path, OpenOptions::new().read(true)) {
+                let mut in_package = false;
+                let mut is_clippy = false;
+                let mut version: Option<Version> = None;
+
+                // Ad-hoc parsing to avoid dependencies. We control all the file so this
+                // isn't actually a problem
+                for line in file.read_to_cleared_string(&mut buf).lines() {
+                    if line.starts_with('[') {
+                        in_package = line.starts_with("[package]");
+                    } else if in_package && let Some((name, value)) = line.split_once('=') {
+                        match name.trim() {
+                            "name" => is_clippy = value.trim() == "\"clippy\"",
+                            "version"
+                                if let Some(value) = value.trim().strip_prefix('"')
+                                    && let Some(value) = value.strip_suffix('"') =>
+                            {
+                                version = value.parse().ok();
+                            },
+                            _ => {},
+                        }
+                    }
+                }
 
-        let content = result.unwrap();
-        if content.contains("[package]\nname = \"clippy\"") {
-            return path.to_path_buf();
+                if is_clippy {
+                    let Some(version) = version else {
+                        panic!("error reading clippy version from {}", file.path.display());
+                    };
+                    path.pop();
+                    return ClippyInfo { path, version };
+                }
+            }
+
+            path.pop();
+            assert!(
+                path.pop(),
+                "error finding project root, please run from inside the clippy directory"
+            );
         }
     }
-    panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
 }
 
 /// # Panics
@@ -57,86 +231,396 @@ pub fn exit_if_err(status: io::Result<ExitStatus>) {
     }
 }
 
-pub(crate) fn clippy_version() -> (u32, u32) {
-    fn parse_manifest(contents: &str) -> Option<(u32, u32)> {
-        let version = contents
-            .lines()
-            .filter_map(|l| l.split_once('='))
-            .find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
-        let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
-            return None;
-        };
-        let (minor, patch) = version.split_once('.')?;
-        Some((minor.parse().ok()?, patch.parse().ok()?))
+#[derive(Clone, Copy)]
+pub enum UpdateStatus {
+    Unchanged,
+    Changed,
+}
+impl UpdateStatus {
+    #[must_use]
+    pub fn from_changed(value: bool) -> Self {
+        if value { Self::Changed } else { Self::Unchanged }
+    }
+
+    #[must_use]
+    pub fn is_changed(self) -> bool {
+        matches!(self, Self::Changed)
     }
-    let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
-    parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
 }
 
-#[derive(Clone, Copy, PartialEq, Eq)]
+#[derive(Clone, Copy)]
 pub enum UpdateMode {
-    Check,
     Change,
+    Check,
+}
+impl UpdateMode {
+    #[must_use]
+    pub fn from_check(check: bool) -> Self {
+        if check { Self::Check } else { Self::Change }
+    }
 }
 
-pub(crate) fn exit_with_failure() {
-    println!(
-        "Not all lints defined properly. \
-                 Please run `cargo dev update_lints` to make sure all lints are defined properly."
-    );
-    process::exit(1);
+#[derive(Default)]
+pub struct FileUpdater {
+    src_buf: String,
+    dst_buf: String,
 }
+impl FileUpdater {
+    fn update_file_checked_inner(
+        &mut self,
+        tool: &str,
+        mode: UpdateMode,
+        path: &Path,
+        update: &mut dyn FnMut(&Path, &str, &mut String) -> UpdateStatus,
+    ) {
+        let mut file = File::open(path, OpenOptions::new().read(true).write(true));
+        file.read_to_cleared_string(&mut self.src_buf);
+        self.dst_buf.clear();
+        match (mode, update(path, &self.src_buf, &mut self.dst_buf)) {
+            (UpdateMode::Check, UpdateStatus::Changed) => {
+                eprintln!(
+                    "the contents of `{}` are out of date\nplease run `{tool}` to update",
+                    path.display()
+                );
+                process::exit(1);
+            },
+            (UpdateMode::Change, UpdateStatus::Changed) => file.replace_contents(self.dst_buf.as_bytes()),
+            (UpdateMode::Check | UpdateMode::Change, UpdateStatus::Unchanged) => {},
+        }
+    }
 
-/// Replaces a region in a file delimited by two lines matching regexes.
-///
-/// `path` is the relative path to the file on which you want to perform the replacement.
-///
-/// See `replace_region_in_text` for documentation of the other options.
-///
-/// # Panics
-///
-/// Panics if the path could not read or then written
-pub(crate) fn replace_region_in_file(
-    update_mode: UpdateMode,
+    fn update_file_inner(&mut self, path: &Path, update: &mut dyn FnMut(&Path, &str, &mut String) -> UpdateStatus) {
+        let mut file = File::open(path, OpenOptions::new().read(true).write(true));
+        file.read_to_cleared_string(&mut self.src_buf);
+        self.dst_buf.clear();
+        if update(path, &self.src_buf, &mut self.dst_buf).is_changed() {
+            file.replace_contents(self.dst_buf.as_bytes());
+        }
+    }
+
+    pub fn update_file_checked(
+        &mut self,
+        tool: &str,
+        mode: UpdateMode,
+        path: impl AsRef<Path>,
+        update: &mut dyn FnMut(&Path, &str, &mut String) -> UpdateStatus,
+    ) {
+        self.update_file_checked_inner(tool, mode, path.as_ref(), update);
+    }
+
+    #[expect(clippy::type_complexity)]
+    pub fn update_files_checked(
+        &mut self,
+        tool: &str,
+        mode: UpdateMode,
+        files: &mut [(
+            impl AsRef<Path>,
+            &mut dyn FnMut(&Path, &str, &mut String) -> UpdateStatus,
+        )],
+    ) {
+        for (path, update) in files {
+            self.update_file_checked_inner(tool, mode, path.as_ref(), update);
+        }
+    }
+
+    pub fn update_file(
+        &mut self,
+        path: impl AsRef<Path>,
+        update: &mut dyn FnMut(&Path, &str, &mut String) -> UpdateStatus,
+    ) {
+        self.update_file_inner(path.as_ref(), update);
+    }
+}
+
+/// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
+/// were found, or the missing delimiter if not.
+pub fn update_text_region(
     path: &Path,
     start: &str,
     end: &str,
-    write_replacement: impl FnMut(&mut String),
-) {
-    let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {e}", path.display()));
-    let new_contents = match replace_region_in_text(&contents, start, end, write_replacement) {
-        Ok(x) => x,
-        Err(delim) => panic!("Couldn't find `{delim}` in file `{}`", path.display()),
+    src: &str,
+    dst: &mut String,
+    insert: &mut impl FnMut(&mut String),
+) -> UpdateStatus {
+    let Some((src_start, src_end)) = src.split_once(start) else {
+        panic!("`{}` does not contain `{start}`", path.display());
     };
+    let Some((replaced_text, src_end)) = src_end.split_once(end) else {
+        panic!("`{}` does not contain `{end}`", path.display());
+    };
+    dst.push_str(src_start);
+    dst.push_str(start);
+    let new_start = dst.len();
+    insert(dst);
+    let changed = dst[new_start..] != *replaced_text;
+    dst.push_str(end);
+    dst.push_str(src_end);
+    UpdateStatus::from_changed(changed)
+}
+
+pub fn update_text_region_fn(
+    start: &str,
+    end: &str,
+    mut insert: impl FnMut(&mut String),
+) -> impl FnMut(&Path, &str, &mut String) -> UpdateStatus {
+    move |path, src, dst| update_text_region(path, start, end, src, dst, &mut insert)
+}
+
+#[must_use]
+pub fn is_ident_char(c: u8) -> bool {
+    matches!(c, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_')
+}
+
+pub struct StringReplacer<'a> {
+    searcher: AhoCorasick,
+    replacements: &'a [(&'a str, &'a str)],
+}
+impl<'a> StringReplacer<'a> {
+    #[must_use]
+    pub fn new(replacements: &'a [(&'a str, &'a str)]) -> Self {
+        Self {
+            searcher: AhoCorasickBuilder::new()
+                .match_kind(aho_corasick::MatchKind::LeftmostLongest)
+                .build(replacements.iter().map(|&(x, _)| x))
+                .unwrap(),
+            replacements,
+        }
+    }
+
+    /// Replace substrings if they aren't bordered by identifier characters.
+    pub fn replace_ident_fn(&self) -> impl Fn(&Path, &str, &mut String) -> UpdateStatus {
+        move |_, src, dst| {
+            let mut pos = 0;
+            let mut changed = false;
+            for m in self.searcher.find_iter(src) {
+                if !is_ident_char(src.as_bytes().get(m.start().wrapping_sub(1)).copied().unwrap_or(0))
+                    && !is_ident_char(src.as_bytes().get(m.end()).copied().unwrap_or(0))
+                {
+                    changed = true;
+                    dst.push_str(&src[pos..m.start()]);
+                    dst.push_str(self.replacements[m.pattern()].1);
+                    pos = m.end();
+                }
+            }
+            dst.push_str(&src[pos..]);
+            UpdateStatus::from_changed(changed)
+        }
+    }
+}
+
+#[derive(Clone, Copy)]
+pub enum Token {
+    /// Matches any number of doc comments.
+    AnyDoc,
+    Ident(&'static str),
+    CaptureIdent,
+    LitStr,
+    CaptureLitStr,
+    Bang,
+    CloseBrace,
+    CloseBracket,
+    CloseParen,
+    /// This will consume the first colon even if the second doesn't exist.
+    DoubleColon,
+    Comma,
+    Eq,
+    Lifetime,
+    Lt,
+    Gt,
+    OpenBrace,
+    OpenBracket,
+    OpenParen,
+    Pound,
+}
+
+pub struct RustSearcher<'txt> {
+    text: &'txt str,
+    cursor: lexer::Cursor<'txt>,
+    pos: u32,
+
+    // Either the next token or a zero-sized whitespace sentinel.
+    next_token: lexer::Token,
+}
+impl<'txt> RustSearcher<'txt> {
+    #[must_use]
+    pub fn new(text: &'txt str) -> Self {
+        Self {
+            text,
+            cursor: lexer::Cursor::new(text, FrontmatterAllowed::Yes),
+            pos: 0,
+
+            // Sentinel value indicating there is no read token.
+            next_token: lexer::Token {
+                len: 0,
+                kind: lexer::TokenKind::Whitespace,
+            },
+        }
+    }
+
+    #[must_use]
+    pub fn peek_text(&self) -> &'txt str {
+        &self.text[self.pos as usize..(self.pos + self.next_token.len) as usize]
+    }
+
+    #[must_use]
+    pub fn peek(&self) -> lexer::TokenKind {
+        self.next_token.kind
+    }
+
+    #[must_use]
+    pub fn pos(&self) -> u32 {
+        self.pos
+    }
+
+    #[must_use]
+    pub fn at_end(&self) -> bool {
+        self.next_token.kind == lexer::TokenKind::Eof
+    }
+
+    pub fn step(&mut self) {
+        // `next_len` is zero for the sentinel value and the eof marker.
+        self.pos += self.next_token.len;
+        self.next_token = self.cursor.advance_token();
+    }
+
+    /// Consumes the next token if it matches the requested value and captures the value if
+    /// requested. Returns true if a token was matched.
+    fn read_token(&mut self, token: Token, captures: &mut slice::IterMut<'_, &mut &'txt str>) -> bool {
+        loop {
+            match (token, self.next_token.kind) {
+                // Has to be the first match arm so the empty sentinel token will be handled.
+                // This will also skip all whitespace/comments preceding any tokens.
+                (
+                    _,
+                    lexer::TokenKind::Whitespace
+                    | lexer::TokenKind::LineComment { doc_style: None }
+                    | lexer::TokenKind::BlockComment {
+                        doc_style: None,
+                        terminated: true,
+                    },
+                ) => {
+                    self.step();
+                    if self.at_end() {
+                        // `AnyDoc` always matches.
+                        return matches!(token, Token::AnyDoc);
+                    }
+                },
+                (
+                    Token::AnyDoc,
+                    lexer::TokenKind::BlockComment { terminated: true, .. } | lexer::TokenKind::LineComment { .. },
+                ) => {
+                    self.step();
+                    if self.at_end() {
+                        // `AnyDoc` always matches.
+                        return true;
+                    }
+                },
+                (Token::AnyDoc, _) => return true,
+                (Token::Bang, lexer::TokenKind::Bang)
+                | (Token::CloseBrace, lexer::TokenKind::CloseBrace)
+                | (Token::CloseBracket, lexer::TokenKind::CloseBracket)
+                | (Token::CloseParen, lexer::TokenKind::CloseParen)
+                | (Token::Comma, lexer::TokenKind::Comma)
+                | (Token::Eq, lexer::TokenKind::Eq)
+                | (Token::Lifetime, lexer::TokenKind::Lifetime { .. })
+                | (Token::Lt, lexer::TokenKind::Lt)
+                | (Token::Gt, lexer::TokenKind::Gt)
+                | (Token::OpenBrace, lexer::TokenKind::OpenBrace)
+                | (Token::OpenBracket, lexer::TokenKind::OpenBracket)
+                | (Token::OpenParen, lexer::TokenKind::OpenParen)
+                | (Token::Pound, lexer::TokenKind::Pound)
+                | (
+                    Token::LitStr,
+                    lexer::TokenKind::Literal {
+                        kind: lexer::LiteralKind::Str { terminated: true } | lexer::LiteralKind::RawStr { .. },
+                        ..
+                    },
+                ) => {
+                    self.step();
+                    return true;
+                },
+                (Token::Ident(x), lexer::TokenKind::Ident) if x == self.peek_text() => {
+                    self.step();
+                    return true;
+                },
+                (Token::DoubleColon, lexer::TokenKind::Colon) => {
+                    self.step();
+                    if !self.at_end() && matches!(self.next_token.kind, lexer::TokenKind::Colon) {
+                        self.step();
+                        return true;
+                    }
+                    return false;
+                },
+                (
+                    Token::CaptureLitStr,
+                    lexer::TokenKind::Literal {
+                        kind: lexer::LiteralKind::Str { terminated: true } | lexer::LiteralKind::RawStr { .. },
+                        ..
+                    },
+                )
+                | (Token::CaptureIdent, lexer::TokenKind::Ident) => {
+                    **captures.next().unwrap() = self.peek_text();
+                    self.step();
+                    return true;
+                },
+                _ => return false,
+            }
+        }
+    }
+
+    #[must_use]
+    pub fn find_token(&mut self, token: Token) -> bool {
+        let mut capture = [].iter_mut();
+        while !self.read_token(token, &mut capture) {
+            self.step();
+            if self.at_end() {
+                return false;
+            }
+        }
+        true
+    }
+
+    #[must_use]
+    pub fn find_capture_token(&mut self, token: Token) -> Option<&'txt str> {
+        let mut res = "";
+        let mut capture = &mut res;
+        let mut capture = slice::from_mut(&mut capture).iter_mut();
+        while !self.read_token(token, &mut capture) {
+            self.step();
+            if self.at_end() {
+                return None;
+            }
+        }
+        Some(res)
+    }
+
+    #[must_use]
+    pub fn match_tokens(&mut self, tokens: &[Token], captures: &mut [&mut &'txt str]) -> bool {
+        let mut captures = captures.iter_mut();
+        tokens.iter().all(|&t| self.read_token(t, &mut captures))
+    }
+}
 
-    match update_mode {
-        UpdateMode::Check if contents != new_contents => exit_with_failure(),
-        UpdateMode::Check => (),
-        UpdateMode::Change => {
-            if let Err(e) = fs::write(path, new_contents.as_bytes()) {
-                panic!("Cannot write to `{}`: {e}", path.display());
+#[expect(clippy::must_use_candidate)]
+pub fn try_rename_file(old_name: &Path, new_name: &Path) -> bool {
+    match OpenOptions::new().create_new(true).write(true).open(new_name) {
+        Ok(file) => drop(file),
+        Err(e) if matches!(e.kind(), io::ErrorKind::AlreadyExists | io::ErrorKind::NotFound) => return false,
+        Err(e) => panic_file(&e, FileAction::Create, new_name),
+    }
+    match fs::rename(old_name, new_name) {
+        Ok(()) => true,
+        Err(e) => {
+            drop(fs::remove_file(new_name));
+            if e.kind() == io::ErrorKind::NotFound {
+                false
+            } else {
+                panic_file(&e, FileAction::Rename, old_name);
             }
         },
     }
 }
 
-/// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
-/// were found, or the missing delimiter if not.
-pub(crate) fn replace_region_in_text<'a>(
-    text: &str,
-    start: &'a str,
-    end: &'a str,
-    mut write_replacement: impl FnMut(&mut String),
-) -> Result<String, &'a str> {
-    let (text_start, rest) = text.split_once(start).ok_or(start)?;
-    let (_, text_end) = rest.split_once(end).ok_or(end)?;
-
-    let mut res = String::with_capacity(text.len() + 4096);
-    res.push_str(text_start);
-    res.push_str(start);
-    write_replacement(&mut res);
-    res.push_str(end);
-    res.push_str(text_end);
-
-    Ok(res)
+pub fn write_file(path: &Path, contents: &str) {
+    fs::write(path, contents).unwrap_or_else(|e| panic_file(&e, FileAction::Write, path));
 }