`--file-lines` statement preservation can result in poorly formatted code

Aperta
#7,257 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
tooling

Direzione di ricerca

Start by reproducing with the exact command in the report: create test.rs with the sample code and run cargo run --bin rustfmt -- --unstable-features --file-lines '[{"file":"test.rs","range":[2,2]}]' test.rs. Then read PR #6867, which added the statement-preservation logic (including preserved leading whitespace), to find where unselected statements are emitted unchanged while their enclosing block is re-indented. Done means the partial-format output keeps preserved statements aligned with the new indentation of the block, matching whole-file formatting of the surrounding lines, with a regression test in the rustfmt test suite for --file-lines.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-statements C-bug C-discussion UO-file_lines

https://github.com/rust-lang/rustfmt/pull/6867 introduced logic for preserving statements not covered by the --file-lines selection, including preserving the leading whitespace. This can result in very poorly formatted code when the block containing the statement changes indentation while the statement stays in place.

Summary

When using --file-lines to partially format a file, it's possible that a partially-selected block gets rewritten while its contents do not. This results in very weirdly formatted code that feels incorrect. This happens primarily when the indentation of a block or closure changes while the contents of that block are unselected. This leaves unselected statements within the block at an incorrect indentation compared to their new surroundings.

Consider the following code:

fn main() {
    call(super_long_argument_that_forces_vertical_layout_______________________________, || {
        statement;
    });
}

Let's say I've changed the first argument of the call, so I want to format the file with line 2 selected.

Expected behavior

I'd expect to get something that's fairly consistent with what whole-file formatting produces:

fn main() {
    call(
        super_long_argument_that_forces_vertical_layout_______________________________,
        || {
            statement;
        },
    );
}
Actual behavior

The unselected statement and the closing brace of the closure end up keeping their indentation, resulting in messy code:

fn main() {
    call(
        super_long_argument_that_forces_vertical_layout_______________________________,
        || {
        statement;
    },
    );
}

Configuration

rustfmt cli options used (if applicable):

cargo run --bin rustfmt -- --unstable-features --file-lines '[{"file":"test.rs","range":[2,2]}]' test.rs

Meta

Tested against main commit de3a4b0c3f93e544eb4c22bba905a1c8a4e5067f.

Lingua principale
Rust
Stelle
7.1k
Fork
1.2k
Merge medio
1g 4h
PR unite (30g)
20

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rustfmt

Tutte le issue di rust-lang/rustfmt

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.