llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-tidy] Add check to remove redundant empty lambda parameter clauses

Open unterumarmung opened this issue 8 months ago • 8 comments

It would be useful to have a clang-tidy check/fix-it that removes redundant empty () from lambdas when the rewrite is valid in the active language mode.

Examples:

  • Valid in all tested modes (c++11+): []() { return 42; } -> [] { return 42; } [x]() { return x; } -> [x] { return x; }

  • Valid in c++20+: []<class T>() { return sizeof(T); } -> []<class T> { return sizeof(T); } []<class T>() requires true { return sizeof(T); } -> []<class T> requires true { return sizeof(T); }

  • Valid only in c++23+: [x]() mutable { return x; } -> [x] mutable { return x; } []() noexcept { return 42; } -> [] noexcept { return 42; } []() -> int { return 42; } -> [] -> int { return 42; } []() constexpr { return 42; } -> [] constexpr { return 42; } []() consteval { return 42; } -> [] consteval { return 42; }

So the check would need to be language-mode-aware and avoid rewriting cases that are not valid until newer standards.

Possible names:

  • readability-redundant-lambda-parens
  • readability-omit-empty-lambda-parameter-list

unterumarmung avatar Apr 03 '26 20:04 unterumarmung

@llvm/issue-subscribers-clang-tidy

Author: Daniil Dudkin (unterumarmung)

It would be useful to have a clang-tidy check/fix-it that removes redundant empty `()` from lambdas when the rewrite is valid in the active language mode.

Examples:

  • Valid in all tested modes (c++11+): []() { return 42; } -> [] { return 42; } [x]() { return x; } -> [x] { return x; }

  • Valid in c++20+: []&lt;class T&gt;() { return sizeof(T); } -> []&lt;class T&gt; { return sizeof(T); } []&lt;class T&gt;() requires true { return sizeof(T); } -> []&lt;class T&gt; requires true { return sizeof(T); }

  • Valid only in c++23+: [x]() mutable { return x; } -> [x] mutable { return x; } []() noexcept { return 42; } -> [] noexcept { return 42; } []() -&gt; int { return 42; } -> [] -&gt; int { return 42; } []() constexpr { return 42; } -> [] constexpr { return 42; } []() consteval { return 42; } -> [] consteval { return 42; }

So the check would need to be language-mode-aware and avoid rewriting cases that are not valid until newer standards.

Possible names:

  • readability-redundant-lambda-parens
  • readability-omit-empty-lambda-parameter-list

llvmbot avatar Apr 03 '26 20:04 llvmbot

Should it be "good first issue"?

unterumarmung avatar Apr 03 '26 20:04 unterumarmung

Should it be "good first issue"?

New checks should almost never be a good first issue apart from the easiest one, like portability-avoid-pragma-once

vbvictor avatar Apr 03 '26 20:04 vbvictor

I'd like to work on this. Before I get going, is there a preference between readability-redundant-lambda-parens and readability-omit-empty-lambda-parameter-list?

OmarAzizi avatar Apr 03 '26 21:04 OmarAzizi

I think to follow the current naming style it'd be correct to call it readability-redundant-lambda-parentheses at first. If we find a better name later, we can rename it with https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/rename_check.py

unterumarmung avatar Apr 03 '26 21:04 unterumarmung

readability-redundant-lambda-parameters?

[](){} becomes []{} :)

LegalizeAdulthood avatar Apr 03 '26 22:04 LegalizeAdulthood

or readability-redundant-lambda-parameter-list

unterumarmung avatar Apr 03 '26 22:04 unterumarmung

#174053 requests same feature for clang-format.

IMO we should remove "lambda" in the name of the check to support requires expressions in future.

zwuis avatar Apr 09 '26 17:04 zwuis