0

tl;dr how do I instruct rustfmt (or cargo fmt) to not add braces around pub static ref declarations? I want fewer rustc warnings.

I ran cargo fmt against my code base. The code base uses lazy_static to declare global variables and values.

Before:

fn some_usize() -> usize {
    99
}
lazy_static! {
    pub static ref MY_GLOBAL_USIZE: usize = some_usize();
}

After cargo fmt:

fn some_usize() -> usize {
    99
}
lazy_static! {
    pub static ref MY_GLOBAL_USIZE: usize = { some_usize() };
}

Building results in a warning:

warning: unnecessary braces around block return value
  --> main.rs:10:49
   |
34 |     pub static ref MY_GLOBAL_USIZE: usize = { some_usize() };
   |                                             ^^            ^^
   |
   = note: `#[warn(unused_braces)]` on by default
help: remove these braces
   |
34 -     pub static ref MY_GLOBAL_USIZE: usize = { some_usize() };
34 +     pub static ref MY_GLOBAL_USIZE: usize = some_usize();

How do I instruct rustfmt (i.e. cargo fmt) to not add these unnecessary braces? Looking through Configuring Rustfmt, there does not appear to be such an option.

Of course, I could follow the hint within the rustc warning and declare #![allow(unused_braces)]. I would prefer not to do that.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
  • 2
    I can't seem to reproduce this with latest rustfmt 1.5.1-stable. May be `rustup update` should get you latest and fix this? – bits Sep 16 '22 at 20:00

0 Answers0