clang-tools  6.0.0
UniqueptrDeleteReleaseCheck.cpp
Go to the documentation of this file.
1 //===--- UniqueptrDeleteReleaseCheck.cpp - clang-tidy----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace readability {
20 
21 void UniqueptrDeleteReleaseCheck::registerMatchers(MatchFinder *Finder) {
22  auto IsSusbstituted = qualType(anyOf(
23  substTemplateTypeParmType(), hasDescendant(substTemplateTypeParmType())));
24 
25  auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
26  hasName("std::unique_ptr"),
27  hasTemplateArgument(1, refersToType(qualType(hasDeclaration(cxxRecordDecl(
28  hasName("std::default_delete")))))));
29 
30  Finder->addMatcher(
31  cxxDeleteExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
32  on(expr(hasType(UniquePtrWithDefaultDelete),
33  unless(hasType(IsSusbstituted)))
34  .bind("uptr")),
35  callee(cxxMethodDecl(hasName("release")))))))
36  .bind("delete"),
37  this);
38 }
39 
40 void UniqueptrDeleteReleaseCheck::check(
41  const MatchFinder::MatchResult &Result) {
42  const auto *PtrExpr = Result.Nodes.getNodeAs<Expr>("uptr");
43  const auto *DeleteExpr = Result.Nodes.getNodeAs<Expr>("delete");
44 
45  if (PtrExpr->getLocStart().isMacroID())
46  return;
47 
48  // Ignore dependent types.
49  // It can give us false positives, so we go with false negatives instead to
50  // be safe.
51  if (PtrExpr->getType()->isDependentType())
52  return;
53 
54  SourceLocation AfterPtr = Lexer::getLocForEndOfToken(
55  PtrExpr->getLocEnd(), 0, *Result.SourceManager, getLangOpts());
56 
57  diag(DeleteExpr->getLocStart(),
58  "prefer '= nullptr' to 'delete x.release()' to reset unique_ptr<> "
59  "objects")
60  << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
61  DeleteExpr->getLocStart(), PtrExpr->getLocStart()))
62  << FixItHint::CreateReplacement(
63  CharSourceRange::getTokenRange(AfterPtr, DeleteExpr->getLocEnd()),
64  " = nullptr");
65 }
66 
67 } // namespace readability
68 } // namespace tidy
69 } // namespace clang