rust-lang/rustfmt

reformatting creates new (incorrectly) formatted code

Closed this issue · 4 comments

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bc4a0958067516436e6477a4646314ff

Given

use std::io;

pub fn foo(version_and_command: u8) -> io::Result<()> {
    match version_and_command {
        0 => {}
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                format!(
                    "invalid proxy protocol command 0x{:02X}. expected local (0x20) or proxy (0x21)",
                    version_and_command
                ),
            ))
        }
    }
    
    Ok(())
}

Running rustfmt produces a correctly formatted

use std::io;

pub fn foo(version_and_command: u8) -> io::Result<()> {
    match version_and_command {
        0 => {}
        _ => return Err(io::Error::new(
            io::ErrorKind::Other,
            format!(
                "invalid proxy protocol command 0x{:02X}. expected local (0x20) or proxy (0x21)",
                version_and_command
            ),
        )),
    }
    
    Ok(())
}

But running it again produces

use std::io;

pub fn foo(version_and_command: u8) -> io::Result<()> {
    match version_and_command {
        0 => {}
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                format!(
                "invalid proxy protocol command 0x{:02X}. expected local (0x20) or proxy (0x21)",
                version_and_command
            ),
            ))
        }
    }
    
    Ok(())
}

@conradludgate what version of rustfmt are you using?

Thanks, If you've got access to a nightly version of rustfmt you can configure version=Two or style_edition=2024.

This is how the code is reformatted when using the updated option:

use std::io;

pub fn foo(version_and_command: u8) -> io::Result<()> {
    match version_and_command {
        0 => {}
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                format!(
                    "invalid proxy protocol command 0x{:02X}. expected local (0x20) or proxy (0x21)",
                    version_and_command
                ),
            ));
        }
    }

    Ok(())
}

@conradludgate what version of rustfmt are you using?

This was from both stable and latest nightly toolchains. I'm away from my desk right now so I can't find the exact rustfmt version