clang-tools  10.0.0
UniqueptrDeleteReleaseCheck.cpp
Go to the documentation of this file.
1 //===--- UniqueptrDeleteReleaseCheck.cpp - clang-tidy----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19 
20 void UniqueptrDeleteReleaseCheck::registerMatchers(MatchFinder *Finder) {
21  auto IsSusbstituted = qualType(anyOf(
22  substTemplateTypeParmType(), hasDescendant(substTemplateTypeParmType())));
23 
24  auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
25  hasName("std::unique_ptr"),
26  hasTemplateArgument(1, refersToType(qualType(hasDeclaration(cxxRecordDecl(
27  hasName("std::default_delete")))))));
28 
29  Finder->addMatcher(
30  cxxDeleteExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
31  on(expr(hasType(UniquePtrWithDefaultDelete),
32  unless(hasType(IsSusbstituted)))
33  .bind("uptr")),
34  callee(cxxMethodDecl(hasName("release")))))))
35  .bind("delete"),
36  this);
37 }
38 
39 void UniqueptrDeleteReleaseCheck::check(
40  const MatchFinder::MatchResult &Result) {
41  const auto *PtrExpr = Result.Nodes.getNodeAs<Expr>("uptr");
42  const auto *DeleteExpr = Result.Nodes.getNodeAs<Expr>("delete");
43 
44  if (PtrExpr->getBeginLoc().isMacroID())
45  return;
46 
47  // Ignore dependent types.
48  // It can give us false positives, so we go with false negatives instead to
49  // be safe.
50  if (PtrExpr->getType()->isDependentType())
51  return;
52 
53  SourceLocation AfterPtr = Lexer::getLocForEndOfToken(
54  PtrExpr->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
55 
56  diag(DeleteExpr->getBeginLoc(),
57  "prefer '= nullptr' to 'delete x.release()' to reset unique_ptr<> "
58  "objects")
59  << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
60  DeleteExpr->getBeginLoc(), PtrExpr->getBeginLoc()))
61  << FixItHint::CreateReplacement(
62  CharSourceRange::getTokenRange(AfterPtr, DeleteExpr->getEndLoc()),
63  " = nullptr");
64 }
65 
66 } // namespace readability
67 } // namespace tidy
68 } // namespace clang
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//