Browse Source

[CodeIssues] Added type checking to string is null or empty issue.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
4d49d61ab5
  1. 5
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/StringIsNullOrEmptyIssue.cs
  2. 18
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/StringIsNullOrEmptyInspectorTests.cs

5
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/StringIsNullOrEmptyIssue.cs

@ -31,7 +31,7 @@ using ICSharpCode.NRefactory.PatternMatching; @@ -31,7 +31,7 @@ using ICSharpCode.NRefactory.PatternMatching;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
/// <summary>
/// Checks for str == null && str == ""
/// Checks for str == null &amp;&amp; str == " "
/// Converts to: string.IsNullOrEmpty (str)
/// </summary>
[IssueDescription("Use string.IsNullOrEmpty",
@ -124,6 +124,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -124,6 +124,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
if (m.Success) {
var str = m.Get<Expression>("str").Single();
var def = ctx.Resolve (str).Type.GetDefinition ();
if (def == null || def.KnownTypeCode != ICSharpCode.NRefactory.TypeSystem.KnownTypeCode.String)
return;
AddIssue(binaryOperatorExpression, ctx.TranslateString("Use string.IsNullOrEmpty"), script => {
Expression expr = new PrimitiveType ("string").Invoke("IsNullOrEmpty", str.Clone());
if (isNegated)

18
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/StringIsNullOrEmptyInspectorTests.cs

@ -499,5 +499,23 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -499,5 +499,23 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
}
}");
}
[Test]
public void TestArrays ()
{
var input = @"class Foo
{
void Bar ()
{
int[] foo = new int[10];
if (foo == null || foo.Length == 0) {
}
}
}";
TestRefactoringContext context;
var issues = GetIssues (new StringIsNullOrEmptyIssue (), input, out context);
Assert.AreEqual (0, issues.Count);
}
}
}

Loading…
Cancel
Save