Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Clang warn: `'&&' within '||'`?

Tags:

c

clang

My understanding is the parentheses make no difference, so is there any reason (other than to “improve“ code-clarity) that Clang warns this as a default? I prefer not to add the parentheses as I dislike adding code for code’s sake.

src/websocket.c:420:43: warning: '&&' within '||' [-Wlogical-op-parentheses]
        if (rv == 0 && N != 0 || rv == -1 && errno == ECONNRESET) {
                              ~~ ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
src/websocket.c:420:43: note: place parentheses around the '&&' expression to
      silence this warning
        if (rv == 0 && N != 0 || rv == -1 && errno == ECONNRESET) {
                                 ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
like image 298
mxcl Avatar asked Jun 05 '13 12:06

mxcl


People also ask

Does Clang define __ GNU C __?

(GNU C is a language, GCC is a compiler for that language.Clang defines __GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ according to the version of gcc that it claims full compatibility with.

Why does Clang use GCC?

Clang is designed to provide a frontend compiler that can replace GCC. Apple Inc. (including NeXT later) has been using GCC as the official compiler. GCC has always performed well as a standard compiler in the open source community.

Is Clang better than GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


Video Answer


2 Answers

The natural tendency is to read it left to right, and it's easy to forget the operator precedence. That said, it's just a warning, and if you know what you're doing and your own style allows it, feel free to suppress it.

like image 101
Wooble Avatar answered Oct 17 '22 10:10

Wooble


I'm guessing because it's simply a bit unclear, unless the reader is very good at C's operator precedence rules.

Your expression is like this:

if (A && B || C && D)

and since && has higher precendence than ||, it means

if ((A && B) || (C && D))

which I guess is what you mean, but it's not very clear when reading.

like image 30
unwind Avatar answered Oct 17 '22 12:10

unwind