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

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 27 02:31:12 PDT 2024


Author: Danny Mösch
Date: 2024-04-27T11:31:08+02:00
New Revision: b2c9f7d3188e41163574a83a835437955cf4b80f

URL: https://github.com/llvm/llvm-project/commit/b2c9f7d3188e41163574a83a835437955cf4b80f
DIFF: https://github.com/llvm/llvm-project/commit/b2c9f7d3188e41163574a83a835437955cf4b80f.diff

LOG: [clang-tidy] Ensure nullable variable is not accessed without validity test (#90173)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp

Removed: 
    


################################################################################
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