rust-lang / rust-lang/rustfmt

Errantly formatting multiline macro

Open
#6,233 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

I have this macro call here that is formatted by rustfmt as so.

$ rustfmt --check godot-macros/src/class/data_models/field_export.rs
Diff in /home/fp/3rd/gdext/godot-macros/src/class/data_models/field_export.rs at line 402:
                 }?;
                 let deprecation_warning = if *radians {
                     quote! {
-			#export_func;
-			::godot::__deprecated::emit_deprecated_warning!(export_range_radians);
-                    }
+                    #export_func;
+                    ::godot::__deprecated::emit_deprecated_warning!(export_range_radians);
+                            }
                 } else {
                     quote! { #export_func }
                 };
$ rustfmt --version
rustfmt 1.7.0-stable (129f3b9 2024-06-10)

You can see here that rustfmt wants to un-indent the macro contents and double indent the end bracket for some reason.

Here's aa MCVE for this issue. The important details are that the innermost expressions are within a curly bracket-delimited macro, which itself is within 3 levels of scope.

// correct formatting
fn bar() {
    {
        {
            foo! {
		export_func;
                warning;
            }
        }
    }
}
// rustfmt output
fn bar() {
    {
        {
            foo! {
            export_func;
                    warning;
                }
        }
    }
}

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 issue with the three-level nested MCVE and rustfmt 1.7.0, comparing the shown expected and actual formatting. Trace the macro-formatting path responsible for curly-bracket-delimited macros, then add a regression test demonstrating that the macro contents and closing brace retain the intended indentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.