[clang] [clang] Add regression test for unreachable-code warning on attributed statements (PR #246768)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 13:59:47 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Shraavasti Bhat (shraavb)
<details>
<summary>Changes</summary>
Adds a regression test for #<!-- -->39721.
A statement attribute such as `[[gsl::suppress]]` on a `return`, or on an `if` that never returns, used to produce a bogus `-Wunreachable-code` warning on the attribute itself. This was fixed in Clang 14 (bisected to c74ab84ea23f497ac83501473220cd9cfefe81e8 in the issue), but no test covers it.
The test checks that:
- no warning is emitted on the attribute in either case
- the warning for the genuinely unreachable `return 2;` is still emitted
Test run (`build/bin/llvm-lit clang/test/SemaCXX/unreachable-code.cpp`):
```
PASS: Clang :: SemaCXX/unreachable-code.cpp (1 of 1)
Passed: 1 (100.00%)
```
To check the test can fail, I ran the file's RUN line with `-verify` on two modified copies (`a.cpp`, `b.cpp`; the checked-in test is unchanged).
`a.cpp`: `expected-warning` removed from `return 2;`:
```
error: 'expected-warning' diagnostics seen but not expected:
File a.cpp Line 139: 'return' will never be executed
1 error generated.
```
`b.cpp`: `expected-warning` added on the attribute line:
```
error: 'expected-warning' diagnostics expected but not seen:
File b.cpp Line 136: will never be executed
1 error generated.
```
AI use: Claude Code was used to explain the issue, and to help verify the test, fix whitespace and run git commands. I wrote the test.
Closes #<!-- -->39721
---
Full diff: https://github.com/llvm/llvm-project/pull/246768.diff
1 Files Affected:
- (modified) clang/test/SemaCXX/unreachable-code.cpp (+16)
``````````diff
diff --git a/clang/test/SemaCXX/unreachable-code.cpp b/clang/test/SemaCXX/unreachable-code.cpp
index 410ed9ae8861bd..edd87e130de52d 100644
--- a/clang/test/SemaCXX/unreachable-code.cpp
+++ b/clang/test/SemaCXX/unreachable-code.cpp
@@ -130,3 +130,19 @@ namespace gh57123 {
}
}
}
+
+namespace gh39721 {
+ int f1() {
+ [[gsl::suppress("foobar")]]
+ return 1;
+
+ return 2; // expected-warning {{will never be executed}}
+ }
+
+ void f2() {
+ [[gsl::suppress("foo")]]
+ if (true) {
+ throw 1;
+ }
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/246768
More information about the cfe-commits
mailing list