https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/180337
>From 79654f0d9f0d8814b81fd5c8780dd87bd1add6d8 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sat, 7 Feb 2026 12:30:30 +0300 Subject: [PATCH 1/2] [clang-tidy] Fix bugprone-string-constructor FN with allocators --- .../bugprone/StringConstructorCheck.cpp | 13 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../checkers/bugprone/string-constructor.cpp | 51 +++++++++++++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index d2e631e539b78..4ec705a6016ab 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -81,7 +81,10 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger()))), + anyOf(argumentCountIs(2), + allOf(argumentCountIs(3), + hasArgument(2, unless(hasType(qualType(isInteger())))))), + hasArgument(0, hasType(qualType(isInteger()))), hasArgument(1, hasType(qualType(isInteger()))), anyOf( // Detect the expression: string('x', 40); @@ -101,7 +104,10 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))), - argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), + anyOf(argumentCountIs(2), + allOf(argumentCountIs(3), + hasArgument(2, unless(hasType(qualType(isInteger())))))), + hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -123,7 +129,8 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))), - argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + anyOf(argumentCountIs(3), argumentCountIs(4)), + hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(qualType(isInteger()))), hasArgument(2, hasType(qualType(isInteger()))), anyOf( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 0ad69f5fdc5aa..c8edf0f08dc77 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -148,6 +148,11 @@ Changes in existing checks <clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro definition in the warning message if the macro is defined on command line. +- Improved :doc:`bugprone-string-constructor + <clang-tidy/checks/bugprone/string-constructor>` check to detect suspicious + string constructor calls when the string class constructor has a default + allocator argument. + - Improved :doc:`bugprone-unsafe-functions <clang-tidy/checks/bugprone/unsafe-functions>` check by adding the function ``std::get_temporary_buffer`` to the default list of unsafe functions. (This diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp index 2576d19916250..af844b04aa438 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp @@ -8,10 +8,10 @@ class char_traits {}; template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C> > struct basic_string { basic_string(); - basic_string(const C*, unsigned int size); + basic_string(const C*, unsigned int size, const A &a = A()); basic_string(const C *, const A &allocator = A()); - basic_string(unsigned int size, C c); - basic_string(const C*, unsigned int pos, unsigned int size); + basic_string(unsigned int size, C c, const A &a = A()); + basic_string(const C*, unsigned int pos, unsigned int size, const A &a = A()); }; typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; @@ -119,6 +119,44 @@ std::string_view StringViewFromZero() { // CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour } +void TestExplicitAllocator() { + std::allocator<char> a; + + std::string s1('x', 5, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: string constructor parameters are probably swapped; expecting string(count, character) [bugprone-string-constructor] + // CHECK-FIXES: std::string s1(5, 'x', a); + std::string s2(0, 'x', a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string + std::string s3(-4, 'x', a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter + std::string s4(0x1000000, 'x', a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter + + std::string q0("test", 0, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string + std::string q1(kText, -4, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter + std::string q2("test", 200, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size + std::string q3(kText3, 0x1000000, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter + + std::string r1("test", 1, 0, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string + std::string r2("test", 0, -4, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter + std::string r3("test", -4, 1, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as position of the first character parameter + std::string r4("test", 0, 0x1000000, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter + std::string r5("test", 0, 5, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size + std::string r6("test", 3, 2, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size + std::string r7("test", 4, 1, a); + // CHECK-MESSAGES: [[@LINE-1]]:15: warning: position of the first character parameter is bigger than string literal character range +} + void Valid() { std::string empty(); std::string str(4, 'x'); @@ -132,6 +170,13 @@ void Valid() { std::string s8("test", 3, 1); std::string s9("te" "st", 1, 2); + std::allocator<char> a; + std::string sa1(4, 'x', a); + std::string sa2("test", 4, a); + std::string sa3("test", 3, a); + std::string sa4("test", 0, 4, a); + std::string sa5("test", 3, 1, a); + std::string_view emptyv(); std::string_view sv1("test", 4); std::string_view sv2("test", 3); >From 6c67dd86bdf0242a6cf6ea4af095077644dde644 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sun, 8 Feb 2026 11:09:48 +0300 Subject: [PATCH 2/2] fix 3 integer ctor --- .../clang-tidy/bugprone/StringConstructorCheck.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 4ec705a6016ab..d6bc4caf23879 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -81,9 +81,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - anyOf(argumentCountIs(2), - allOf(argumentCountIs(3), - hasArgument(2, unless(hasType(qualType(isInteger())))))), + anyOf(argumentCountIs(2), argumentCountIs(3)), hasArgument(0, hasType(qualType(isInteger()))), hasArgument(1, hasType(qualType(isInteger()))), anyOf( _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
