From ad547af6f94b292a17528c96caf601a09f9789ea Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 10 May 2022 18:25:01 +0200 Subject: [PATCH] Add tests for #6323, #6855, #6857 (#4096) * Add tests for #6870, #10749 * Add test for #6372 * Add tests for #6855, #6857 * Add test for #6323 * Comment --- test/testcondition.cpp | 26 ++++++++++++++++++++++++++ test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 7107e99f7..9db7e5273 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4127,6 +4127,32 @@ private: " int method() override { return 1; }\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + // #6855 + check("struct S { int i; };\n" + "void f(S& s) {\n" + " if (!(s.i > 0) && (s.i != 0))\n" + " s.i = 0;\n" + " else if (s.i < 0)\n" + " s.s = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Condition 's.i<0' is always false\n", errout.str()); + + // #6857 + check("int bar(int i) { return i; }\n" + "void foo() {\n" + " if (bar(1) == 0 && bar(1) > 0) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'bar(1)==0' is always false\n" + "[test.cpp:3]: (style) Condition 'bar(1)>0' is always true\n", + errout.str()); + + check("struct S { int bar(int i) const; };\n" + "void foo(const S& s) {\n" + " if (s.bar(1) == 0 && s.bar(1) > 0) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: s.bar(1) == 0 && s.bar(1) > 0.\n", + errout.str()); } void alwaysTrueSymbolic() diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index d878092ba..60f3a0376 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6384,6 +6384,14 @@ private: " ((*&(*&s.t[0].p))) = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("struct S { int i; };\n" // #6323 + "void f() {\n" + " struct S s;\n" + " int x = -3;\n" + " int y = x < (1, s.i);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s.i\n", errout.str()); } void ctu_(const char* file, int line, const char code[]) {