async-stream icon indicating copy to clipboard operation
async-stream copied to clipboard

Rustfmt cannot format stream! blocks

Open vkgnosis opened this issue 3 years ago • 2 comments
trafficstars

Take a simple example like

fn main() {
    let s = async_stream::stream! {
        for i in 0..3 {
            yield i;
        }
    };
}

in a new rust project. Add some spaces to break the indentation somewhere in the macro invocation. Run cargo fmt. See that Rustfmt completely ignores the code inside of the macro.

This already happens in a simple block like

    let s = async_stream::stream! {
         let a = 0u32;
    };

I'm not sure if this is a bug in Rustfmt or something this crate can fix or if there are some Rustfmt options that can be changed to fix this. My current workaround is to remove the async_stream::stream! part, run Rustfmt, add it back.

vkgnosis avatar Mar 11 '22 11:03 vkgnosis

rustfmt does not format macros that use braces.

A known workaround is to combine parentheses and blocks.

-     let s = async_stream::stream! {
+     let s = async_stream::stream!({
           let a = 0u32;
-     };
+     });

(Note that the formatting will not work anyway if macro-specific syntax such as for await is used.)

taiki-e avatar Mar 11 '22 11:03 taiki-e

This is an issue with tokio::select! as well.

Noah-Kennedy avatar Mar 11 '22 16:03 Noah-Kennedy