llvm / llvm/llvm-project

[clang-tidy] Support C++26 placeholder bindings

Open
#190,387 4 comments 0 reactions 1 assignee Claimed by @OmarAzizi View on GitHub
c++26 check-request clang-tidy
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

C++26 placeholder bindings allow an intentionally ignored binding to be expressed directly at the binding site. This is useful for structured bindings that currently introduce a name only to suppress its unused warning immediately afterward.

For example, code like this:

```cpp
const auto [x, y] = compute();
(void)y;
use(x);
```

can be written more directly as:

```cpp
const auto [x, _] = compute();
use(x);
```

This seems like a good fit for a `modernize` check. The pattern is local, the intent is explicit, and the rewrite makes the code simpler and easier to read.

A reasonable initial scope would be limited to structured bindings where one binding is never used except in a suppression idiom such as `(void)name;`. In such cases, the check could replace that binding with `_` and remove the suppression statement.

Another example:

```cpp
if (const auto [it, inserted] = set.insert("Test"); inserted) {
(void)it;
std::cout << "Value was inserted successfully.\n";
}
```

could become:

```cpp
if (const auto [_, inserted] = set.insert("Test"); inserted) {
std::cout << "Value was inserted successfully.\n";
}
```

There is a related `std::tie` case, for example:

```cpp
bool inserted = false;
std::tie(std::ignore, inserted) = set.insert("Test");
if (inserted)
std::cout << "Value was inserted successfully.\n";
```

but that part should likely remain in `modernize-use-structured-binding`, since that check already handles similar tuple-like unpacking patterns. After such code is rewritten to a structured binding, a placeholder-specific check could then replace the ignored binding with `_`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.