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

Đang mở
#7,257 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
7.1k
Fork
1.2k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
20

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rustfmt

Tất cả issue của rust-lang/rustfmt

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.