Warning: unnecessary parentheses around `if` condition

When I run below code, it shows warning: unnecessary parentheses around if condition:

#[macro_export]
macro_rules! set_bit {
    (&mut $num: expr, $n: tt, $val: tt) => {{
        println!("here");
        if $val {
            $num |= 1 << $n;
        } else {
            $num &= !(1 << $n);
        }
    }};

    (&mut $num: expr, $n: tt, ($val: tt)) => {{
        if $val {
            $num |= 1 << $n;
        } else {
            $num &= !(1 << $n);
        }
    }};
}

#[test]
fn test_set_bit() {
    let mut bit = 2;
    let disable = true;
    set_bit!(&mut bit, 1, (!disable));
    println!("{}", bit);
}

But if I remove the parentheses around the disable, it shows no rules expected the token disable, so how to fix this?

This would fit better on https://users.rust-lang.org/

Okay, Thanks!

I think that a way to make this subject on topic here is:

Why are style lints applied in macro expansion? This is kind of surprising. Can't this be disabled wholesale?

Like, allow lints that appear inside macro expansion, if they concern to just style / asthetics

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.