C++: Exclude more comparisons from cpp/constant-comparison

This commit is contained in:
Jeroen Ketema
2025-12-04 23:09:48 +01:00
parent 509cbf7049
commit 90d6c9fc56
3 changed files with 8 additions and 10 deletions

View File

@@ -25,11 +25,14 @@ import UnsignedGEZero
//
// So to reduce the number of false positives, we do not report a result if
// the comparison is in a macro expansion. Similarly for template
// instantiations.
// instantiations, static asserts, non-type template arguments, and constexprs.
from ComparisonOperation cmp, SmallSide ss, float left, float right, boolean value, string reason
where
not cmp.isInMacroExpansion() and
not cmp.isFromTemplateInstantiation(_) and
not exists(StaticAssert s | s.getCondition() = cmp.getParent*()) and
not exists(Declaration d | d.getATemplateArgument() = cmp.getParent*()) and
not exists(Variable v | v.isConstexpr() | v.getInitializer().getExpr() = cmp.getParent*()) and
not functionContainsDisabledCode(cmp.getEnclosingFunction()) and
reachablePointlessComparison(cmp, left, right, value, ss) and
// a comparison between an enum and zero is always valid because whether

View File

@@ -50,9 +50,4 @@
| PointlessComparison.cpp:43:6:43:29 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327.5. |
| PointlessComparison.cpp:44:6:44:28 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327.5. |
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
| RegressionTests.cpp:131:17:131:25 | ... < ... | Comparison is always true because 43 <= ... + .... |
| RegressionTests.cpp:135:26:135:52 | ... < ... | Comparison is always false because 16 >= ... * .... |
| RegressionTests.cpp:140:50:140:61 | ... < ... | Comparison is always false because ... * ... >= 84. |
| RegressionTests.cpp:154:21:154:31 | ... < ... | Comparison is always false because ... - ... >= 34. |
| RegressionTests.cpp:154:21:154:31 | ... < ... | Comparison is always false because ... - ... >= 38. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

View File

@@ -128,17 +128,17 @@ void testTempObject() {
void staticAssert() {
static const int a = 42;
static const int b = 43;
static_assert(a < b + 0, ""); // GOOD [FALSE POSITIVE]
static_assert(a < b + 0, ""); // GOOD
}
constexpr int global_1 = 42;
constexpr int global_2 = global_1 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD [FALSE POSITIVE]
constexpr int global_2 = global_1 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
static const int global_3 = 42;
static const int global_4 = global_3 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
template<unsigned int p, unsigned int n, bool = ((2u * n) < p)>
struct templateCompare : public templateCompare<p, 2u * n> // GOOD [FALSE POSITIVE]
struct templateCompare : public templateCompare<p, 2u * n> // GOOD
{ };
template< unsigned int p, unsigned int n>
@@ -151,7 +151,7 @@ unsigned int templateCompare_x = templateCompare<42, 42>::v;
template<int n>
struct someType {
typedef someType<((n - 4) < 0 ? 0 : n - 4)> b; // GOOD [FALSE POSITIVE]
typedef someType<((n - 4) < 0 ? 0 : n - 4)> b; // GOOD
};
someType<42>::b someType_x;