llvm / llvm/llvm-project

`readability-non-const-parameter`: false positive for placement-new storage

Open
#206,613 2 comments 0 reactions 0 assignees View on GitHub
clang-tidy confirmed false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

For a pointer parameter used only as storage for a placement-new expression,
`readability-non-const-parameter` suggests making it pointer-to-const. The
pointee *is* modified (placement-new constructs an object into it), so the
parameter must stay non-const. Applying the fixit produces code that no longer
compiles — and `clang-tidy --fix` applies it automatically.

```cpp
#include

struct S { int x; };

void construct(char *buffer) {
new (buffer) S();
}
```

Produces:
```
$ clang-tidy --checks='-*,readability-non-const-parameter' repro.cpp -- -std=c++17
warning: pointer parameter 'buffer' can be pointer to const
[readability-non-const-parameter]
6 | void construct(char *buffer) {
| ^
| const
```

and applying auto-fixup breaks the source. Somehow `void*` parameters are not affected.

Tested with clang-tidy-22 did not try newer versions or trunk

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the diagnostic from the provided repro.cpp with clang-tidy --checks='-*,readability-non-const-parameter' and -std=c++17. Trace the readability-non-const-parameter check and its fix-it handling for placement-new expressions, then add a regression test showing that construct(char *buffer) is not reported or rewritten while the existing behavior remains covered.

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
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.