llvm / llvm/llvm-project

False positive `-Wunneeded-internal-declaration` for function only called in discarded statement

Open
#175,440 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

https://godbolt.org/z/5j7EYsYTh
```cpp
static void f() {}

int main() {
if constexpr (false) { f(); }
}
```
```
:1:13: warning: function 'f' is not needed and will not be emitted [-Wunneeded-internal-declaration]
1 | static void f() {}
| ^
1 warning generated.
```
This feels like a false positive because normally, uses in discarded statements are still counted for the purpose of diagnostics. For example, I recall that older versions of Clang warned `-Wunused-parameter` historically, but no longer do for the following code:
```cpp
int g(int x) {
if constexpr (false) {
return x;
} else {
return 0;
}
}
```

The code is artificial; I ran into this issue in practice with:
```cpp
if constexpr (is_debug_build)
```
(because preprocessing is lame, and I didn't want `#if`)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the linked Godbolt example with Clang and inspect the handling of -Wunneeded-internal-declaration for a function referenced only inside a discarded if constexpr statement. Compare it with the discarded-statement behavior described for -Wunused-parameter; done means the artificial example no longer reports a false-positive warning while genuinely unused functions remain diagnosed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.