llvm / llvm/llvm-project

[clang-tidy] New check: bugprone-redundant-adjacent-find-check

Open
#222,405 1 comment 0 reactions 0 assignees View on GitHub
check-request clang-tidy
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Here is suggestion to add check to detects redundant end iterator checks after `std::adjacent_find`. When `std::adjacent_find` returns an iterator `it` that is not equal to `end()`, it is guaranteed that `std::next(it) != end()` is also true. A subsequent check for `std::next(it) != end()` is therefore redundant and should be removed.

**Non-compliant example:**
```cpp
auto it = std::adjacent_find(v.begin(), v.end(), pred);
if (it != v.end()) {
auto next_it = std::next(it);
if (next_it != v.end()) { // ALWAYS true - completely redundant
process(it, next_it);
}
}
```

**Compliant example:**
```cpp
auto it = std::adjacent_find(v.begin(), v.end(), pred);
if (it != v.end()) {
auto next_it = std::next(it);
process(it, next_it);
}
```

Contributor guide

Open the contributing guide

Research direction

Start at the clang-tidy entry point and compare existing bugprone checks to understand how a new check is structured. Use the adjacent_find examples in this issue to define the redundant next-iterator condition; done means the check diagnoses that condition and leaves the compliant example unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.