Browse Source

Don't produce NegativeRelationalExpressionIssue inside operator declarations

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
64b8217fb0
  1. 45
      ICSharpCode.NRefactory.CSharp/Ast/SyntaxExtensions.cs
  2. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  3. 16
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/NegativeRelationalExpressionIssue.cs
  4. 13
      ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs
  5. 33
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/NegativeRelationalExpressionIssueTests.cs

45
ICSharpCode.NRefactory.CSharp/Ast/SyntaxExtensions.cs

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
// Copyright (c) 2013 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// Extension methods for the syntax tree.
/// </summary>
public static class SyntaxExtensions
{
public static bool IsComparisonOperator(this OperatorType operatorType)
{
switch (operatorType) {
case OperatorType.Equality:
case OperatorType.Inequality:
case OperatorType.GreaterThan:
case OperatorType.LessThan:
case OperatorType.GreaterThanOrEqual:
case OperatorType.LessThanOrEqual:
return true;
default:
return false;
}
}
}
}

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -93,6 +93,7 @@ @@ -93,6 +93,7 @@
<Compile Include="Ast\AstType.cs" />
<Compile Include="Ast\DocumentationReference.cs" />
<Compile Include="Ast\IdentifierExpressionBackreference.cs" />
<Compile Include="Ast\SyntaxExtensions.cs" />
<Compile Include="Ast\SyntaxTree.cs" />
<Compile Include="Ast\ComposedType.cs" />
<Compile Include="Ast\CSharpModifierToken.cs" />

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

@ -74,14 +74,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -74,14 +74,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (negatedOp == BinaryOperatorType.Any)
return;
if (IsFloatingPoint (binaryOperatorExpr.Left) || IsFloatingPoint (binaryOperatorExpr.Right))
return;
if (IsFloatingPoint (binaryOperatorExpr.Left) || IsFloatingPoint (binaryOperatorExpr.Right)) {
if (negatedOp != BinaryOperatorType.Equality && negatedOp != BinaryOperatorType.InEquality)
return;
}
AddIssue (unaryOperatorExpression, ctx.TranslateString ("Simplify negative relational expression"),
script => script.Replace (unaryOperatorExpression,
new BinaryOperatorExpression (binaryOperatorExpr.Left.Clone (), negatedOp,
binaryOperatorExpr.Right.Clone ())));
}
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{
if (operatorDeclaration.OperatorType.IsComparisonOperator()) {
// Ignore operator declaration; within them it's common to define one operator
// by negating another.
return;
}
base.VisitOperatorDeclaration(operatorDeclaration);
}
}
}
}

13
ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs

@ -1188,18 +1188,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1188,18 +1188,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
static bool IsComparisonOperator(IMethod m)
{
var type = OperatorDeclaration.GetOperatorType(m.Name);
if (type.HasValue) {
switch (type.Value) {
case OperatorType.Equality:
case OperatorType.Inequality:
case OperatorType.GreaterThan:
case OperatorType.LessThan:
case OperatorType.GreaterThanOrEqual:
case OperatorType.LessThanOrEqual:
return true;
}
}
return false;
return type.HasValue && type.Value.IsComparisonOperator();
}
sealed class LiftedUserDefinedOperator : SpecializedMethod, OverloadResolution.ILiftedOperator

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

@ -100,6 +100,39 @@ class TestClass @@ -100,6 +100,39 @@ class TestClass
{
var x = !(d > 0.1);
}
}";
Test<NegativeRelationalExpressionIssue> (input, 0);
}
[Test]
public void TestFloatingPointEquality ()
{
var input = @"
class TestClass
{
void TestMethod (double d)
{
var x = !(d == 0.1);
}
}";
Test<NegativeRelationalExpressionIssue> (input, 1);
}
[Test]
public void TestUserDefinedOperator ()
{
var input = @"
struct LineChangeInfo
{
public static bool operator ==(LineChangeInfo lhs, LineChangeInfo rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(LineChangeInfo lhs, LineChangeInfo rhs)
{
return !(lhs == rhs);
}
}";
Test<NegativeRelationalExpressionIssue> (input, 0);
}

Loading…
Cancel
Save