[clang-tools-extra] [clang-tidy] Add check readability-return-expression-in-void-function (PR #76249)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 08:42:45 PST 2023


================
@@ -0,0 +1,31 @@
+//===--- ReturnExpressionInVoidFunctionCheck.cpp - clang-tidy -------------===//
+//
+// 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 "ReturnExpressionInVoidFunctionCheck.h"
+#include "clang/AST/Stmt.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void ReturnExpressionInVoidFunctionCheck::registerMatchers(
+    MatchFinder *Finder) {
+  Finder->addMatcher(
+      returnStmt(hasReturnValue(hasType(voidType()))).bind("void_return"),
+      this);
+}
+
+void ReturnExpressionInVoidFunctionCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>("void_return");
+  diag(VoidReturn->getBeginLoc(), "return statements should not return void");
----------------
PiotrZSL wrote:

message could be better, to point out that that it's about returning void expression not, void type,
maybe better that return statement in void function shouldn't have subexpression/value

https://github.com/llvm/llvm-project/pull/76249


More information about the cfe-commits mailing list