Issue |
146296
|
Summary |
[clang-tidy] Check request: misc-constexpr-non-static-in-scope
|
Labels |
clang-tidy
|
Assignees |
|
Reporter |
denzor200
|
Many developers mistakenly believe that `constexpr` variables inside functions **automatically have a static lifetime**, like `static` variables. This common misconception leads to dangerous code that:
- Compiles without errors, but causes undefined behavior (UB) at runtime.
- Hard to detect in code reviews and testing.
- Misleading because constexpr is associated with "compile-time safety".
Need a check that will find `constexpr` non-`static` variables inside a function scope. The check will provide fixit hint to add `static` keyword.
BEFORE:
```
void foo() {
constexpr int x = 42;
// ...
}
```
AFTER:
```
void foo() {
static constexpr int x = 42;
// ...
}
```
The check will not provide warning for variables outside a function scope:
```
namespace ns {
inline constexpr int MAX_SIZE = 1024; // OK - no need static here
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs