rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[BUG] Rustfmt Produces Erroneous Code When Trying To Format Lazy_static Macro Call

Open TxMat opened this issue 1 year ago • 2 comments

Hi,

I found this bug with the lazy_static macro but it probably extends to all other macro calls with parenthesis.

here is the code before the format :

lazy_static!(
    static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
    static ref CASCADE_IP: String = std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
    static ref CASCADE_PORT: String = std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
);

and after

lazy_static! {
    static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
    static ref CASCADE_IP: String = std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
    static ref CASCADE_PORT: String = std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
}; // <-- this guy

The trailing semicolon is not removed which cause the code to fail to compile.

TxMat avatar Jan 09 '24 08:01 TxMat

Thanks for the report. You're best option is to write the lazy_static! macro with curly braces to begin with to avoid this issue.

lazy_static! {
    static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
    static ref CASCADE_IP: String = std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
    static ref CASCADE_PORT: String = std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
}

ytmimi avatar Jan 09 '24 16:01 ytmimi

@rustbot claim

rscprof avatar Jan 16 '24 19:01 rscprof