Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress a warning in clang++?

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?

like image 479
Erel Segal-Halevi Avatar asked Apr 07 '19 14:04

Erel Segal-Halevi


People also ask

How do I turn off clang warning?

Using _Pragma to suppress the warning. Using #pragma to suppress the warning. Using command line options to suppress the warning.

How do I supress a warning in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .

How do I ignore a warning in Makefile?

Maybe you can look for CFLAGS options in Makefile and remove the -Werror flag. The Werror flag will make all warnings into errors. Show activity on this post.


1 Answers

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.

like image 200
Mike Kinghan Avatar answered Oct 12 '22 21:10

Mike Kinghan