diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 619b8d6f9..bf665daba 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -68,6 +68,19 @@ void CheckOther::checkCastIntToCharAndBack() checkCastIntToCharAndBackError(tok, tok->strAt(2)); } } + } else if (Token::Match(tok, "EOF %comp% ( %var% = std :: cin . get (") || Token::Match(tok, "EOF %comp% ( %var% = cin . get (")) { + tok = tok->tokAt(3); + if (tok && tok->varId()) { + const Variable *var = tok->variable(); + if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) { + checkCastIntToCharAndBackError(tok, "cin.get"); + } + } + } else if (Token::Match(tok, "%var% = std :: cin . get (") || Token::Match(tok, "%var% = cin . get (")) { + const Variable *var = tok->variable(); + if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) { + vars[tok->varId()] = "cin.get"; + } } if (Token::Match(tok, "%var% %comp% EOF")) { if (vars.find(tok->varId()) != vars.end()) { diff --git a/test/testother.cpp b/test/testother.cpp index f64a2cd04..6b110a2d3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7265,6 +7265,72 @@ private: "} while (EOF != i)" "}\n"); ASSERT_EQUALS("", errout.str()); + + // cin.get() + check("void f(){\n" + " char ch;\n" + " while ((ch = std::cin.get()) != EOF) {\n" + " std::cout << ch;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str()); + + check("void f(){\n" + " char ch;\n" + " while ((ch = cin.get()) != EOF) {\n" + " std::cout << ch;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str()); + + check("void f(){\n" + " int i;\n" + " while ((i = cin.get()) != EOF) {\n" + " std::cout << i;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(){\n" + " int i;\n" + " while ((i = std::cin.get()) != EOF) {\n" + " std::cout << i;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(){\n" + " char ch;\n" + " while ( EOF != (ch = std::cin.get()) ) {\n" + " std::cout << ch;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str()); + + check("void f(){\n" + " char ch;\n" + " while ( EOF != (ch = cin.get()) ) {\n" + " std::cout << ch;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF.\n", errout.str()); + + check("void f(){\n" + " int i;\n" + " while ( EOF != (i = cin.get()) ) {\n" + " std::cout << i;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(){\n" + " int i;\n" + " while ( EOF != (i = std::cin.get()) ) {\n" + " std::cout << i;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } };