Skip to content

Commit f6dff92

Browse files
Fix #7515 FN stlcstrStream with std::cout (#8040)
1 parent ea63a21 commit f6dff92

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/checkstl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ void CheckStl::string_c_str()
20822082
const Token* strm = tok;
20832083
while (Token::simpleMatch(strm, "<<"))
20842084
strm = strm->astOperand1();
2085-
if (strm && strm->variable() && strm->variable()->isStlType())
2085+
if (strm && ((strm->variable() && strm->variable()->isStlType()) || Token::Match(strm->tokAt(-1), "std :: cout|cerr")))
20862086
string_c_strStream(tok);
20872087
}
20882088
}

test/teststl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,6 +4568,14 @@ class TestStl : public TestFixture {
45684568
"[test.cpp:14:10]: (performance) Passing the result of c_str() to a stream is slow and redundant. [stlcstrStream]\n",
45694569
errout_str());
45704570

4571+
check("void f(const std::string& s) {\n"
4572+
" std::cout << s.c_str();\n"
4573+
" std::cerr << s.c_str();\n"
4574+
"}\n");
4575+
ASSERT_EQUALS("[test.cpp:2:15]: (performance) Passing the result of c_str() to a stream is slow and redundant. [stlcstrStream]\n"
4576+
"[test.cpp:3:15]: (performance) Passing the result of c_str() to a stream is slow and redundant. [stlcstrStream]\n",
4577+
errout_str());
4578+
45714579
check("struct S { std::string str; };\n"
45724580
"struct T { S s; };\n"
45734581
"struct U { T t[1]; };\n"

0 commit comments

Comments
 (0)