Browse Source

Added code issue to address bug #11856

https://bugzilla.xamarin.com/show_bug.cgi?id=11856

This works for expressions by doing the following:
- it looks if the expression is the first in a sequence of conditional expressions
- if is first, it extracts all expressions in the chained expressions
- after that it compares all expressions and if two are found as duplicates, the first duplicate is shown

The code fix will remove the duplicate expression.

This bug will also look to bit operations ( & and | ) as they have similar semantics
pull/32/merge
Ciprian Khlud 12 years ago
parent
commit
6e4f648b48
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 123
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateExpressionsInConditionsIssue.cs
  3. 81
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DuplicateExpressionsInConditionsIssueTests.cs
  4. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

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

@ -537,6 +537,7 @@ @@ -537,6 +537,7 @@
<Compile Include="Refactoring\CodeIssues\ConvertToStaticMethodIssue.cs" />
<Compile Include="Refactoring\CodeIssues\ParameterCanBeDemotedIssue\ParameterCanBeIEnumerableIssue.cs" />
<Compile Include="Refactoring\CodeIssues\DuplicateBodyMethodIssue.cs" />
<Compile Include="Refactoring\CodeIssues\DuplicateExpressionsInConditionsIssue.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

123
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateExpressionsInConditionsIssue.cs

@ -0,0 +1,123 @@ @@ -0,0 +1,123 @@
//
// DuplicateExpressionsInConditionsIssue.cs
//
// Author:
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
//
// Copyright (c) 2013 Ciprian Khlud
//
// 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.Collections.Generic;
using ICSharpCode.NRefactory.PatternMatching;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[IssueDescription("Expression has some redundant items",
Description = "Expression has some redundant items",
Category = IssueCategories.CodeQualityIssues,
Severity = Severity.Warning,
IssueMarker = IssueMarker.GrayOut,
ResharperDisableKeyword = "ConditionalTernaryEqualBranch")]
public class DuplicateExpressionsInConditionsIssue : ICodeIssueProvider
{
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
{
return new GatherVisitor (context).GetIssues ();
}
static readonly List<BinaryOperatorType> SupportedOperators = new List<BinaryOperatorType>();
static DuplicateExpressionsInConditionsIssue()
{
SupportedOperators.Add(BinaryOperatorType.BitwiseAnd);
SupportedOperators.Add(BinaryOperatorType.BitwiseOr);
SupportedOperators.Add(BinaryOperatorType.ConditionalAnd);
SupportedOperators.Add(BinaryOperatorType.ConditionalOr);
}
class GatherVisitor : GatherVisitorBase<IdenticalConditionalBranchIssue>
{
public GatherVisitor (BaseRefactoringContext ctx)
: base (ctx)
{
}
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
{
var expression = binaryOperatorExpression;
base.VisitBinaryOperatorExpression(expression);
if(!SupportedOperators.Contains(expression.Operator) )
return;
var parentExpression = expression.Parent as BinaryOperatorExpression;
if(parentExpression!=null && parentExpression.Operator==expression.Operator)
{
//handle only parent sequence
return;
}
var expressions = GetExpressions(binaryOperatorExpression, expression);
for(var i=0;i<expressions.Count-1;i++)
{
for (var j = i+1; j < expressions.Count; j++)
{
var expressionLeft = expressions[i];
var expressionRight = expressions[j];
if (!expressionLeft.IsMatch(expressionRight))
continue;
var action = new CodeAction(ctx.TranslateString("Remove redundant expression"),
script => RemoveRedundantExpression(script, expressionRight),
expressionRight);
AddIssue(expressionRight,
ctx.TranslateString(string.Format("The expression '{0}' is identical in the left branch",
expressionRight)), action);
}
}
}
private static void RemoveRedundantExpression(Script script, Expression expressionRight)
{
var parent = expressionRight.Parent as BinaryOperatorExpression;
if(parent==null) //should never happen!
return;
script.Replace(parent, parent.Left.Clone());
}
private static List<Expression> GetExpressions(BinaryOperatorExpression binaryOperatorExpression,
BinaryOperatorExpression expression)
{
var baseExpression = expression;
var leftExpression = baseExpression.FirstChild as BinaryOperatorExpression;
var expressions = new List<Expression>();
while (leftExpression != null && binaryOperatorExpression.Operator == leftExpression.Operator)
{
expressions.Add(baseExpression.Right);
baseExpression = leftExpression;
leftExpression = leftExpression.Left as BinaryOperatorExpression;
}
expressions.Add(baseExpression.Right);
expressions.Add(baseExpression.Left);
expressions.Reverse();
return expressions;
}
}
}
}

81
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DuplicateExpressionsInConditionsIssueTests.cs

@ -0,0 +1,81 @@ @@ -0,0 +1,81 @@
//
// DuplicateExpressionsInConditionsIssueTests.cs
//
// Author:
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
//
// Copyright (c) 2013 Ciprian Khlud
//
// 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 ICSharpCode.NRefactory.CSharp.Refactoring;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
{
[TestFixture]
public class DuplicateExpressionsInConditionsIssueTests : InspectionActionTestBase
{
[Test]
public void TestSimple ()
{
var input = @"
class TestClass
{
void TestMethod (int i)
{
var a = (i > 0) && (i > 0);
}
}";
var output = @"
class TestClass
{
void TestMethod (int i)
{
var a = (i > 0);
}
}";
Test<DuplicateExpressionsInConditionsIssue> (input, output);
}
[Test]
public void Test ()
{
var input = @"
class TestClass
{
void TestMethod (int i)
{
var a = (i > 0) && (i % 2 == 0) && (i > 0);
}
}";
var output = @"
class TestClass
{
void TestMethod (int i)
{
var a = (i > 0) && (i % 2 == 0);
}
}";
Test<DuplicateExpressionsInConditionsIssue> (input, output);
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -412,6 +412,7 @@ @@ -412,6 +412,7 @@
<Compile Include="CSharp\CodeIssues\ConvertToStaticMethodIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\ParameterCanBeDemotedIssue\ParameterCanBeIEnumerableTests.cs" />
<Compile Include="CSharp\CodeIssues\DuplicateBodyMethodIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\DuplicateExpressionsInConditionsIssueTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save