[clang-tidy] Add check to remove redundant empty lambda parameter clauses
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-parensreadability-omit-empty-lambda-parameter-list
@llvm/issue-subscribers-clang-tidy
Author: Daniil Dudkin (unterumarmung)
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-parensreadability-omit-empty-lambda-parameter-list
Should it be "good first issue"?
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
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?
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
readability-redundant-lambda-parameters?
[](){} becomes []{} :)
or readability-redundant-lambda-parameter-list
#174053 requests same feature for clang-format.
IMO we should remove "lambda" in the name of the check to support requires expressions in future.