llvm / llvm/llvm-project

False positive on -Wmismatched-new-delete if variable is reused

Open
#195,648 8 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics confirmed false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c++
// Type your code here, or load an example.
int square(int num) {
int* g = new int;
delete g;
g = new int[4];
delete[] g;
return num * num;
}
```
https://godbolt.org/z/jcWePexce
->
```
:6:5: warning: 'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'? [-Wmismatched-new-delete]
6 | delete[] g;
| ^ ~~
:3:14: note: allocated with 'new' here
3 | int* g = new int;
| ^
```
No it's not, it's a different pointer that happens to reuse the variable.

The code is admittedly dumb, but feels worth asking about, at least.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example at the linked Godbolt entry point with -Wmismatched-new-delete and confirm the diagnostic follows the reused variable rather than the second allocation. Then trace the compiler diagnostic and its existing regression tests; done means this example no longer produces a false-positive warning while genuine mismatched deletes remain diagnosed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.