[clang-tidy] Add `modernize-use-elements-view` check for `std::views::keys` / `std::views::values`
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
A modernization check for loops that only use one component of an associative container element would be useful in C++20 and later. In many codebases, loops still iterate key-value pairs even when only the key or only the value is actually needed.
Typical examples are:
```cpp
for (const auto &[key, value] : m) {
use(key);
}
```
or:
```cpp
for (auto &&entry : m) {
use(entry.second);
}
```
when the loop body is really interested in just one projection of the range.
In C++20, the intent can often be expressed more directly as:
```cpp
for (const auto &key : m | std::views::keys) {
use(key);
}
```
or:
```cpp
for (auto &&value : m | std::views::values) {
use(value);
}
```
This makes the loop read in terms of what it actually consumes, rather than exposing the pair structure when that structure is incidental.
Contributor guide
Research direction
Start by surveying existing clang-tidy modernize checks and their tests, then trace how checks recognize range-based loops and apply C++20 transformations. Define the supported key-only and value-only cases, including structured bindings and entry access, and add coverage showing that qualifying loops become std::views::keys or std::views::values while other loops remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100