Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OR pattern in match arm makes rustfmt forget comment alignment #6060

Open
tnull opened this issue Feb 7, 2024 · 1 comment · May be fixed by #6086
Open

OR pattern in match arm makes rustfmt forget comment alignment #6060

tnull opened this issue Feb 7, 2024 · 1 comment · May be fixed by #6086
Labels
a-comments a-matches match arms, patterns, blocks, etc

Comments

@tnull
Copy link

tnull commented Feb 7, 2024

When reformatting the following example:

let error_code = match err {
	msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
	msgs::DecodeError::UnknownRequiredFeature|
	msgs::DecodeError::InvalidValue|
	msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
	_ => 0x2000 | 2, // Should never happen
};

we receive this result:

let error_code = match err {
	msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
	msgs::DecodeError::UnknownRequiredFeature
	| msgs::DecodeError::InvalidValue
	| msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
	_ => 0x2000 | 2,                                 // Should never happen
};

Note that the comment at the end of the second match arm is unaligned with the others. For the third match arm rustfmt even introduces many whitespaces to ensure it's aligned with the in the first match arm. However, it seems that rustfmt entirely forgets about this alignment in the second match arm, presumably due to it being an OR pattern

This seems to happen independently from any particular config option or rustfmt version, but currently I'm using

> rustfmt --version
rustfmt 1.7.0-stable (82e1608d 2023-12-21)
@ytmimi
Copy link
Contributor

ytmimi commented Feb 7, 2024

Thanks for the report. Yeah, there's something about the OR pattern that's throwing off the alignment. Without the OR pattern things align as you'd expect.

Input:

fn main() {
    let error_code = match err {
        msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
        msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
	_ => 0x2000 | 2, // Should never happen
    };
}

Output:

fn main() {
    let error_code = match err {
        msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
        msgs::DecodeError::ShortRead => 0x4000 | 22,     // invalid_onion_payload
        _ => 0x2000 | 2,                                 // Should never happen
    };
}

@MarcusGrass MarcusGrass linked a pull request Feb 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments a-matches match arms, patterns, blocks, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants