Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 1 | //===- RedundantStringInitCheck.cpp - clang-tidy ----------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "RedundantStringInitCheck.h" |
Etienne Bergeron | e15ef2f | 2016-05-17 19:36:09 +0000 | [diff] [blame] | 10 | #include "../utils/Matchers.h" |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 11 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
Etienne Bergeron | e15ef2f | 2016-05-17 19:36:09 +0000 | [diff] [blame] | 14 | using namespace clang::tidy::matchers; |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace readability { |
| 19 | |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 20 | void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) { |
| 21 | if (!getLangOpts().CPlusPlus) |
| 22 | return; |
| 23 | |
Etienne Bergeron | 1329b98 | 2016-03-22 17:39:36 +0000 | [diff] [blame] | 24 | // Match string constructor. |
| 25 | const auto StringConstructorExpr = expr(anyOf( |
| 26 | cxxConstructExpr(argumentCountIs(1), |
| 27 | hasDeclaration(cxxMethodDecl(hasName("basic_string")))), |
| 28 | // If present, the second argument is the alloc object which must not |
| 29 | // be present explicitly. |
| 30 | cxxConstructExpr(argumentCountIs(2), |
| 31 | hasDeclaration(cxxMethodDecl(hasName("basic_string"))), |
| 32 | hasArgument(1, cxxDefaultArgExpr())))); |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 33 | |
Etienne Bergeron | 1329b98 | 2016-03-22 17:39:36 +0000 | [diff] [blame] | 34 | // Match a string constructor expression with an empty string literal. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 35 | const auto EmptyStringCtorExpr = cxxConstructExpr( |
| 36 | StringConstructorExpr, |
| 37 | hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0))))); |
Etienne Bergeron | 1329b98 | 2016-03-22 17:39:36 +0000 | [diff] [blame] | 38 | |
| 39 | const auto EmptyStringCtorExprWithTemporaries = |
Tim Shen | 325c727 | 2016-06-21 20:11:20 +0000 | [diff] [blame] | 40 | cxxConstructExpr(StringConstructorExpr, |
| 41 | hasArgument(0, ignoringImplicit(EmptyStringCtorExpr))); |
Etienne Bergeron | 1329b98 | 2016-03-22 17:39:36 +0000 | [diff] [blame] | 42 | |
| 43 | // Match a variable declaration with an empty string literal as initializer. |
| 44 | // Examples: |
| 45 | // string foo = ""; |
| 46 | // string bar(""); |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 47 | Finder->addMatcher( |
Tim Shen | 325c727 | 2016-06-21 20:11:20 +0000 | [diff] [blame] | 48 | namedDecl( |
Manuel Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 49 | varDecl(hasType(hasUnqualifiedDesugaredType(recordType( |
| 50 | hasDeclaration(cxxRecordDecl(hasName("basic_string")))))), |
Tim Shen | 325c727 | 2016-06-21 20:11:20 +0000 | [diff] [blame] | 51 | hasInitializer(expr(ignoringImplicit(anyOf( |
| 52 | EmptyStringCtorExpr, |
| 53 | EmptyStringCtorExprWithTemporaries))) |
| 54 | .bind("expr"))), |
| 55 | unless(parmVarDecl())) |
Alexander Kornienko | 1612fa0 | 2016-02-25 23:57:23 +0000 | [diff] [blame] | 56 | .bind("decl"), |
| 57 | this); |
| 58 | } |
| 59 | |
| 60 | void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) { |
| 61 | const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>("expr"); |
| 62 | const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl"); |
| 63 | diag(CtorExpr->getExprLoc(), "redundant string initialization") |
| 64 | << FixItHint::CreateReplacement(CtorExpr->getSourceRange(), |
| 65 | Decl->getName()); |
| 66 | } |
| 67 | |
| 68 | } // namespace readability |
| 69 | } // namespace tidy |
| 70 | } // namespace clang |