Browse Source

string.IsNullOrEmpty issue: detect 'str != null && str.Length > 0' pattern

pull/45/merge
Daniel Grunwald 12 years ago
parent
commit
658752eace
  1. 26
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/StringIsNullOrEmptyIssue.cs
  2. 10
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/StringIsNullOrEmptyInspectorTests.cs

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

@ -58,16 +58,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
), ),
// str == "" || str == null // str == "" || str == null
// str.Length == 0 || str == null
new BinaryOperatorExpression ( new BinaryOperatorExpression (
new Choice { PatternHelper.CommutativeOperator (new AnyNode ("str"), BinaryOperatorType.Equality, new PrimitiveExpression ("")),
PatternHelper.CommutativeOperator (new AnyNode ("str"), BinaryOperatorType.Equality, new PrimitiveExpression ("")),
PatternHelper.CommutativeOperator (
new MemberReferenceExpression (new AnyNode ("str"), "Length"),
BinaryOperatorType.Equality,
new PrimitiveExpression (0)
)
},
BinaryOperatorType.ConditionalOr, BinaryOperatorType.ConditionalOr,
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.Equality, new NullReferenceExpression ()) PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.Equality, new NullReferenceExpression ())
) )
@ -76,6 +68,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static readonly Pattern negPattern = new Choice { static readonly Pattern negPattern = new Choice {
// str != null && str != "" // str != null && str != ""
// str != null && str.Length != 0 // str != null && str.Length != 0
// str != null && str.Length > 0
new BinaryOperatorExpression ( new BinaryOperatorExpression (
PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ()), PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ()),
BinaryOperatorType.ConditionalAnd, BinaryOperatorType.ConditionalAnd,
@ -85,20 +78,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
new MemberReferenceExpression (new Backreference ("str"), "Length"), new MemberReferenceExpression (new Backreference ("str"), "Length"),
BinaryOperatorType.InEquality, BinaryOperatorType.InEquality,
new PrimitiveExpression (0) new PrimitiveExpression (0)
),
new BinaryOperatorExpression (
new MemberReferenceExpression (new Backreference ("str"), "Length"),
BinaryOperatorType.GreaterThan,
new PrimitiveExpression (0)
) )
} }
), ),
// str != "" && str != null // str != "" && str != null
// str.Length != 0 && str != null
new BinaryOperatorExpression ( new BinaryOperatorExpression (
new Choice { PatternHelper.CommutativeOperator (new AnyNode ("str"), BinaryOperatorType.InEquality, new PrimitiveExpression ("")),
PatternHelper.CommutativeOperator (new AnyNode ("str"), BinaryOperatorType.InEquality, new PrimitiveExpression ("")),
PatternHelper.CommutativeOperator (
new MemberReferenceExpression (new AnyNode ("str"), "Length"),
BinaryOperatorType.InEquality,
new PrimitiveExpression (0)
)
},
BinaryOperatorType.ConditionalAnd, BinaryOperatorType.ConditionalAnd,
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ()) PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ())
) )

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

@ -438,12 +438,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
[TestCase("str == null || str.Length == 0")] [TestCase("str == null || str.Length == 0")]
[TestCase("str == null || 0 == str.Length")] [TestCase("str == null || 0 == str.Length")]
[TestCase("str.Length == 0 || str == null")]
[TestCase("0 == str.Length || str == null")]
[TestCase("null == str || str.Length == 0")] [TestCase("null == str || str.Length == 0")]
[TestCase("null == str || 0 == str.Length")] [TestCase("null == str || 0 == str.Length")]
[TestCase("str.Length == 0 || null == str")]
[TestCase("0 == str.Length || null == str")]
public void TestInspectorCaseNL (string expression) public void TestInspectorCaseNL (string expression)
{ {
var input = @"class Foo var input = @"class Foo
@ -470,12 +466,10 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
[TestCase("str != null && str.Length != 0")] [TestCase("str != null && str.Length != 0")]
[TestCase("str != null && 0 != str.Length")] [TestCase("str != null && 0 != str.Length")]
[TestCase("str.Length != 0 && str != null")] [TestCase("str != null && str.Length > 0")]
[TestCase("0 != str.Length && str != null")]
[TestCase("null != str && str.Length != 0")] [TestCase("null != str && str.Length != 0")]
[TestCase("null != str && 0 != str.Length")] [TestCase("null != str && 0 != str.Length")]
[TestCase("str.Length != 0 && null != str")] [TestCase("null != str && str.Length > 0")]
[TestCase("0 != str.Length && null != str")]
public void TestInspectorCaseLN (string expression) public void TestInspectorCaseLN (string expression)
{ {
var input = @"class Foo var input = @"class Foo

Loading…
Cancel
Save