strPlusChar: Fixed false positives

This commit is contained in:
Daniel Marjamäki 2009-01-18 17:42:41 +00:00
parent b1250e1750
commit f409861492
2 changed files with 17 additions and 5 deletions

View File

@ -987,17 +987,17 @@ void CheckOther::strPlusChar()
}
//
else if (Token::Match(tok, "%str% + %any%"))
else if (Token::Match(tok, "[=(] %str% + %any%"))
{
// char constant..
const char *s = tok->strAt(2);
const char *s = tok->strAt(3);
if (*s == '\'')
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok->next()));
// char variable..
unsigned int varid = tok->tokAt(2)->varId();
unsigned int varid = tok->tokAt(3)->varId();
if (varid > 0 && varid < 10000 && charVars[varid])
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok->next()));
}
}
}

View File

@ -44,6 +44,7 @@ private:
TEST_CASE(strPlusChar1); // "/usr" + '/'
TEST_CASE(strPlusChar2); // "/usr" + ch
TEST_CASE(strPlusChar3); // ok: path + "/sub" + '/'
}
void check(const char code[])
@ -201,6 +202,17 @@ private:
ASSERT_EQUALS(std::string("[test.cpp:4]: Unusual pointer arithmetic\n"), errout.str());
}
void strPlusChar3()
{
// Strange looking pointer arithmetic..
strPlusChar("void foo()\n"
"{\n"
" std::string temp = \"/tmp\";\n"
" std::string path = temp + '/' + \"sub\" + '/';\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
};
REGISTER_TEST(TestOther)