From 41fecb2e6dc4f8462ef60010e3b470562c4727e6 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 13 Jul 2012 06:01:19 -0700 Subject: [PATCH] Fixed evaluation of redundant conditions (#3972) --- lib/checkother.cpp | 8 ++++---- test/testother.cpp | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 945e16205..33f59654f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1109,14 +1109,14 @@ void CheckOther::checkIncorrectLogicOperator() { 0, { First, "<" }, "&&", { First, ">" }, 0, LessEqual, AlwaysFalse }, // (x < 1) && (x > 3) <- always false { 0, { First, "<=" }, "&&", { First, ">|>=" }, 0, Less, AlwaysFalse }, // (x <= 1) && (x > 3) <- always false { 0, { First, "<" }, "&&", { First, ">=" }, 0, Less, AlwaysFalse }, // (x < 1) && (x >= 3) <- always false - { "!!&&", { First, ">" }, "%oror%", { NA, "==" }, "!!&&", LessEqual, AlwaysTrue }, // (x > 3) || (x == 4) <- always true - { "!!&&", { First, "<" }, "%oror%", { NA, "==" }, "!!&&", MoreEqual, AlwaysTrue }, // (x < 5) || (x == 4) <- always true - { "!!&&", { First, ">=" }, "%oror%", { NA, "==" }, "!!&&", Less, AlwaysTrue }, // (x >= 3) || (x == 4) <- always true - { "!!&&", { First, "<=" }, "%oror%", { NA, "==" }, "!!&&", More, AlwaysTrue }, // (x <= 5) || (x == 4) <- always true { 0, { First, ">" }, "&&", { NA, "==" }, 0, MoreEqual, AlwaysFalse }, // (x > 5) && (x == 1) <- always false { 0, { First, "<" }, "&&", { NA, "==" }, 0, LessEqual, AlwaysFalse }, // (x < 1) && (x == 3) <- always false { 0, { First, ">=" }, "&&", { NA, "==" }, 0, More, AlwaysFalse }, // (x >= 5) && (x == 1) <- always false { 0, { First, "<=" }, "&&", { NA, "==" }, 0, Less, AlwaysFalse }, // (x <= 1) && (x == 3) <- always false + { "!!&&", { NA, "==" }, "%oror%", { First, ">" }, "!!&&", More, SecondTrue }, // (x == 4) || (x > 3) <- second expression always true + { "!!&&", { NA, "==" }, "%oror%", { First, "<" }, "!!&&", Less, SecondTrue }, // (x == 4) || (x < 5) <- second expression always true + { "!!&&", { NA, "==" }, "%oror%", { First, ">=" }, "!!&&", MoreEqual, SecondTrue }, // (x == 4) || (x >= 3) <- second expression always true + { "!!&&", { NA, "==" }, "%oror%", { First, "<=" }, "!!&&", LessEqual, SecondTrue }, // (x == 4) || (x <= 5) <- second expression always true { "!!&&", { First, ">" }, "%oror%", { NA, "!=" }, "!!&&", MoreEqual, SecondTrue }, // (x > 5) || (x != 1) <- second expression always true { "!!&&", { First, "<" }, "%oror%", { NA, "!=" }, "!!&&", LessEqual, SecondTrue }, // (x < 1) || (x != 3) <- second expression always true { "!!&&", { First, ">=" }, "%oror%", { NA, "!=" }, "!!&&", More, SecondTrue }, // (x >= 5) || (x != 1) <- second expression always true diff --git a/test/testother.cpp b/test/testother.cpp index ff0cb4e50..c9818fcbf 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2934,17 +2934,6 @@ private: } void incorrectLogicOperator3() { - check("void f(int x, bool& b) {\n" - " b = x > 3 || x == 4;\n" - " b = x < 5 || x == 4;\n" - " b = x >= 3 || x == 4;\n" - " b = x <= 5 || x == 4;\n" - "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Logical disjunction always evaluates to true: x > 3 || x == 4.\n" - "[test.cpp:3]: (warning) Logical disjunction always evaluates to true: x < 5 || x == 4.\n" - "[test.cpp:4]: (warning) Logical disjunction always evaluates to true: x >= 3 || x == 4.\n" - "[test.cpp:5]: (warning) Logical disjunction always evaluates to true: x <= 5 || x == 4.\n", errout.str()); - check("void f(int x, bool& b) {\n" " b = x > 5 && x == 1;\n" " b = x < 1 && x == 3;\n" @@ -2986,6 +2975,17 @@ private: ); ASSERT_EQUALS("", errout.str()); + check("void f(int x, bool& b) {\n" + " b = x > 3 || x == 4;\n" + " b = x < 5 || x == 4;\n" + " b = x >= 3 || x == 4;\n" + " b = x <= 5 || x == 4;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: If x == 4, the comparison x > 3 is always true.\n" + "[test.cpp:3]: (style) Redundant condition: If x == 4, the comparison x < 5 is always true.\n" + "[test.cpp:4]: (style) Redundant condition: If x == 4, the comparison x >= 3 is always true.\n" + "[test.cpp:5]: (style) Redundant condition: If x == 4, the comparison x <= 5 is always true.\n", errout.str()); + check("void f(int x, bool& b) {\n" " b = x > 5 || x != 1;\n" " b = x < 1 || x != 3;\n"