llvm / llvm/llvm-project

[clang-tidy] misc-include-cleaner reports <new> unused for placement new

Open
#213,817 2 comments 0 reactions 0 assignees View on GitHub
clang-include-cleaner
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`misc-include-cleaner` reports `` as unused in a file whose only use of it is a
placement-new expression. Following the advice makes the file fail to compile, and clang's
own diagnostic names the very header the check wants removed.

The reference is to `operator new(std::size_t, void*)`, which is declared in ``. It
appears to be missed because a placement-new expression is an operator call rather than a
named symbol reference, so nothing in the file points at ``. The missing-includes half
is consistent with that reading: with `` deleted it does not ask for a provider either.

### Version

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

Installed from PyPI (`clang-tidy==22.1.8`). Reproduced on macOS with libc++.

### Reproducer

`new_repro.cc`:

```c++
#include
#include

struct T { int x; };

int f(void *storage) {
T *t = new (storage) T{1};
return t->x;
}
```

`.clang-tidy`:

```yaml
Checks: '-*,misc-include-cleaner'
CheckOptions:
- key: misc-include-cleaner.MissingIncludes
value: 'false'
```

A compilation database is required for the check to run; `--` on the command line is not
enough (with `--` the check silently reports nothing at all, including for a deliberately
unused header).

```
$ clang-tidy -p . --quiet new_repro.cc
new_repro.cc:2:1: warning: included header new is not used directly [misc-include-cleaner]
```

### Expected

No diagnostic. `` is used: it provides the `operator new(std::size_t, void*)` overload
that the placement-new expression resolves to.

### Why this one bites

Acting on the suggestion breaks the build, and clang says so explicitly:

```
$ clang++ -std=c++20 -fsyntax-only new_repro_without_new_include.cc
error: no matching 'operator new' function for non-allocating placement new expression;
include
6 | T *t = new (storage) T{1};
| ^ ~~~~~~~~~
```

So the front end already knows the requirement and names the header; the information does
not reach the check.

### Possibly related

#64191 is the same shape for a different operator — `` was reported unused because
`operator|` in range-adaptor composition was not tracked.

Contributor guide

Open the contributing guide

Research direction

Start with new_repro.cc, its .clang-tidy configuration, and the clang-tidy command shown in the report; confirm the warning with a compilation database. Trace misc-include-cleaner's handling of placement-new and operator calls, using the related ranges operator issue (#64191) for comparison. Done means the check no longer reports as unused while the existing compilation behavior remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.