Browse Source

[CodeIssue] NegativeRelationalExpressionIssue: no longer report issues for floating-point operations

newNRvisualizers
Mansheng Yang 13 years ago
parent
commit
3be1ef1bda
  1. 11
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/NegativeRelationalExpressionIssue.cs
  2. 14
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/NegativeRelationalExpressionIssueTests.cs

11
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/NegativeRelationalExpressionIssue.cs

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
// THE SOFTWARE.
using System.Collections.Generic;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -47,6 +48,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -47,6 +48,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
}
bool IsFloatingPoint (AstNode node)
{
var typeDef = ctx.Resolve (node).Type.GetDefinition ();
return typeDef != null &&
(typeDef.KnownTypeCode == KnownTypeCode.Single || typeDef.KnownTypeCode == KnownTypeCode.Double);
}
public override void VisitUnaryOperatorExpression (UnaryOperatorExpression unaryOperatorExpression)
{
base.VisitUnaryOperatorExpression (unaryOperatorExpression);
@ -66,6 +74,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -66,6 +74,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (negatedOp == BinaryOperatorType.Any)
return;
if (IsFloatingPoint (binaryOperatorExpr.Left) || IsFloatingPoint (binaryOperatorExpr.Right))
return;
AddIssue (unaryOperatorExpression, ctx.TranslateString ("Simplify negative relational expression"),
script => script.Replace (unaryOperatorExpression,
new BinaryOperatorExpression (binaryOperatorExpr.Left.Clone (), negatedOp,

14
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/NegativeRelationalExpressionIssueTests.cs

@ -89,5 +89,19 @@ class TestClass @@ -89,5 +89,19 @@ class TestClass
{
Test ("<=", ">");
}
[Test]
public void TestFloatingPoint ()
{
var input = @"
class TestClass
{
void TestMethod (double d)
{
var x = !(d > 0.1);
}
}";
Test<NegativeRelationalExpressionIssue> (input, 0);
}
}
}

Loading…
Cancel
Save