C++: Exclude comparisons from enum constants in `cpp/constant-comparison

This commit is contained in:
Jeroen Ketema
2025-12-19 10:59:00 +01:00
parent 112eaadfae
commit 5117b5906b
3 changed files with 4 additions and 3 deletions

View File

@@ -25,7 +25,8 @@ 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, static asserts, non-type template arguments, and constexprs.
// instantiations, static asserts, non-type template arguments, enum constants,
// and constexprs.
from ComparisonOperation cmp, SmallSide ss, float left, float right, boolean value, string reason
where
not cmp.isInMacroExpansion() and
@@ -33,6 +34,7 @@ where
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 exists(EnumConstant e | e.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,5 +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:165:9:165:33 | ... > ... | Comparison is always true because ... * ... >= 64. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

View File

@@ -162,5 +162,5 @@ struct A_Struct {
};
enum E {
E_e = sizeof(A_Struct) * 8 > 50 // GOOD [FALSE POSITIVE]
E_e = sizeof(A_Struct) * 8 > 50 // GOOD
};