================
@@ -0,0 +1,68 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RedundantLambdaParenthesesCheck.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(LambdaExpr, hasRedundantParens) {
+  return (Node.hasExplicitParameters() || Node.isGenericLambda()) &&
+         Node.getCallOperator()->getNumParams() == 0 &&
+         (!Node.isGenericLambda() ||
+          Finder->getASTContext().getLangOpts().CPlusPlus20);
+}
+
+} // namespace
+
+void RedundantLambdaParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(lambdaExpr(hasRedundantParens()).bind("lambda"), this);
+}
+
+void RedundantLambdaParenthesesCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
+
+  if (Lambda->getBeginLoc().isMacroID())
+    return;
+
+  const LangOptions &LangOpts = getLangOpts();
+
+  const auto FTL = Lambda->getCallOperator()->getFunctionTypeLoc();
+  const SourceLocation LParenLoc = FTL.getLParenLoc();
+  const SourceLocation RParenLoc = FTL.getRParenLoc();
+
+  if (LParenLoc.isInvalid() || RParenLoc.isInvalid())
+    return;
+
+  if (!LangOpts.CPlusPlus23) {
+    std::optional<Token> RParen =
+        Lexer::findNextToken(LParenLoc, *Result.SourceManager, LangOpts);
+    if (!RParen || RParen->isNot(tok::r_paren))
+      return;
+    std::optional<Token> NextTok = Lexer::findNextToken(
+        RParen->getLocation(), *Result.SourceManager, LangOpts);
+    if (NextTok && NextTok->is(tok::raw_identifier)) {
+      StringRef Id = NextTok->getRawIdentifier();
+      if (Id == "constexpr" || Id == "consteval" || Id == "mutable" ||
+          Id == "noexcept")
+        return;
+    }
+    if (NextTok && NextTok->is(tok::arrow))
+      return;
----------------
OmarAzizi wrote:

@localspook I addressed all three points, but for the attribute test case, I 
tried several attributes (`[[deprecated]]`, `[[nodiscard]]`, 
`[[clang::noinline]]`) after () on a lambda, but they all result in compile 
errors, so I couldn't write a test case for that path. Happy to add one if you 
have a specific attribute in mind.

> I know this is taking quite a few rounds of review, but thank you for putting 
> up with me and responding quickly ^_^

Also, not at all i'm learning new things about LLVM, and it's fun

https://github.com/llvm/llvm-project/pull/190438
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to