Fixed #3972: Don't issue auto variable error when the address is assigned to a reference to a non-pointer.
This commit is contained in:
parent
4ed15d87b6
commit
247d820027
|
@ -35,11 +35,11 @@ namespace {
|
|||
}
|
||||
|
||||
|
||||
bool CheckAutoVariables::isRefArg(unsigned int varId)
|
||||
bool CheckAutoVariables::isRefPtrArg(unsigned int varId)
|
||||
{
|
||||
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(varId);
|
||||
|
||||
return(var && var->isArgument() && var->isReference());
|
||||
return(var && var->isArgument() && var->isReference() && var->isPointer());
|
||||
}
|
||||
|
||||
bool CheckAutoVariables::isPtrArg(unsigned int varId)
|
||||
|
@ -92,7 +92,7 @@ void CheckAutoVariables::autoVariables()
|
|||
|
||||
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
|
||||
// Critical assignment
|
||||
if (Token::Match(tok, "[;{}] %var% = & %var%") && isRefArg(tok->next()->varId()) && isAutoVar(tok->tokAt(4)->varId())) {
|
||||
if (Token::Match(tok, "[;{}] %var% = & %var%") && isRefPtrArg(tok->next()->varId()) && isAutoVar(tok->tokAt(4)->varId())) {
|
||||
const Variable * var = symbolDatabase->getVariableFromVarId(tok->tokAt(4)->varId());
|
||||
if (checkRvalueExpression(var, tok->tokAt(5)))
|
||||
errorAutoVariableAssignment(tok->next(), false);
|
||||
|
|
|
@ -67,7 +67,7 @@ public:
|
|||
void returncstr();
|
||||
|
||||
private:
|
||||
bool isRefArg(unsigned int varId);
|
||||
bool isRefPtrArg(unsigned int varId);
|
||||
bool isPtrArg(unsigned int varId);
|
||||
bool isAutoVar(unsigned int varId);
|
||||
bool isAutoVarArray(unsigned int varId);
|
||||
|
|
|
@ -221,6 +221,12 @@ private:
|
|||
" p = &i;\n"
|
||||
"}", false);
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
|
||||
|
||||
check("void foo(std::string& s) {\n"
|
||||
" char* p = foo;\n"
|
||||
" s = &p[0];\n"
|
||||
"}", false);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void testautovar9() {
|
||||
|
|
Loading…
Reference in New Issue