From b99630e432614d06b380afb15c466665065eaa0a Mon Sep 17 00:00:00 2001 From: Nathan James Date: Wed, 29 Jul 2020 15:35:28 +0100 Subject: [PATCH] [clang-tidy] Fix RedundantStringCStrCheck with r values The previous fix for this, https://reviews.llvm.org/D76761, Passed test cases but failed in the real world as std::string has a non trivial destructor so creates a CXXBindTemporaryExpr. This handles that shortfall and updates the test case std::basic_string implementation to use a non trivial destructor to reflect real world behaviour. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D84831 --- .../readability/RedundantStringCStrCheck.cpp | 14 ++++++++------ .../checkers/readability-redundant-string-cstr.cpp | 2 ++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp index bea02a6ba111b..1f371eed2db80 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @@ -92,16 +92,18 @@ void RedundantStringCStrCheck::registerMatchers( callee(memberExpr().bind("member")), callee(cxxMethodDecl(hasAnyName("c_str", "data")))) .bind("call"); - + const auto HasRValueTempParent = + hasParent(materializeTemporaryExpr(unless(isBoundToLValue()))); // Detect redundant 'c_str()' calls through a string constructor. // If CxxConstructExpr is the part of some CallExpr we need to // check that matched ParamDecl of the ancestor CallExpr is not rvalue. Finder->addMatcher( - traverse(ast_type_traits::TK_AsIs, - cxxConstructExpr(StringConstructorExpr, - hasArgument(0, StringCStrCallExpr), - unless(hasParent(materializeTemporaryExpr( - unless(isBoundToLValue())))))), + traverse( + ast_type_traits::TK_AsIs, + cxxConstructExpr( + StringConstructorExpr, hasArgument(0, StringCStrCallExpr), + unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr( + HasRValueTempParent)))))), this); // Detect: 's == str.c_str()' -> 's == str' diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp index 2561b81805bda..e1df8cccc10b3 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp @@ -15,6 +15,8 @@ struct basic_string { basic_string(); basic_string(const C *p, const A &a = A()); + ~basic_string(); + const C *c_str() const; const C *data() const;