diff options
| author | Malcolm Parsons <malcolm.parsons@gmail.com> | 2016-11-03 12:56:48 +0000 |
|---|---|---|
| committer | Malcolm Parsons <malcolm.parsons@gmail.com> | 2016-11-03 12:56:48 +0000 |
| commit | 8b70e2631c6ab4259a4aa9e797c99132fb66e79b (patch) | |
| tree | 2f6c75fc3c1c324c2d6f2def8d14a9c97e25279c /clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp | |
| parent | a705ab175d2b6b6ae0edb234b49a76fe6db38144 (diff) | |
| download | bcm5719-llvm-8b70e2631c6ab4259a4aa9e797c99132fb66e79b.tar.gz bcm5719-llvm-8b70e2631c6ab4259a4aa9e797c99132fb66e79b.zip | |
[clang-tidy] Handle data() in readability-redundant-string-cstr
Summary:
std::string::data() and std::string::c_str() are equivalent.
Enhance the readability-redundant-string-cstr check to also handle
calls to data().
Reviewers: etienneb, alexfh, aaron.ballman
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D26279
llvm-svn: 285901
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp index ecd173ad964..5b0e4ca265e 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @@ -99,7 +99,7 @@ void RedundantStringCStrCheck::registerMatchers( const auto StringCStrCallExpr = cxxMemberCallExpr(on(StringExpr.bind("arg")), callee(memberExpr().bind("member")), - callee(cxxMethodDecl(hasName("c_str")))) + callee(cxxMethodDecl(hasAnyName("c_str", "data")))) .bind("call"); // Detect redundant 'c_str()' calls through a string constructor. @@ -192,7 +192,8 @@ void RedundantStringCStrCheck::registerMatchers( void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) { const auto *Call = Result.Nodes.getStmtAs<CallExpr>("call"); const auto *Arg = Result.Nodes.getStmtAs<Expr>("arg"); - bool Arrow = Result.Nodes.getStmtAs<MemberExpr>("member")->isArrow(); + const auto *Member = Result.Nodes.getStmtAs<MemberExpr>("member"); + bool Arrow = Member->isArrow(); // Replace the "call" node with the "arg" node, prefixed with '*' // if the call was using '->' rather than '.'. std::string ArgText = @@ -200,7 +201,8 @@ void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) { if (ArgText.empty()) return; - diag(Call->getLocStart(), "redundant call to `c_str()`") + diag(Call->getLocStart(), "redundant call to %0") + << Member->getMemberDecl() << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText); } |

