llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang doesn't report unused variables initialized in if statements

Open wolfpld opened this issue 1 year ago • 1 comments

Consider the following code:

int main( int argc, char** argv ) {
    if( auto unused = argc > 1 ) {
        int alsoUnused = 0;
    }
}

Both unused and alsoUnused variables are initialized, but not used. GCC is able to report this correctly:

% g++ -Wall test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:3:21: warning: unused variable ‘alsoUnused’ [-Wunused-variable]
    3 |                 int alsoUnused = 0;
      |                     ^~~~~~~~~~
test.cpp:2:18: warning: unused variable ‘unused’ [-Wunused-variable]
    2 |         if( auto unused = argc > 1 ) {
      |                  ^~~~~~

With Clang (15.0.7) you only get warning for the alsoUnused variable. The variable initialized in the if statement is not seen as unused.

% clang++ -Wall test.cpp
test.cpp:3:7: warning: unused variable 'alsoUnused' [-Wunused-variable]
                int alsoUnused = 0;
                    ^
1 warning generated.

Since unused is not used anywhere, it would be nice if Clang could report that.

wolfpld avatar Mar 24 '23 14:03 wolfpld