[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 09:54:50 PST 2023


Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/76249 at github.com>


================
@@ -0,0 +1,31 @@
+//===--- AvoidReturnWithVoidValueCheck.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 "AvoidReturnWithVoidValueCheck.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 AvoidReturnWithVoidValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      returnStmt(hasReturnValue(hasType(voidType()))).bind("void_return"),
+      this);
+}
+
+void AvoidReturnWithVoidValueCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>("void_return");
+  diag(VoidReturn->getBeginLoc(),
+       "return statement in void function should not return a value");
----------------
PiotrZSL wrote:

consider "return statement within a void function should not have a specified return value"

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


More information about the cfe-commits mailing list