10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
20 const auto DeleteExpr =
22 has(declRefExpr(to(decl(equalsBoundNode(
"deletedPointer"))))))
25 const auto DeleteMemberExpr =
26 cxxDeleteExpr(has(memberExpr(hasDeclaration(
27 fieldDecl(equalsBoundNode(
"deletedMemberPointer"))))))
28 .bind(
"deleteMemberExpr");
30 const auto PointerExpr = anyOf(
31 declRefExpr(to(decl().bind(
"deletedPointer"))),
32 memberExpr(hasDeclaration(fieldDecl().bind(
"deletedMemberPointer"))));
34 const auto BinaryPointerCheckCondition = binaryOperator(hasOperands(
35 anyOf(cxxNullPtrLiteralExpr(), integerLiteral(equals(0))), PointerExpr));
38 ifStmt(hasCondition(anyOf(PointerExpr, BinaryPointerCheckCondition)),
40 DeleteExpr, DeleteMemberExpr,
41 compoundStmt(anyOf(has(DeleteExpr), has(DeleteMemberExpr)),
44 .bind(
"ifWithDelete"),
49 const auto *IfWithDelete = Result.Nodes.getNodeAs<IfStmt>(
"ifWithDelete");
50 const auto *Compound = Result.Nodes.getNodeAs<CompoundStmt>(
"compound");
53 IfWithDelete->getBeginLoc(),
54 "'if' statement is unnecessary; deleting null pointer has no effect");
55 if (IfWithDelete->getElse())
59 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
60 IfWithDelete->getBeginLoc(),
62 *Result.SourceManager,
63 Result.Context->getLangOpts())
67 Diag << FixItHint::CreateRemoval(
68 CharSourceRange::getTokenRange(Compound->getLBracLoc()));
69 Diag << FixItHint::CreateRemoval(
70 CharSourceRange::getTokenRange(Compound->getRBracLoc()));
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.