Fixed #1723 (Variable assigned value which is never used but is thrown)

This commit is contained in:
Robert Reif 2010-06-03 07:05:57 +02:00 committed by Daniel Marjamäki
parent 09e02b9f9f
commit 4c4844b722
4 changed files with 31 additions and 2 deletions

View File

@ -832,6 +832,8 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
addressOf = true;
next = start + 1;
}
else if (tok->tokAt(start)->str() == "new")
return 0;
else
next = start;
}
@ -1245,7 +1247,7 @@ void CheckOther::functionVariableUsage()
tok = tok->tokAt(8);
}
else if (Token::Match(tok, "delete|return %var%"))
else if (Token::Match(tok, "delete|return|throw %var%"))
variables.readAll(tok->next()->varId());
// assignment
@ -1350,7 +1352,7 @@ void CheckOther::functionVariableUsage()
variables.use(tok->varId()); // use = read + write
else if ((Token::Match(tok, "[(=&!]") || isOp(tok)) &&
(Token::Match(tok->next(), "%var%") && !Token::Match(tok->next(), "true|false")))
(Token::Match(tok->next(), "%var%") && !Token::Match(tok->next(), "true|false|new")))
variables.readAll(tok->next()->varId());
else if (Token::Match(tok, "-=|+=|*=|/=|&=|^= %var%") || Token::Match(tok, "|= %var%"))

View File

@ -2690,6 +2690,9 @@ void Tokenizer::setVarId()
if (tok->str() == "new")
continue;
if (tok->str() == "throw")
continue;
if (tok->str() == "unsigned")
tok = tok->next();

View File

@ -132,6 +132,7 @@ private:
TEST_CASE(varid_reference_to_containers);
TEST_CASE(varid_in_class);
TEST_CASE(varid_operator);
TEST_CASE(varid_throw);
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -1972,6 +1973,19 @@ private:
ASSERT_EQUALS(expected, actual);
}
void varid_throw() // ticket #1723
{
const std::string actual = tokenizeDebugListing(
"UserDefinedException* pe = new UserDefinedException();\n"
"throw pe;\n");
const std::string expected("\n\n##file 0\n"
"1: UserDefinedException * pe@1 ; pe@1 = new UserDefinedException ( ) ;\n"
"2: throw pe@1 ;\n");
ASSERT_EQUALS(expected, actual);
}
void varidclass1()
{
const std::string actual = tokenizeDebugListing(

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(localvar15);
TEST_CASE(localvar16); // ticket #1709
TEST_CASE(localvar17); // ticket #1720
TEST_CASE(localvar18); // ticket #1723
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
@ -1162,6 +1163,15 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'line_start' is assigned a value that is never used\n", errout.str());
}
void localvar18() // ticket #1723
{
functionVariableUsage("A::A(int iValue) {\n"
" UserDefinedException* pe = new UserDefinedException();\n"
" throw pe;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void localvaralias1()
{
functionVariableUsage("void foo()\n"