Skip to content

format_strings can cause code to no longer be reformatted #6576

Open
@VorpalBlade

Description

@VorpalBlade

The following code fragment:

fn sanity_check(
    path: &Utf8Path,
    style: Style,
    chezmoi: &impl Chezmoi,
) -> Result<(), anyhow::Error> {
    if !path.is_file() {
        return Err(anyhow!("{} is not a regular file", path));
    }
    if Style::InPath == style && chezmoi.version()? < CHEZMOI_AUTO_SOURCE_VERSION {
        return Err(anyhow!(
            "To use \"--style path\" you need chezmoi {CHEZMOI_AUTO_SOURCE_VERSION} or newer"
        ));
    }
    match crate::doctor::hook_paths(chezmoi)?.as_slice() {
        [] => Ok(()),
        _ => {
            Err(anyhow!("Legacy hook script found, see chezmoi_modify_manager --doctor and please read https://github.com/VorpalBlade/chezmoi_modify_manager/blob/main/doc/migration_3.md"))
        }
    }
}

Normally gets reformatted as follows (using edition 2024):

diff --git a/src/add.rs b/src/add.rs
index c8d2689..d2a9f58 100644
--- a/src/add.rs
+++ b/src/add.rs
@@ -340,9 +340,9 @@ fn sanity_check(
     }
     match crate::doctor::hook_paths(chezmoi)?.as_slice() {
         [] => Ok(()),
-        _ => {
-            Err(anyhow!("Legacy hook script found, see chezmoi_modify_manager --doctor and please read https://github.com/VorpalBlade/chezmoi_modify_manager/blob/main/doc/migration_3.md"))
-        }
+        _ => Err(anyhow!(
+            "Legacy hook script found, see chezmoi_modify_manager --doctor and please read https://github.com/VorpalBlade/chezmoi_modify_manager/blob/main/doc/migration_3.md"
+        )),
     }
 }

However, if I enable the nightly format_strings = true, this reformatting doesn't take place. But if it is already reformatted, the code isn't touched (so it is "bi-stable" as it were).

I don't see why format_strings should affect this at all. Tried with rustc 1.89.0-nightly (d13a431a6 2025-06-09) /rustfmt 1.8.0-nightly (d13a431a6c 2025-06-09) (strange that they have separate version numbers, didn't know that).

This can be seen at commit 1308218f328882a1b452e0a3e8d84754e4d6863b in https://github.com/VorpalBlade/chezmoi_modify_manager (two files are affected by the exact same issue when using "format_strings = true" and nightly rustfmt).

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-stringsString literalsonly-with-optionrequires a non-default option value to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions