Browse Source

Fixed false positive that caused a 'is operator always returns true' warning even though it returns false. (e.g. 'intVar is double')

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
fe815dc2a0
  1. 9
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssue.cs
  2. 14
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssueTests.cs

9
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssue.cs

@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
using System.Collections.Generic;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -59,7 +60,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -59,7 +60,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var providedType = ctx.ResolveType (isExpression.Type);
var foundConversion = conversions.ImplicitConversion(type, providedType);
if (!foundConversion.IsValid)
if (!IsValidReferenceOrBoxingConversion(type, providedType))
return;
var action = new CodeAction (ctx.TranslateString ("Compare with 'null'"),
@ -68,6 +69,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -68,6 +69,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
AddIssue (isExpression, ctx.TranslateString ("Given expression is always of the provided type. " +
"Consider comparing with 'null' instead"), new [] { action });
}
bool IsValidReferenceOrBoxingConversion(IType fromType, IType toType)
{
Conversion c = conversions.ImplicitConversion(fromType, toType);
return c.IsValid && (c.IsIdentityConversion || c.IsReferenceConversion || c.IsBoxingConversion);
}
}
}
}

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

@ -90,5 +90,19 @@ class TestClass @@ -90,5 +90,19 @@ class TestClass
}";
Test<ExpressionIsAlwaysOfProvidedTypeIssue> (input, 1, output);
}
[Test]
public void IntIsNotDouble ()
{
var input = @"
sealed class TestClass
{
void TestMethod (int x)
{
if (x is double) ;
}
}";
Test<ExpressionIsAlwaysOfProvidedTypeIssue> (input, 0);
}
}
}

Loading…
Cancel
Save