`struct_field_align_threshold` counts attributes in the alignment width of struct literal fields

Abierto
#7,203 4 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@tkr-sh ya está trabajando en esto.

Desde el 24/10/2026.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
57/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust

Línea de trabajo

Read the struct_field_align_threshold formatting logic and the context in the linked discussion for #7202; the issue provides Rust snippets showing how attributes affect alignment. Compare the current output with the proposed field-only alignment behavior and determine the expected treatment of multiline attributes. Done means the behavior is agreed upon and covered by formatting tests for the relevant cases.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

C-bug I-poor-formatting UO-struct_field_align_threshold

Hello,

This is a follow-up to #7202.
The PR was closed with the request to open an issue and discuss the expected behavior first, so here it is.

I noticed that when using the struct_field_align_threshold nightly option, there was a missbehavior where:

Self {
    a: 1,
    #[cfg(feature = "test")]
    longer: 2,
}

was rewritten as:

Self {
    a:                               1,
    #[cfg(feature = "test")]
    longer:                          2,
}

as if it was

    #[cfg(feature = "test")] longer: 2,

Full context of why it occurs and why I think it's a bug here: https://github.com/rust-lang/rustfmt/pull/7202#issue-5951552048

Expected behavior

There is afaik 2 possible solutions, and one that seems better.

Option A: align to fields without care of attributes (imo the expected behavior)
Self {
    a:      1,
    #[cfg(feature = "test")]
    longer: 2,
}
Option B: align to max(field.len(), attribute.len())
Self {
    a:                       1,
    #[cfg(feature = "test")]
    longer:                  2,
}

Personally, this one seems a bit odd to me for 4 reasons:

  1. This is not following the name of the rusfmt option: struct_field_align_threshold (struct's field)
  2. Let's say that we reorder the arguments order to have the cfg above with longer:
Self {
    #[cfg(feature = "test")]
    longer:                  2,
    a:                       1,
}

I feel like this is really confusing compared to

Self {
    #[cfg(feature = "test")]
    longer: 2,
    a:      1,
}
  1. This is not the behavior of a field definition:
// Current behavior of a struct / field definition.
struct Test {
    a:      u8,
    #[cfg(feature = "test")]
    longer: u8,
}
  1. If the goal is to avoid to have things "stick out", it will probably have the opposite effect:
Test {
    a:                     String::from(
        "aaaaaaaaaaaaaaa",
    ),
    #[cfg(feature = "a")]
    vvvvv:                 vec![],
}

vs

Test {
    a:     String::from(
        "aaaaaaaaaaaaaaa",
    ),
    #[cfg(feature = "a")]
    vvvvv: vec![],
}

Which also has more chance to just be

Self {
    a:     String::from("aaaaaaaaaaaaaaa"),
    #[cfg(feature = "a")]
    vvvvv: vec![],
}

because it will usually be shorter.


Also remains the question of how to behave on multiline attribute with some parts sticking out:

Self {
    a:                    String::from("aaaaaaaaaaaaaaa"),
    #[cfg(
        feature = "aaaaa"
    )]
    vvvvv:                vec![],
}

which would be easily confusing, specially with wrapping:

Self {
    a:                    String::from(
        "aaaaaaaaaaaaaaa"
    ),
    #[cfg(
        feature = "aaaaa"
    )]
    vvvvv:                vec![],
}

vs

Self {
    a:     String::from("aaaaaaaaaaaaaaa"),
    #[cfg(
        feature = "aaaaa"
    )]
    vvvvv: vec![],
}

and

Self {
    a:     String::from(
        "aaaaaaaaaaaaaaa"
    ),
    #[cfg(
        feature = "aaaaa"
    )]
    vvvvv: vec![],
}

All of these elements combined + the current behavior of struct field definition, makes me lean towards option A.

Lenguaje dominante
Rust
Estrellas
7.1k
Forks
1.2k
Merge medio
1 d 4 h
PR fusionados (30 d)
20

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rustfmt

Todos los issues de rust-lang/rustfmt

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.