clang-tidy: not suggest cmStrCat for short appends and concats

We rely, that standard library have really effective `+=` and `+` operators
for `std::string` with any paired arguments: `std::string`, `const char *` and `char`.
So, that change just blocks `c = a + b` and `c += a` replacements,
but still replaces the `c = a + b + d` and `c += a + b` and longer (with more `+` operators).
This commit is contained in:
AJIOB
2026-09-22 10:31:17 +03:00
parent 509ac6160a
commit 182473210f
4 changed files with 125 additions and 128 deletions
@@ -126,6 +126,10 @@ void StringConcatenationUseCmstrcatCheck::check(
void StringConcatenationUseCmstrcatCheck::issueCorrection(
ExprChain const& Chain, MatchFinder::MatchResult const& Result)
{
if (Chain.second.size() < 2) {
return;
}
std::vector<FixItHint> FixIts;
CXXOperatorCallExpr const* ExprNode;
std::vector<clang::CXXOperatorCallExpr const*>::const_iterator It =
@@ -14,20 +14,18 @@ std::string concat;
// Correction needed
void test1()
{
concat = cmStrCat(a, b);
concat = cmStrCat(a, " and this is a string literal");
concat = cmStrCat(a, 'O');
concat = cmStrCat("This is a string literal", b);
concat = cmStrCat('O', a);
concat = cmStrCat(a, " and this is a string literal", 'O', b);
concat = cmStrCat(concat, b);
concat = cmStrCat(concat, " and this is a string literal");
concat = cmStrCat(concat, 'o');
concat = cmStrCat(concat, a, a);
concat = cmStrCat(concat, " and this is a string literal", a);
concat = cmStrCat(concat, b, 'o', a);
concat = cmStrCat(concat, b, " and this is a string literal ", 'o', b);
if (true)
concat = cmStrCat(concat, a, b);
std::pair<std::string, std::string> p;
concat = cmStrCat(p.first, p.second);
concat = cmStrCat(p.first, p.second, a);
}
// No correction needed
@@ -37,4 +35,17 @@ void test2()
a = "This is a string literal";
a = 'X';
cmStrCat(a, b);
concat = a + b;
concat = a + " and this is a string literal";
concat = a + 'O';
concat = "This is a string literal" + b;
concat = 'O' + a;
concat += b;
std::pair<std::string, std::string> p;
concat = p.first + p.second;
if (true)
concat += a;
}
@@ -1,124 +1,95 @@
cmake-string-concatenation-use-cmstrcat.cxx:17:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
17 | concat = a + b;
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:17:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:17:14: note: FIX-IT applied suggested code changes
17 | concat = a + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:17:17: note: FIX-IT applied suggested code changes
17 | concat = a + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:18:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
18 | concat = a + " and this is a string literal";
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:18:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:18:14: note: FIX-IT applied suggested code changes
18 | concat = a + " and this is a string literal";
| ^
cmake-string-concatenation-use-cmstrcat.cxx:18:47: note: FIX-IT applied suggested code changes
18 | concat = a + " and this is a string literal";
| ^
cmake-string-concatenation-use-cmstrcat.cxx:19:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
19 | concat = a + 'O';
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:19:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:19:14: note: FIX-IT applied suggested code changes
19 | concat = a + 'O';
| ^
cmake-string-concatenation-use-cmstrcat.cxx:19:19: note: FIX-IT applied suggested code changes
19 | concat = a + 'O';
| ^
cmake-string-concatenation-use-cmstrcat.cxx:20:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
20 | concat = "This is a string literal" + b;
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:20:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:20:39: note: FIX-IT applied suggested code changes
20 | concat = "This is a string literal" + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:20:42: note: FIX-IT applied suggested code changes
20 | concat = "This is a string literal" + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:21:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
21 | concat = 'O' + a;
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:21:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:21:16: note: FIX-IT applied suggested code changes
21 | concat = 'O' + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:21:19: note: FIX-IT applied suggested code changes
21 | concat = 'O' + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:22:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
22 | concat = a + " and this is a string literal" + 'O' + b;
17 | concat = a + " and this is a string literal" + 'O' + b;
| ^ ~ ~ ~
| cmStrCat( , , , )
cmake-string-concatenation-use-cmstrcat.cxx:22:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:22:14: note: FIX-IT applied suggested code changes
22 | concat = a + " and this is a string literal" + 'O' + b;
cmake-string-concatenation-use-cmstrcat.cxx:17:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:17:14: note: FIX-IT applied suggested code changes
17 | concat = a + " and this is a string literal" + 'O' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:22:48: note: FIX-IT applied suggested code changes
22 | concat = a + " and this is a string literal" + 'O' + b;
cmake-string-concatenation-use-cmstrcat.cxx:17:48: note: FIX-IT applied suggested code changes
17 | concat = a + " and this is a string literal" + 'O' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:22:54: note: FIX-IT applied suggested code changes
22 | concat = a + " and this is a string literal" + 'O' + b;
cmake-string-concatenation-use-cmstrcat.cxx:17:54: note: FIX-IT applied suggested code changes
17 | concat = a + " and this is a string literal" + 'O' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:22:57: note: FIX-IT applied suggested code changes
22 | concat = a + " and this is a string literal" + 'O' + b;
cmake-string-concatenation-use-cmstrcat.cxx:17:57: note: FIX-IT applied suggested code changes
17 | concat = a + " and this is a string literal" + 'O' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:24:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
24 | concat += b;
| ^~
| = cmStrCat(concat, )
cmake-string-concatenation-use-cmstrcat.cxx:24:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:24:14: note: FIX-IT applied suggested code changes
24 | concat += b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:25:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
25 | concat += " and this is a string literal";
| ^~
| = cmStrCat(concat, )
cmake-string-concatenation-use-cmstrcat.cxx:25:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:25:44: note: FIX-IT applied suggested code changes
25 | concat += " and this is a string literal";
| ^
cmake-string-concatenation-use-cmstrcat.cxx:26:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
26 | concat += 'o';
| ^~
| = cmStrCat(concat, )
cmake-string-concatenation-use-cmstrcat.cxx:26:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:26:16: note: FIX-IT applied suggested code changes
26 | concat += 'o';
| ^
cmake-string-concatenation-use-cmstrcat.cxx:27:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
27 | concat += b + " and this is a string literal " + 'o' + b;
cmake-string-concatenation-use-cmstrcat.cxx:19:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
19 | concat += a + a;
| ^~ ~
| = cmStrCat(concat, , )
cmake-string-concatenation-use-cmstrcat.cxx:19:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:19:15: note: FIX-IT applied suggested code changes
19 | concat += a + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:19:18: note: FIX-IT applied suggested code changes
19 | concat += a + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:20:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
20 | concat += " and this is a string literal" + a;
| ^~ ~
| = cmStrCat(concat, , )
cmake-string-concatenation-use-cmstrcat.cxx:20:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:20:45: note: FIX-IT applied suggested code changes
20 | concat += " and this is a string literal" + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:20:48: note: FIX-IT applied suggested code changes
20 | concat += " and this is a string literal" + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:21:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
21 | concat += b + 'o' + a;
| ^~ ~ ~
| = cmStrCat(concat, , , )
cmake-string-concatenation-use-cmstrcat.cxx:21:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:21:15: note: FIX-IT applied suggested code changes
21 | concat += b + 'o' + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:21:21: note: FIX-IT applied suggested code changes
21 | concat += b + 'o' + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:21:24: note: FIX-IT applied suggested code changes
21 | concat += b + 'o' + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:22:10: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
22 | concat += b + " and this is a string literal " + 'o' + b;
| ^~ ~ ~ ~
| = cmStrCat(concat, , , , )
cmake-string-concatenation-use-cmstrcat.cxx:27:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:27:15: note: FIX-IT applied suggested code changes
27 | concat += b + " and this is a string literal " + 'o' + b;
cmake-string-concatenation-use-cmstrcat.cxx:22:10: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:22:15: note: FIX-IT applied suggested code changes
22 | concat += b + " and this is a string literal " + 'o' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:27:50: note: FIX-IT applied suggested code changes
27 | concat += b + " and this is a string literal " + 'o' + b;
cmake-string-concatenation-use-cmstrcat.cxx:22:50: note: FIX-IT applied suggested code changes
22 | concat += b + " and this is a string literal " + 'o' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:27:56: note: FIX-IT applied suggested code changes
27 | concat += b + " and this is a string literal " + 'o' + b;
cmake-string-concatenation-use-cmstrcat.cxx:22:56: note: FIX-IT applied suggested code changes
22 | concat += b + " and this is a string literal " + 'o' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:27:59: note: FIX-IT applied suggested code changes
27 | concat += b + " and this is a string literal " + 'o' + b;
cmake-string-concatenation-use-cmstrcat.cxx:22:59: note: FIX-IT applied suggested code changes
22 | concat += b + " and this is a string literal " + 'o' + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:30:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
30 | concat = p.first + p.second;
| ^ ~
| cmStrCat( , )
cmake-string-concatenation-use-cmstrcat.cxx:30:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:30:20: note: FIX-IT applied suggested code changes
30 | concat = p.first + p.second;
cmake-string-concatenation-use-cmstrcat.cxx:25:12: warning: use cmStrCat() instead of string append [cmake-string-concatenation-use-cmstrcat]
25 | concat += a + b;
| ^~ ~
| = cmStrCat(concat, , )
cmake-string-concatenation-use-cmstrcat.cxx:25:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:25:17: note: FIX-IT applied suggested code changes
25 | concat += a + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:25:20: note: FIX-IT applied suggested code changes
25 | concat += a + b;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:30:30: note: FIX-IT applied suggested code changes
30 | concat = p.first + p.second;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:28:12: warning: use cmStrCat() instead of string concatenation [cmake-string-concatenation-use-cmstrcat]
28 | concat = p.first + p.second + a;
| ^ ~ ~
| cmStrCat( , , )
cmake-string-concatenation-use-cmstrcat.cxx:28:12: note: FIX-IT applied suggested code changes
cmake-string-concatenation-use-cmstrcat.cxx:28:20: note: FIX-IT applied suggested code changes
28 | concat = p.first + p.second + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:28:31: note: FIX-IT applied suggested code changes
28 | concat = p.first + p.second + a;
| ^
cmake-string-concatenation-use-cmstrcat.cxx:28:34: note: FIX-IT applied suggested code changes
28 | concat = p.first + p.second + a;
| ^
@@ -14,20 +14,18 @@ std::string concat;
// Correction needed
void test1()
{
concat = a + b;
concat = a + " and this is a string literal";
concat = a + 'O';
concat = "This is a string literal" + b;
concat = 'O' + a;
concat = a + " and this is a string literal" + 'O' + b;
concat += b;
concat += " and this is a string literal";
concat += 'o';
concat += a + a;
concat += " and this is a string literal" + a;
concat += b + 'o' + a;
concat += b + " and this is a string literal " + 'o' + b;
if (true)
concat += a + b;
std::pair<std::string, std::string> p;
concat = p.first + p.second;
concat = p.first + p.second + a;
}
// No correction needed
@@ -37,4 +35,17 @@ void test2()
a = "This is a string literal";
a = 'X';
cmStrCat(a, b);
concat = a + b;
concat = a + " and this is a string literal";
concat = a + 'O';
concat = "This is a string literal" + b;
concat = 'O' + a;
concat += b;
std::pair<std::string, std::string> p;
concat = p.first + p.second;
if (true)
concat += a;
}