llvm / llvm/llvm-project

Add option to readability-function-size to not count lambdas as nesting level

Open
#169,961 1 comment 0 reactions 0 assignees View on GitHub
clang-tidy enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I like to be pretty strict with nesting, only allowing for 3 levels - function, loop, condition. I am however running into issues when I need to pass a loop+condition into some other function as a lambda:

```cpp
// NOLINTNEXTLINE(readability-function-size): Nesting counts lambda as level
void explorer_impl::filter_entries(storage& stack) {
// Nesting level 1
auto& entries = stack.back().entries;
const auto predicate = [&stack](const auto& entry) {
// Nesting level 2
for (auto it = stack.crbegin(); it != stack.crend(); ++it) {
// Nesting level 3
const auto decision = filter_entry(it->filter, entry);
if (decision != glob::decision::undecided) {
// Nesting level 4
return decision == glob::decision::ignored;
}
}
return false;
};

entries.erase(
std::remove_if(entries.begin(), entries.end(), predicate),
entries.end()
);
}
```

While there are indeed 4 different nested scopes, I would like an option to treat the lambda as a separate function, since that is what it is in my mental model as it has its own `return` scope. In some cases I would just make it an actual function to reduce indentation, but here I need the capture functionality. If I rewrote it as a pre-lambda functor, then `predicate` would be its own 3-level function and the check would pass.

Contributor guide

Open the contributing guide

Research direction

Start with the readability-function-size check and trace how it counts nested scopes. Compare the requested lambda boundary with existing nesting rules, then add coverage for the shown C++ pattern and verify that the option preserves current behavior when disabled.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.