Unused struct members: bail out upon casting

This commit is contained in:
Daniel Marjamäki 2009-10-17 17:36:55 +02:00
parent fa14dcbf68
commit 2e9f7755ee
3 changed files with 23 additions and 2 deletions

View File

@ -619,6 +619,11 @@ void CheckOther::checkStructMemberUsage()
if (tok2->str() == "}")
break;
}
// Bail out if some data is casted to struct..
const std::string s("( struct " + tok->next()->str() + " * ) & %var% [");
if (Token::findmatch(tok, s.c_str()))
structname = 0;
}
if (tok->str() == "}")

View File

@ -54,6 +54,7 @@ public:
checkOther.checkUnsignedDivision();
checkOther.checkCharVariable();
checkOther.checkVariableScope();
checkOther.checkStructMemberUsage();
}
}
@ -65,7 +66,6 @@ public:
{
checkOther.warningRedundantCode();
checkOther.checkConstantFunctionParameter();
checkOther.checkStructMemberUsage();
checkOther.checkIncompleteStatement();
if (settings->_showAll)
{

View File

@ -40,7 +40,6 @@ private:
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error buffer..
errout.str("");
@ -58,6 +57,7 @@ private:
TEST_CASE(structmember3);
TEST_CASE(structmember4);
TEST_CASE(structmember5);
TEST_CASE(structmember6);
}
void structmember1()
@ -149,6 +149,22 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
}
void structmember6()
{
check("struct AB\n"
"{\n"
" int a;\n"
" int b;\n"
"};\n"
"\n"
"void foo(char *buf)\n"
"{\n"
" struct AB *ab = (struct AB *)&buf[10];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestUnusedVar)