Skip to content

file-lines can modify lines above the selected range #6863

@randomPoison

Description

@randomPoison

When using --file-lines to only partially format a function body, an unselected line immediately preceding the selected range can also be touched.

Summary

Consider the following example file test.rs where lines 2-4 in the function body are indented incorrectly:

fn main() {
println!("a");
println!("b");
println!("c");
}

If I format with --file-lines '[{"file":"test.rs","range":[2,2]}]' then I get the correct output, with only the code on line 2 changed:

fn main() {
    println!("a");
println!("b");
println!("c");
}

But if I attempt to format only line 4 with --file-lines '[{"file":"test.rs","range":[4,4]}]' then line 3 gets incorrectly formatted along with line 4:

fn main() {
println!("a");
    println!("b");
    println!("c");
}

Configuration

rustfmt cli options used:

rustfmt --unstable-features --file-lines '[{"file":"test.rs","range":[4,4]}]' test.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICES-has-mcveStatus: a Minimal Complete and Verifiable Example has been found for this issueUO-file_linesUnstable option: file_lines

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions