[clang-tools-extra] [clang-tidy] Ensure nullable variable is not accessed without validity test (PR #90173)

Danny Mösch via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 23:47:50 PDT 2024


https://github.com/SimplyDanny created https://github.com/llvm/llvm-project/pull/90173

None

>From f8e0509e0586ae7cb1a236e27b8be27a94bf7cf5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Fri, 26 Apr 2024 08:38:40 +0200
Subject: [PATCH] [clang-tidy] Ensure nullable variable is not accessed without
 validity test

---
 .../readability/AvoidReturnWithVoidValueCheck.cpp          | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
index 48bca41f4a3b1e..f077040a35295e 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
@@ -64,8 +64,11 @@ void AvoidReturnWithVoidValueCheck::check(
            << BraceInsertionHints.closingBraceFixIt();
   }
   Diag << FixItHint::CreateRemoval(VoidReturn->getReturnLoc());
-  if (!Result.Nodes.getNodeAs<FunctionDecl>("function_parent") ||
-      SurroundingBlock->body_back() != VoidReturn)
+  const auto *FunctionParent =
+      Result.Nodes.getNodeAs<FunctionDecl>("function_parent");
+  if (!FunctionParent ||
+      (SurroundingBlock && SurroundingBlock->body_back() != VoidReturn))
+    // If this is not the last statement in a function body, we add a `return`.
     Diag << FixItHint::CreateInsertion(SemicolonPos.getLocWithOffset(1),
                                        " return;", true);
 }



More information about the cfe-commits mailing list