Improved check for shifting by negative values and removed false positives
This commit is contained in:
parent
dcba21dedd
commit
67e40a85e5
|
@ -3329,10 +3329,18 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
|
|||
|
||||
void CheckOther::checkNegativeBitwiseShift()
|
||||
{
|
||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||
|
||||
for (const Token *tok = _tokenizer->tokens(); tok ; tok = tok->next()) {
|
||||
if (Token::Match(tok,"%var% >>|<< %num%") || Token::Match(tok,"%num >>|<< %num%")) {
|
||||
if ((tok->strAt(2))[0] == '-')
|
||||
negativeBitwiseShiftError(tok);
|
||||
if ((Token::Match(tok,"%var% >>|<< %num%") || Token::Match(tok,"%num% >>|<< %num%")) && !Token::Match(tok->previous(),">>|<<")) {
|
||||
if (tok->isName()) {
|
||||
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId());
|
||||
if (var && var->typeStartToken()->isStandardType() && (tok->strAt(2))[0] == '-')
|
||||
negativeBitwiseShiftError(tok);
|
||||
} else {
|
||||
if ((tok->strAt(2))[0] == '-')
|
||||
negativeBitwiseShiftError(tok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6111,6 +6111,26 @@ private:
|
|||
" a >>= -1;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" int a = 123 << -1;\n"
|
||||
"}");
|
||||
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Shifting by a negative value.\n", "", errout.str());
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" std::cout << -1;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" std::cout << a << -1 ;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" std::cout << 3 << -1 ;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void incompleteArrayFill() {
|
||||
|
|
Loading…
Reference in New Issue