llvm / llvm/llvm-project

clang-tidy: Getting "misc-static-assert" reports for every assert

Open
#213,560 1 comment 4 reactions 1 assignee Claimed by @zeyi2 View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

# Summary

clang-tidy is generating this warning for **every** assert, even ones that obviously cannot be evaluated at compile time.

# Steps to reproduce

`repro.cpp`:

```c++
#include

void f(int* p, int n)
{
assert(p != nullptr);
assert(n > 0);
assert(p);
}
```

Run:

```console
$ clang-tidy --checks='-*,misc-static-assert' repro.cpp -- -std=c++20
```

## Observed Output

```
3 warnings generated.
repro.cpp:5:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
5 | assert(p != nullptr);
| ^~~~~~
| static_assert
repro.cpp:6:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
6 | assert(n > 0);
| ^~~~~~
| static_assert
repro.cpp:7:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
7 | assert(p);
| ^~~~~~
| static_assert
```

## Expected Output

No warnings

# NOTE

On this system, `assert(n>0)` preprocesses to:

```console
$ clang++ -E -P -x c++ -std=c++20 - <<'EOF' | tail -1
#include
void f(int n){ assert(n>0); }
EOF
void f(int n){ ((n>0) ? void (1 ? 1 : bool (n>0)) : __assert_fail ("n>0", __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); }
```

There are two conditional operators in the expansion. The outer one's condition is the asserted
expression `n > 0`. The inner one, `1 ? 1 : bool(n>0)`, has the literal `1`
as its condition, which is constant and I assume what the warning references.

I suspect this commit is the problem:
[`ea37298b65bd67f94c3c2640e91ec5865a5019ad`](https://sourceware.org/git/?p=glibc.git;a=commit;h=ea37298b65bd67f94c3c2640e91ec5865a5019ad)
— *"assert: Support assert as variadic macro for C++26 [PR27276]"*;

# System information

```console
$ clang-tidy --version
LLVM (http://llvm.org/):
LLVM version 22.1.8
Optimized build.

$ clangd --version
clangd version 22.1.8
Features: linux
Platform: x86_64-pc-linux-gnu
```

| | |
| --- | --- |
| glibc | Arch package `glibc 2.44+r5+g7cba77790f32-1` |
| OS | Arch Linux |

I have not tested other clang-tidy versions, so I cannot say when this started or whether trunk
behaves differently.

I would not expect this to reproduce on a system with a glibc predating the commit above, or with
a standard library whose `assert` does not use the nested-ternary form, but I have not verified
that on such a system.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.