#3744 (Unexpected unused value warning for char ptr types set in switch)

This commit is contained in:
Daniel Marjamäki 2012-04-28 15:43:42 +02:00
parent a0d92f5ed9
commit 082e6d506b
2 changed files with 19 additions and 1 deletions

View File

@ -445,7 +445,8 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
// not in same scope as declaration
else {
// no other assignment in this scope
if (var1->_assignments.find(scope) == var1->_assignments.end()) {
if (var1->_assignments.find(scope) == var1->_assignments.end() ||
scope->type == Scope::eSwitch) {
// nothing to replace
if (var1->_assignments.empty())
replace = false;

View File

@ -138,6 +138,7 @@ private:
TEST_CASE(localvarFunction); // ticket #1799
TEST_CASE(localvarIfNOT); // #3104 - if ( NOT var )
TEST_CASE(localvarAnd); // #3672
TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch
}
void checkStructMemberUsage(const char code[]) {
@ -3078,6 +3079,22 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarSwitch() { // #3744 - false positive when used in switch
functionVariableUsage("const char *f(int x) {\n"
" const char a[] = \"abc\";\n"
" const char def[] = \"def\";\n"
" const char *ptr;\n"
" switch(x) {\n"
" case 1: ptr=a; break;\n"
" default: ptr=def; break;\n"
" }\n"
" return ptr;\n"
"}");
// Don't write an error that "a" is not used
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestUnusedVar)