Issue 140118
Summary `-Wunused-const-variable` warning is not emitted under `-Werror` if another earlier warning causes compilation to abort
Labels new issue
Assignees
Reporter vsz
    # Summary
This is the case for other warnings as well, such as: `-Wunused-template`, `-Wunneeded-member-function`, `-Wunused-local-typedef`, `-Wunused-const-variable`, `-Wunused-function`, `-Wunused-member-function`, `-Wundefined-internal`, `-Wunused-variable`, `-Wundefined-func-template` and possibly more.

When compiling with Clang using -Werror, the -Wunused-const-variable warning is not emitted if another warning (e.g., -Wunused-variable) occurs earlier in the translation unit and is promoted to an error. This results in inconsistent warning behavior depending on whether -Werror is enabled.

# Steps to Reproduce
### test.cpp
```cpp
const int x = 42;  // Line 1

void Fcn() {
  int local = 24;  // Line 4
}
```

## Case A – Expected Behavior:
```
clang++ test.cpp -std=c++17 -Wunused-variable -Wunused-const-variable
```
Output:
```
test.cpp:4:7: warning: unused variable 'local' [-Wunused-variable]
test.cpp:1:11: warning: unused variable 'x' [-Wunused-const-variable]
```
## Case B – -Werror suppresses second warning:
```
clang++ test.cpp -std=c++17 -Wunused-variable -Wunused-const-variable -Werror
```
Output:
```
test.cpp:4:7: error: unused variable 'local' [-Wunused-variable]
```

## Case C – Only const variable exists:
### test.cpp
```cpp
const int x = 42;  // Line 1
```
```
clang++ test.cpp -std=c++17 -Wunused-const-variable -Werror
```
Output:
```
test.cpp:1:11: error: unused variable 'x' [-Wunused-const-variable]
```
## Expected Behavior

All enabled warnings should be emitted consistently, regardless of whether `-Werror` is present. 

## Root cause

Clang defers some diagnostics until the end of the translation unit (`Sema::ActionEndOfTranslationUnit()`). It does not process `unused` warnings if there was an error as is outlined [here](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/Sema.cpp#L1483).



_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to