Warnings: Lack of parenthesis in conditional if statement when mixing AND and OR?

I would still say that is not ideal. If you do that, then you'll never going to learn rules like operator precedence. And neither the coworker reading the code. I thought that learning is a good thing. "Let's just put parentheses and never check the truth, not even of curiosity. :slight_smile:" I admit this whole thing is a debate. So maybe there is no really right or wrong answer. But you may be completely right. For certain people, putting parentheses is the most appropriate choice.

I agree. Moreover -- superfluous parentheses are confusing. The reader will have to stop and think: why did they put parentheses here? Oh, no reason, I see.

I would actually like a warning for superfluous parentheses, not sure if there is a clippy lint for this.

2 Likes

Utterly perfect! You do realize that bit is ignored here (except if it's too big and overflow happens). Still want to insist there are no problem with these precendeces?

You'll never learn them except if you use them every day. It just so happen that I worked with bitmasks on my $DAYJOB for last few months thus illigiality of << precedence is still fresh in my mind. Othervise pretty knowledgeable @tczajka haven't done that recently thus he forgot. Even if I'm pretty sure he was bitten by it at some point in the past, but these things tend to be forgotten over time.

Unfortunately that's something people have very hard time agreeing about. You couldn't please everyone. And it's not clear how to trigger warning in your example while still trying to keen number of parenthesis to minimum.

The only clear case are parenthesis after if where they are never needed.

Other places… I'm not so sure about.

Yeah, there's the #[warn(unused_parens)] lint which is enabled by default.

Your example demonstrates that bit is not ignored, it correctly prints different answers for different bits.

Ugh. It's only ignored in C, but not in Rust. That one is on me, I guess, I still have to deal with C/C++ thus I never realized that Rust fixed that particular wart of C/C++.

That's nice, I guess, one less wart to keep in mind… when C/C++ would become history, means in half-century, I guess.

This only warns about superfluous parentheses around the whole expression in certain cases, but what I want is a warning for superfluous parentheses where they don't change precedence, for example:

let x = a + (b * c);
let y = (a && b) || (c && d);

As no one has mentioned; I expect you know; the operators are not mathematical. You can write far worse just exploiting them being lazy boolean operators than a failure to understand precedence.