Search code examples
warningsclang++

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?


Solution

  • 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.