1

Coding with Eclipse, I like my code to be as clean as possible. I basically activated every warning possible on my gcc and the -Werr flag ensures me I cannot overlook them.

Although I did that, I do not have any compilation problem (since my code is valid), but still I got this annoying warning from Eclipse:

Suggested parenthesis around expression 'flags & CONSTANT'

The expression being considered is

if(flags & CONSTANT || bufferUsed == 0) ...

Following Operators precedence, both Bitwise AND and Equality have higher precedence than Logical OR.

Is Eclipse just dumb, issuing warnings based on some to-be-proven-cases-where-code-looking-like-mine-went-wrong or is that warning issued on a solid basis?

Bernard Rosset
  • 4,523
  • 6
  • 27
  • 29
  • 1
    It looks like it is just a warning making sure you mean & rather than && as explained [here](http://stackoverflow.com/a/5476774/2600883) – Oilyraincloud Sep 02 '14 at 22:52
  • 6
    Well, putting the parentheses doesn't really make your code harder to read, in fact it makes it easier, and you ensure that no one will have trouble with remember operator precedence. The warning may seem dumb to you, but it's probably wise to use the parentheses. – Filipe Gonçalves Sep 02 '14 at 23:09
  • @Oilyraincloud What is explained on your link is the mixup between assignment and comparison operators, because the first has lower precedence making the whole expression right-to-left associative, thus effectively going against the intuitive left to right way of reading. It effectively addresses a common way of producing mistakes. Even If I intended to use &&, its precedence is still lower than ||, thus the warning must have some other reason. – Bernard Rosset Sep 03 '14 at 08:07
  • 1
    @FilipeGonçalves I understand issuing warnings when lower-precedence operator should intuitively take over the priority (see Oilyraincloud's example). Bitwise AND is rarely done with an expression as one of its tokens. Thus, I do not think anyone intuitively expects anything other than `CONSTANT` being the right token of the bitwise AND operator. I still fail to see the readability concern. Maybe between `||` and `==`, but it is not what was underlined by Eclipse. – Bernard Rosset Sep 03 '14 at 08:07
  • This warning is very annoying. Programmers should be aware of operator precedence. And I disagree with @Filipe Gonçalves it does make the code unreadable. What's next? A warning that 4*5+6 should be written as (4*5)+6? – Michael Lehn Aug 30 '20 at 10:14

1 Answers1

-1

I wish instead of such inept warnings, those overly enthusiastic compiler writers would finally come up with a message like

Suggested space between if and (

;-)

Armali
  • 18,255
  • 14
  • 57
  • 171