J-Camilleri created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Bug Fix for #35811


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D61861

Files:
  clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
  clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp


Index: clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
+++ clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
@@ -6,7 +6,7 @@
   int *p = 0;
 
   // #1
-  if (p) { // #2
+  if (p /**/) { // #2
     delete p;
   } // #3
   // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 'if' statement is unnecessary; 
deleting null pointer has no effect [readability-delete-null-pointer]
Index: clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
@@ -7,6 +7,7 @@
 
//===----------------------------------------------------------------------===//
 
 #include "DeleteNullPointerCheck.h"
+#include "../utils/LexerUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Lexer.h"
@@ -62,9 +63,11 @@
 
   Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
       IfWithDelete->getBeginLoc(),
-      Lexer::getLocForEndOfToken(IfWithDelete->getCond()->getEndLoc(), 0,
-                                 *Result.SourceManager,
-                                 Result.Context->getLangOpts())));
+      utils::lexer::getPreviousToken(IfWithDelete->getThen()->getBeginLoc(),
+                                     *Result.SourceManager,
+                                     Result.Context->getLangOpts())
+          .getLocation()));
+
   if (Compound) {
     Diag << FixItHint::CreateRemoval(
         CharSourceRange::getTokenRange(Compound->getLBracLoc()));


Index: clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
+++ clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
@@ -6,7 +6,7 @@
   int *p = 0;
 
   // #1
-  if (p) { // #2
+  if (p /**/) { // #2
     delete p;
   } // #3
   // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
Index: clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "DeleteNullPointerCheck.h"
+#include "../utils/LexerUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Lexer.h"
@@ -62,9 +63,11 @@
 
   Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
       IfWithDelete->getBeginLoc(),
-      Lexer::getLocForEndOfToken(IfWithDelete->getCond()->getEndLoc(), 0,
-                                 *Result.SourceManager,
-                                 Result.Context->getLangOpts())));
+      utils::lexer::getPreviousToken(IfWithDelete->getThen()->getBeginLoc(),
+                                     *Result.SourceManager,
+                                     Result.Context->getLangOpts())
+          .getLocation()));
+
   if (Compound) {
     Diag << FixItHint::CreateRemoval(
         CharSourceRange::getTokenRange(Compound->getLBracLoc()));
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to