Why is this an issue?
Certain mathematical comparisons will always return the same value, and should not be performed.
Specifically, the following comparisons will return either always true or always false depending on the kind of
comparison:
- comparing a
charwith a numeric constant that is outside of the range ofchar - comparing a
floatwith a numeric constant that is outside of the range offloat - comparing a
longwith a numeric constant that is outside of the range oflong - comparing a
ulongwith a numeric constant that is outside of the range ofulong - etc.
Noncompliant code example
float f = 42.0f;
if (f <= double.MaxValue) { } // Noncompliant: always true
if (f > double.MaxValue) { } // Noncompliant: always false
Resources
Documentation
- Microsoft Learn: Comparison operators (C# reference)
- Microsoft Learn: Ranges for integral numeric types (C# reference)
- Microsoft Learn: Range for char (C# reference)