Skip to content

rustfmt shifts macro body 4 indent levels to the right for no reason #5832

Open
@Boscop

Description

@Boscop

Version:

$ cargo fmt --version
rustfmt 1.6.0-nightly (1065d87 2023-07-09)

Config:

edition = "2021"
version = "Two"
max_width = 110
newline_style = "Unix"
use_small_heuristics = "Max"
tab_spaces = 2
imports_granularity = "Crate"
use_field_init_shorthand = true
use_try_shorthand = true
spaces_around_ranges = true
overflow_delimited_expr = true

I have this code:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
          // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
          $node.ports[$port_id]
        }
      }

after running rustfmt, it shifted the inner macro body 4 indent levels (4*2 spaces) to the right for no reason!:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
                  // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
                  $node.ports[$port_id]
                }
      }

Note: This doesn't happen when the comment is shorter like:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
          // TODO: Assign to val instead of re-reading on every access
          $node.ports[$port_id]
        }
      }

It also doesn't happen when the original long comment is before the inner macro like:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
        macro $name() {
          $node.ports[$port_id]
        }
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-macrosbugPanic, non-idempotency, invalid code, etc.p-low

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions