[clang-tools-extra] f325b5b - [clang-tidy] Support std::string_view in readability-redundant-string-cstr
Tamas Berghammer via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 2 03:24:05 PST 2023
Author: Tamas Berghammer
Date: 2023-01-02T06:24:00-05:00
New Revision: f325b5b8cb1d6c16dd04c88380caea7b0a7750eb
URL: https://github.com/llvm/llvm-project/commit/f325b5b8cb1d6c16dd04c88380caea7b0a7750eb
DIFF: https://github.com/llvm/llvm-project/commit/f325b5b8cb1d6c16dd04c88380caea7b0a7750eb.diff
LOG: [clang-tidy] Support std::string_view in readability-redundant-string-cstr
Previously we were matching constructor calls for std::string and
llvm::StringRef and this change extends this set with including
std::string_view as well.
Reviewed By: njames93, carlosgalvezp
Differential Revision: https://reviews.llvm.org/D140018
Added:
Modified:
clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
index 79211731ff7be..9ecd6d612e0c8 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
@@ -86,6 +86,11 @@ void RedundantStringCStrCheck::registerMatchers(
// be present explicitly.
hasArgument(1, cxxDefaultArgExpr()))));
+ // Match string constructor.
+ const auto StringViewConstructorExpr = cxxConstructExpr(
+ argumentCountIs(1),
+ hasDeclaration(cxxMethodDecl(hasName("basic_string_view"))));
+
// Match a call to the string 'c_str()' method.
const auto StringCStrCallExpr =
cxxMemberCallExpr(on(StringExpr.bind("arg")),
@@ -101,7 +106,8 @@ void RedundantStringCStrCheck::registerMatchers(
traverse(
TK_AsIs,
cxxConstructExpr(
- StringConstructorExpr, hasArgument(0, StringCStrCallExpr),
+ anyOf(StringConstructorExpr, StringViewConstructorExpr),
+ hasArgument(0, StringCStrCallExpr),
unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
HasRValueTempParent)))))),
this);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2135dd1e01be0..580776b76fd14 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -204,6 +204,10 @@ Changes in existing checks
The check now skips concept definitions since redundant expressions still make sense
inside them.
+- Support removing ``c_str`` calls from ``std::string_view`` constructor calls in
+ :doc: `readability-redundant-string-cstr <clang-tidy/checks/readability/redundant-string-cstr>`
+ check.
+
Removed checks
^^^^^^^^^^^^^^
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 e1df8cccc10b3..ed50ad16f2423 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
@@ -48,6 +48,15 @@ typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;
typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring;
typedef basic_string<char16, std::char_traits<char16>, std::allocator<char16>> u16string;
typedef basic_string<char32, std::char_traits<char32>, std::allocator<char32>> u32string;
+
+template <typename C, typename T>
+struct basic_string_view {
+ basic_string_view(const C* s);
+};
+typedef basic_string_view<char, std::char_traits<char>> string_view;
+typedef basic_string_view<wchar_t, std::char_traits<wchar_t>> wstring_view;
+typedef basic_string_view<char16, std::char_traits<char16>> u16string_view;
+typedef basic_string_view<char32, std::char_traits<char32>> u32string_view;
}
std::string operator+(const std::string&, const std::string&);
@@ -169,6 +178,15 @@ void f6(const std::string &s) {
tmp.insert(1, s);
tmp.insert(1, s.c_str(), 2);
}
+void f7(std::string_view sv) {
+ std::string s;
+ f7(s.c_str());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}f7(s);{{$}}
+ f7(s.data());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}f7(s);{{$}}
+}
// Tests for std::wstring.
@@ -177,6 +195,15 @@ void g1(const std::wstring &s) {
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES: {{^ }}g1(s);{{$}}
}
+void g2(std::wstring_view sv) {
+ std::wstring s;
+ g2(s.c_str());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}g2(s);{{$}}
+ g2(s.data());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}g2(s);{{$}}
+}
// Tests for std::u16string.
@@ -185,6 +212,15 @@ void h1(const std::u16string &s) {
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES: {{^ }}h1(s);{{$}}
}
+void h2(std::u16string_view sv) {
+ std::u16string s;
+ h2(s.c_str());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}h2(s);{{$}}
+ h2(s.data());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}h2(s);{{$}}
+}
// Tests for std::u32string.
@@ -193,6 +229,15 @@ void k1(const std::u32string &s) {
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES: {{^ }}k1(s);{{$}}
}
+void k2(std::u32string_view sv) {
+ std::u32string s;
+ k2(s.c_str());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}k2(s);{{$}}
+ k2(s.data());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
+ // CHECK-FIXES: {{^ }}k2(s);{{$}}
+}
// Tests on similar classes that aren't good candidates for this checker.
More information about the cfe-commits
mailing list