Skip to content

Incorrect "struct_variant_width cannot have a value that exceeds max_width" when using macros #7147

Closed

Description

@Techcable

Summary

Using struct_variant_width = 0 config option triggers an incorrect warning when encountering a macro.

Using this code:

macro_rules! m {
    () => {};
}

With config struct_variant_width =0, gives the following warning

`struct_variant_width` cannot have a value that exceeds `max_width`. `struct_variant_width` will be set to the same value as `max_width`

Potential Clause

Claude Opus 5.5 minimized this bug. It thinks the issue was introduced by PR #6651. The new adjust_max_width function will underflow if any of the width configs are smaller than the indenetation (permalink).

fn adjust_max_width(&mut self, delta: isize) {
    // overflows whenever value < -delta
    let adjust = |value: usize| (value as isize + delta) as usize;

    // if `self.array_width = 0`, this will overflow for delta < 0
    self.array_width.2 = adjust(self.array_width.2);
    // <similar adjustments>
    self.struct_variant_width.2 = adjust(self.struct_variant_width.2); // this is likely causing the issue
    
    self.max_width.2 = adjust(self.max_width.2);
    self.set_heuristics();
}

This seems like a resonable theory, but I have not yet confirmed it.

If this theory is correct, claude suggests using usize::saturating_add_signed to avoid this underflow.

Meta

Version it worked on

This worked just fine on nightly-2026-09-16.

Version with regression

The problem occurs on rustfmt 1.11.0-nightly (75a75c3e0a 2026-09-26)

Activity

  1. added
    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE
    on Sep 27, 2026
  2. added
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    I-prioritizeNominated for prioritization
    on Sep 27, 2026
  3. self-assigned this
    on Sep 28, 2026
  4. added
    A-macrosArea: macros (procedural macros, macro_rules! macros, etc.)
    P-highHigh priority
    and removed
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    I-prioritizeNominated for prioritization
    P-highHigh priority
    on Sep 28, 2026
  5. added 2 commits that reference this issue on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-macrosArea: macros (procedural macros, macro_rules! macros, etc.)C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEP-highHigh prioritySO-struct_variant_widthStable option: struct_variant_widthregression-from-stable-to-betaRegression from stable to beta.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions