Browse Source

CompareFloatWithEqualityOperatorIssue: don't show warning when comparing with infinities.

pull/32/merge
Daniel Grunwald 13 years ago
parent
commit
67d80c8834
  1. 16
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/CompareFloatWithEqualityOperatorIssue.cs
  2. 35
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/CompareFloatWithEqualityOperatorIssueTests.cs

16
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/CompareFloatWithEqualityOperatorIssue.cs

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
// THE SOFTWARE.
using System.Collections.Generic;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
@ -60,6 +61,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -60,6 +61,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return IsFloatingPointType (ctx.Resolve (node).Type);
}
bool IsConstantInfinity(AstNode node)
{
ResolveResult rr = ctx.Resolve(node);
if (!rr.IsCompileTimeConstant)
return false;
if (rr.ConstantValue is float)
return float.IsInfinity((float)rr.ConstantValue);
else if (rr.ConstantValue is double)
return double.IsInfinity((double)rr.ConstantValue);
else
return false;
}
bool IsNaN (AstNode node, out string floatType)
{
floatType = "";
@ -105,6 +119,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -105,6 +119,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} else if (IsNaN (binaryOperatorExpression.Right, out floatType)) {
AddIsNaNIssue (binaryOperatorExpression, binaryOperatorExpression.Left, floatType);
} else if (IsFloatingPoint(binaryOperatorExpression.Left) || IsFloatingPoint(binaryOperatorExpression.Right)) {
if (IsConstantInfinity(binaryOperatorExpression.Left) || IsConstantInfinity(binaryOperatorExpression.Right))
return;
AddIssue (binaryOperatorExpression, ctx.TranslateString ("Compare a difference with EPSILON"),
script => {
// Math.Abs(diff) op EPSILON

35
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/CompareFloatWithEqualityOperatorIssueTests.cs

@ -94,5 +94,40 @@ class TestClass @@ -94,5 +94,40 @@ class TestClass
}";
Test<CompareFloatWithEqualityOperatorIssue> (input, 4, output);
}
[Test]
public void TestPositiveInfinity ()
{
var input = @"
class TestClass
{
void TestMethod (double x, float y)
{
bool test = x == System.Double.PositiveInfinity;
bool test2 = x != double.PositiveInfinity;
bool test3 = y == float.PositiveInfinity;
bool test4 = x != float.PositiveInfinity;
}
}";
Test<CompareFloatWithEqualityOperatorIssue> (input, 0);
}
[Test]
public void TestNegativeInfinity ()
{
var input = @"
class TestClass
{
void TestMethod (double x, float y)
{
bool test = x == System.Double.NegativeInfinity;
bool test2 = x != double.NegativeInfinity;
bool test3 = y == float.NegativeInfinity;
bool test4 = x != float.NegativeInfinity;
}
}";
Test<CompareFloatWithEqualityOperatorIssue> (input, 0);
}
}
}

Loading…
Cancel
Save