summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp9
-rw-r--r--clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp9
2 files changed, 15 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp b/clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
index 0c5eacef2e5..303833d73ec 100644
--- a/clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp
+++ b/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 @@ void DeleteNullPointerCheck::check(const MatchFinder::MatchResult &Result) {
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()));
diff --git a/clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp b/clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
index b46e52a754b..5a7ccae4d52 100644
--- a/clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
+++ b/clang-tools-extra/test/clang-tidy/readability-delete-null-pointer.cpp
@@ -3,6 +3,15 @@
#define NULL 0
void f() {
+ int *ps = 0;
+ if (ps /**/) // #0
+ delete ps;
+ // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+
+ // CHECK-FIXES: int *ps = 0;
+ // CHECK-FIXES-NEXT: {{^ }}// #0
+ // CHECK-FIXES-NEXT: delete ps;
+
int *p = 0;
// #1
OpenPOWER on IntegriCloud