diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 23874e2af..6c9f85ef4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2178,6 +2178,11 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname) // Checking for shift by negative values //--------------------------------------------------------------------------- +static bool isNegative(const Token *tok, const Settings *settings) +{ + return tok->valueType() && tok->valueType()->sign == ValueType::SIGNED && tok->getValueLE(-1LL, settings); +} + void CheckOther::checkNegativeBitwiseShift() { for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) { @@ -2213,9 +2218,9 @@ void CheckOther::checkNegativeBitwiseShift() continue; // Get negative rhs value. preferably a value which doesn't have 'condition'. - if (tok->astOperand1()->getValueLE(-1LL, _settings)) + if (isNegative(tok->astOperand1(), _settings)) negativeBitwiseShiftError(tok, 1); - else if (tok->astOperand2()->getValueLE(-1LL, _settings)) + else if (isNegative(tok->astOperand2(), _settings)) negativeBitwiseShiftError(tok, 2); } } diff --git a/test/testother.cpp b/test/testother.cpp index 02d443457..e597e4744 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4788,6 +4788,10 @@ private: // Negative LHS check("const int x = -1 >> 2;"); ASSERT_EQUALS("[test.cpp:1]: (error) Shifting a negative value is undefined behaviour\n", errout.str()); + + // #6383 - unsigned type + check("const int x = (unsigned int)(-1) >> 2;"); + ASSERT_EQUALS("", errout.str()); } void incompleteArrayFill() {