How to suppress a warning in clang++?
Asked Answered
C

2

13

I compiled the following c++ program:

 int main() {  2==3;  }

with:

clang++-5.0 -std=c++17 -Wunused-comparison prog.cpp

and got the warning:

warning: equality comparison result unused [-Wunused-comparison]
2==3;
~^~~

... so, probably this is not the correct way to suppress a warning in CLANG.

In the clang manual, this part is a "TODO".

What is the correct command-line flag to disable a warning?

Cursorial answered 7/4, 2019 at 14:7 Comment(0)
A
13

In the clang diagnostic that you get from:

$ cat main.cpp
int main()
{
    2==3;
    return 0;
}

$ clang++ -c main.cpp
main.cpp:3:6: warning: equality comparison result unused [-Wunused-comparison]
    2==3;
    ~^~~
1 warning generated.

the bracketed:

-Wunused-comparison

tells you that -Wunused-comparison is the enabled warning (in this case enabled by default) that was responsible for the diagnostic. So to suppress the diagnostic you explicitly disable that warning with the matching -Wno-... flag:

$ clang++ -c -Wno-unused-comparison main.cpp; echo Done
Done

The same applies for GCC.

In general, it is reckless to suppress warnings. One should rather enable them generously - -Wall -Wextra [-pedantic] - and then fix offending code.

Angelikaangelina answered 8/4, 2019 at 18:49 Comment(0)
A
2

add no warning flag as : -Wno-xxx :

https://releases.llvm.org/12.0.0/tools/clang/docs/DiagnosticsReference.html#diagnostic-flags

e.g. enable the c++17 extension warning:

-Wc++17-extensions

suppress the c++17 extension warning:

-Wno-c++17-extensions

Amarillis answered 15/11, 2022 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.