diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp | 75 |
1 files changed, 31 insertions, 44 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp index 5b0e4ca265e..8f086b87858 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @@ -77,17 +77,14 @@ void RedundantStringCStrCheck::registerMatchers( return; // Match expressions of type 'string' or 'string*'. - const auto StringDecl = - cxxRecordDecl(hasName("::std::basic_string")); + const auto StringDecl = cxxRecordDecl(hasName("::std::basic_string")); const auto StringExpr = - expr(anyOf(hasType(StringDecl), - hasType(qualType(pointsTo(StringDecl))))); + expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl))))); // Match string constructor. const auto StringConstructorExpr = expr(anyOf( - cxxConstructExpr( - argumentCountIs(1), - hasDeclaration(cxxMethodDecl(hasName("basic_string")))), + cxxConstructExpr(argumentCountIs(1), + hasDeclaration(cxxMethodDecl(hasName("basic_string")))), cxxConstructExpr( argumentCountIs(2), hasDeclaration(cxxMethodDecl(hasName("basic_string"))), @@ -103,21 +100,18 @@ void RedundantStringCStrCheck::registerMatchers( .bind("call"); // Detect redundant 'c_str()' calls through a string constructor. - Finder->addMatcher( - cxxConstructExpr(StringConstructorExpr, - hasArgument(0, StringCStrCallExpr)), - this); + Finder->addMatcher(cxxConstructExpr(StringConstructorExpr, + hasArgument(0, StringCStrCallExpr)), + this); // Detect: 's == str.c_str()' -> 's == str' Finder->addMatcher( cxxOperatorCallExpr( - anyOf(hasOverloadedOperatorName("<"), - hasOverloadedOperatorName(">"), - hasOverloadedOperatorName(">="), - hasOverloadedOperatorName("<="), - hasOverloadedOperatorName("!="), - hasOverloadedOperatorName("=="), - hasOverloadedOperatorName("+")), + anyOf( + hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"), + hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="), + hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="), + hasOverloadedOperatorName("+")), anyOf(allOf(hasArgument(0, StringExpr), hasArgument(1, StringCStrCallExpr)), allOf(hasArgument(0, StringCStrCallExpr), @@ -126,47 +120,41 @@ void RedundantStringCStrCheck::registerMatchers( // Detect: 'dst += str.c_str()' -> 'dst += str' // Detect: 's = str.c_str()' -> 's = str' - Finder->addMatcher( - cxxOperatorCallExpr( - anyOf(hasOverloadedOperatorName("="), - hasOverloadedOperatorName("+=")), - hasArgument(0, StringExpr), - hasArgument(1, StringCStrCallExpr)), - this); + Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="), + hasOverloadedOperatorName("+=")), + hasArgument(0, StringExpr), + hasArgument(1, StringCStrCallExpr)), + this); // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)' Finder->addMatcher( - cxxMemberCallExpr(on(StringExpr), - callee(decl(cxxMethodDecl( - hasAnyName("append", "assign", "compare")))), - argumentCountIs(1), - hasArgument(0, StringCStrCallExpr)), + cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName( + "append", "assign", "compare")))), + argumentCountIs(1), hasArgument(0, StringCStrCallExpr)), this); // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)' Finder->addMatcher( cxxMemberCallExpr(on(StringExpr), - callee(decl(cxxMethodDecl(hasName("compare")))), - argumentCountIs(3), - hasArgument(2, StringCStrCallExpr)), + callee(decl(cxxMethodDecl(hasName("compare")))), + argumentCountIs(3), hasArgument(2, StringCStrCallExpr)), this); // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)' Finder->addMatcher( cxxMemberCallExpr(on(StringExpr), - callee(decl(cxxMethodDecl( - hasAnyName("find", "find_first_not_of", "find_first_of", - "find_last_not_of", "find_last_of", "rfind")))), - anyOf(argumentCountIs(1), argumentCountIs(2)), - hasArgument(0, StringCStrCallExpr)), + callee(decl(cxxMethodDecl(hasAnyName( + "find", "find_first_not_of", "find_first_of", + "find_last_not_of", "find_last_of", "rfind")))), + anyOf(argumentCountIs(1), argumentCountIs(2)), + hasArgument(0, StringCStrCallExpr)), this); // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)' Finder->addMatcher( cxxMemberCallExpr(on(StringExpr), - callee(decl(cxxMethodDecl(hasName("insert")))), - argumentCountIs(2), - hasArgument(1, StringCStrCallExpr)), + callee(decl(cxxMethodDecl(hasName("insert")))), + argumentCountIs(2), hasArgument(1, StringCStrCallExpr)), this); // Detect redundant 'c_str()' calls through a StringRef constructor. @@ -176,9 +164,8 @@ void RedundantStringCStrCheck::registerMatchers( // wrt. string types and they internally make a StringRef // referring to the argument. Passing a string directly to // them is preferred to passing a char pointer. - hasDeclaration( - cxxMethodDecl(hasAnyName("::llvm::StringRef::StringRef", - "::llvm::Twine::Twine"))), + hasDeclaration(cxxMethodDecl(hasAnyName( + "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))), argumentCountIs(1), // The only argument must have the form x.c_str() or p->c_str() // where the method is string::c_str(). StringRef also has |