Skip to content

Commit

Permalink
Exclude infinity from S2198, add testcases for Infinity+NaN (#6749)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed Feb 13, 2023
1 parent 2b0fa12 commit 32343e3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Expand Up @@ -72,7 +72,8 @@ private static bool TryGetConstantValue(SemanticModel model, BinaryExpressionSyn

// 'char' needs to roundtrip {{char -> int -> double}}, can't go {{char -> double}}
private static bool TryConvertToDouble(object constant, out double typedConstant) =>
ConversionHelper.TryConvertWith(constant is char ? Convert.ToInt32(constant) : constant, Convert.ToDouble, out typedConstant);
ConversionHelper.TryConvertWith(constant is char ? Convert.ToInt32(constant) : constant, Convert.ToDouble, out typedConstant)
&& !double.IsInfinity(typedConstant);

private static ValuesRange? TryGetRange(ITypeSymbol typeSymbol) =>
typeSymbol switch
Expand Down
Expand Up @@ -453,5 +453,44 @@ public void ConstantInBothOperands()
_ = 42f != double.MaxValue; // Compliant
_ = 42f <= double.MaxValue; // Compliant
}

public void Edgecases()
{
const float fPositiveInfinity = float.PositiveInfinity;
const float fNegativeInfinity = float.NegativeInfinity;
const double dPositiveInfinity = double.PositiveInfinity;
const double dNegativeInfinity = double.NegativeInfinity;

const float fNan = float.NaN;
const double dNan = double.NaN;

float f = 42;

_ = f <= fPositiveInfinity; // Compliant
_ = fNegativeInfinity < f; // Compliant

_ = fNegativeInfinity != f; // Compliant
_ = dNegativeInfinity == f; // Compliant

_ = f >= dPositiveInfinity; // Compliant
_ = dNegativeInfinity > f; // Compliant
_ = f != fPositiveInfinity; // Compliant
_ = f == dPositiveInfinity; // Compliant


_ = f == fNan; // Compliant
_ = f != fNan; // Compliant
_ = f <= fNan; // Compliant
_ = f >= fNan; // Compliant
_ = f > fNan; // Compliant
_ = f < fNan; // Compliant

_ = dNan == f; // Compliant
_ = dNan != f; // Compliant
_ = dNan <= f; // Compliant
_ = dNan >= f; // Compliant
_ = dNan > f; // Compliant
_ = dNan < f; // Compliant
}
}
}

0 comments on commit 32343e3

Please sign in to comment.