You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was refactoring and commented out a chunk of code inside an attributed block. Wherein rustfmt proceeded to format the code, by repeating the attribute(s) (the first without the #) and the starting brace within the block.
I've tried a few variations, and the issue seems to occur, if a block has at least one attribute, while the block itself only contains comments. (Adding just a single line of code anywhere in the block, before or after the comment(s) stops the issue.)
The issue occurs both on stable and nightly.
Line Comments
Before rustfmt:
fnmain(){#[cfg(feature = "foo")]{// let foo = 0;}}
After rustfmt (both stable and nightly):
fnmain(){#[cfg(feature = "foo")]{[cfg(feature = "foo")]{// let foo = 0;}}
Block Comments
Before rustfmt:
fnmain(){#[cfg(feature = "foo")]{/* let foo = 0 */}}
After rustfmt (both stable and nightly):
fnmain(){#[cfg(feature = "foo")]{[cfg(feature = "foo")]{/* let foo = 0 */}}
Exception
I found one exception. If the block only contains a single block comment, and it is specifically written in a single line. Then rustfmt still formats it, but not in a way that breaks the code.
Before rustfmt:
fnmain(){#[cfg(feature = "foo")]{/* let foo = 0; */}}
After rustfmt (both stable and nightly):
fnmain(){#[cfg(feature = "foo")]{/* let foo = 0; */}}
Multiple Attributes
Before rustfmt:
fnmain(){#[foo]#[bar]#[baz]{// let foo = 0;}}
After rustfmt (both stable and nightly):
fn main(){#[foo]#[bar]#[baz]{[foo]#[bar]#[baz]{// let foo = 0;}}
vallentin
changed the title
Rustfmt breaks empty attributed block containing a comment
Rustfmt breaks attributed blocks only containing comments
Nov 11, 2020
I was refactoring and commented out a chunk of code inside an attributed block. Wherein rustfmt proceeded to format the code, by repeating the attribute(s) (the first without the
#
) and the starting brace within the block.I've tried a few variations, and the issue seems to occur, if a block has at least one attribute, while the block itself only contains comments. (Adding just a single line of code anywhere in the block, before or after the comment(s) stops the issue.)
The issue occurs both on stable and nightly.
Line Comments
Before rustfmt:
After rustfmt (both stable and nightly):
Block Comments
Before rustfmt:
After rustfmt (both stable and nightly):
Exception
I found one exception. If the block only contains a single block comment, and it is specifically written in a single line. Then rustfmt still formats it, but not in a way that breaks the code.
Before rustfmt:
After rustfmt (both stable and nightly):
Multiple Attributes
Before rustfmt:
After rustfmt (both stable and nightly):
rustfmt 1.4.20-stable (48f6c32 2020-08-09)
rustfmt 1.4.24-nightly (eb894d5 2020-11-05)
The text was updated successfully, but these errors were encountered: