Skip to content

Commit 67b61a0

Browse files
Fix #12134 false negative: passedByValue (#5607)
1 parent c2d5aef commit 67b61a0

4 files changed

Lines changed: 12 additions & 2 deletions

File tree

cfg/std.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6977,6 +6977,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
69776977
</function>
69786978
<function name="std::string::substr">
69796979
<use-retval/>
6980+
<const/>
69806981
<returnValue type="std::string"/>
69816982
<noreturn>false</noreturn>
69826983
<arg nr="1" default="0" direction="in">
@@ -6990,6 +6991,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
69906991
</function>
69916992
<function name="std::wstring::substr">
69926993
<use-retval/>
6994+
<const/>
69936995
<returnValue type="std::wstring"/>
69946996
<noreturn>false</noreturn>
69956997
<arg nr="1" default="0" direction="in">
@@ -7003,6 +7005,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
70037005
</function>
70047006
<function name="std::basic_string::substr">
70057007
<use-retval/>
7008+
<const/>
70067009
<returnValue type="std::basic_string"/>
70077010
<noreturn>false</noreturn>
70087011
<arg nr="1" default="0" direction="in">

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,7 @@ void CheckOther::checkShadowVariables()
35963596
}
35973597
}
35983598

3599-
void CheckOther::shadowError(const Token *var, const Token *shadowed, std::string type)
3599+
void CheckOther::shadowError(const Token *var, const Token *shadowed, const std::string& type)
36003600
{
36013601
ErrorPath errorPath;
36023602
errorPath.emplace_back(shadowed, "Shadowed declaration");

lib/checkother.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class CPPCHECKLIB CheckOther : public Check {
284284
void accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive);
285285
void funcArgNamesDifferent(const std::string & functionName, nonneg int index, const Token* declaration, const Token* definition);
286286
void funcArgOrderDifferent(const std::string & functionName, const Token * declaration, const Token * definition, const std::vector<const Token*> & declarations, const std::vector<const Token*> & definitions);
287-
void shadowError(const Token *var, const Token *shadowed, std::string type);
287+
void shadowError(const Token *var, const Token *shadowed, const std::string& type);
288288
void knownArgumentError(const Token *tok, const Token *ftok, const ValueFlow::Value *value, const std::string &varexpr, bool isVariableExpressionHidden);
289289
void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value);
290290
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);

test/cfg/std.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4638,6 +4638,13 @@ void string_view_unused(std::string_view v)
46384638
v.substr(1, 3);
46394639
}
46404640

4641+
// cppcheck-suppress passedByValue
4642+
void string_substr(std::string s)
4643+
{
4644+
// cppcheck-suppress ignoredReturnValue
4645+
s.substr(1, 3);
4646+
}
4647+
46414648
void stdspan()
46424649
{
46434650
#ifndef __cpp_lib_span

0 commit comments

Comments
 (0)