rust-lang / rust-lang/rustfmt

`format_strings` can cause code to no longer be reformatted

Open
#6,576 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-strings only-with-option
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior at commit 1308218f328882a1b452e0a3e8d84754e4d6863b in the linked chezmoi_modify_manager repository using the reported nightly rustc and rustfmt versions. Compare formatting with and without format_strings = true; done means the affected code is reformatted consistently rather than remaining bi-stable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.