File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2920,9 +2920,11 @@ void CheckOther::checkRedundantCopy()
29202920 const Token* startTok = var->nameToken ();
29212921 if (startTok->strAt (1 ) == " =" ) // %type% %name% = ... ;
29222922 ;
2923- else if (Token::Match (startTok->next (), " (|{" ) && var->isClass () && var->typeScope ()) {
2923+ else if (Token::Match (startTok->next (), " (|{" ) && var->isClass ()) {
2924+ if (!var->typeScope () && !(var->valueType () && var->valueType ()->container ))
2925+ continue ;
29242926 // Object is instantiated. Warn if constructor takes arguments by value.
2925- if (constructorTakesReference (var->typeScope ()))
2927+ if (var-> typeScope () && constructorTakesReference (var->typeScope ()))
29262928 continue ;
29272929 } else if (Token::simpleMatch (startTok->next (), " ;" ) && startTok->next ()->isSplittedVarDeclEq ()) {
29282930 startTok = startTok->tokAt (2 );
@@ -2934,7 +2936,7 @@ void CheckOther::checkRedundantCopy()
29342936 continue ;
29352937 if (!Token::Match (tok->previous (), " %name% (" ))
29362938 continue ;
2937- if (!Token::Match (tok->link (), " ) )| ;" )) // bailout for usage like "const A a = getA()+3"
2939+ if (!Token::Match (tok->link (), " ) )|}| ;" )) // bailout for usage like "const A a = getA()+3"
29382940 continue ;
29392941
29402942 const Token* dot = tok->astOperand1 ();
Original file line number Diff line number Diff line change @@ -9047,6 +9047,22 @@ class TestOther : public TestFixture {
90479047 " [test.cpp:16] -> [test.cpp:18]: (style) The comparison 'c == m->get()' is always true because 'c' and 'm->get()' represent the same value.\n " ,
90489048 errout_str ());
90499049
9050+ check (" struct S {\n " // #12925
9051+ " const std::string & f() const { return str; }\n "
9052+ " std::string str;\n "
9053+ " };\n "
9054+ " void f(const S* s) {\n "
9055+ " const std::string v{ s->f() };\n "
9056+ " if (v.empty()) {}\n "
9057+ " }\n "
9058+ " void g(const S* s) {\n "
9059+ " const std::string w(s->f());\n "
9060+ " if (w.empty()) {}\n "
9061+ " }\n " );
9062+ ASSERT_EQUALS (" [test.cpp:6]: (performance, inconclusive) Use const reference for 'v' to avoid unnecessary data copying.\n "
9063+ " [test.cpp:10]: (performance, inconclusive) Use const reference for 'w' to avoid unnecessary data copying.\n " ,
9064+ errout_str ());
9065+
90509066 check (" struct T {\n "
90519067 " std::string s;\n "
90529068 " const std::string& get() const { return s; }\n "
You can’t perform that action at this time.
0 commit comments