diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 951f427d4..30553f5a7 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -105,9 +105,12 @@ void CheckIO::checkFileUsage() if (!var || !var->varId() || !Token::Match(var->typeStartToken(), "FILE *")) continue; - if (var->isLocal()) - filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED))); - else { + if (var->isLocal()) { + if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor" + filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN))); + else + filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED))); + } else { filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN))); // TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode } diff --git a/test/testio.cpp b/test/testio.cpp index cee49cfd1..cfe9856e7 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -265,6 +265,18 @@ private: " fwrite(buffer, 5, 6, f);\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void foo() {\n" + " FILE* f;\n" + " fwrite(buffer, 5, 6, f);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Used file that is not opened.\n", errout.str()); + + check("void foo() {\n" + " FILE* f(stdout);\n" + " fwrite(buffer, 5, 6, f);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void fileIOwithoutPositioning() {