llvm / llvm/llvm-project

-Wdeclaration-after-statement dislikes _Static_assert

Open
#161,014 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c
int foo(void)
{
_Static_assert(2+2 == 4, "");
int a = 2;
_Static_assert(2+2 == 4, "");
a += 2;
_Static_assert(2+2 == 4, "");
return a;
}
```
-xc -Wdeclaration-after-statement

https://godbolt.org/z/Ee4aPbcd9

Result: 1x `warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]`.

Expected: Since a succeeding _Static_assert does nothing, it's neither declaration nor code; therefore, omit the warning. Especially considering _Static_assert itself is incompatible with standards before C99.

Found it at winehq, they're using that warning to enforce their coding style. https://gitlab.winehq.org/wine/wine/-/merge_requests/9061#note_116902

e: Corresponding GCC bug is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93287 , which was closed as 'technically it is declaration after code'. I feel that technical correctness is a rather weak argument compared to keeping static asserts usable in practice, but if you agree with them, then feel free to consider this a badly-worded-diagnostic bug.

Contributor guide

Open the contributing guide

Research direction

Reproduce the diagnostic with the provided C example and `-xc -Wdeclaration-after-statement`, then compare the result with the Godbolt link. Trace how Clang classifies `_Static_assert` for this warning and add or update a regression test so the behavior matches the resolved interpretation of the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.