Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Redundant block in branches #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// RedundantBlockInDifferentBranchesIssue.cs
//
// Author:
// Ji Kun <[email protected]>
//
// Copyright (c) 2013 Ji Kun <[email protected]>
//
// 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.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.PatternMatching;
using ICSharpCode.NRefactory.Refactoring;

namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[IssueDescription("RedundantBlockInDifferentBranches",
Description = "Blocks in if/else can be simplified to any of the branches if they have the same block.",
Category = IssueCategories.RedundanciesInCode,
Severity = Severity.Hint,
ResharperDisableKeyword = "RedundantBlockInDifferentBranches",
IssueMarker = IssueMarker.WavedLine)]
public class RedundantBlockInDifferentBranchesIssue : GatherVisitorCodeIssueProvider
{

protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)
{
return new GatherVisitor(context, this);
}

class GatherVisitor : GatherVisitorBase<RedundantBlockInDifferentBranchesIssue>
{
public GatherVisitor(BaseRefactoringContext ctx, RedundantBlockInDifferentBranchesIssue issueProvider) : base (ctx, issueProvider)
{
}

static readonly AstNode pattern = new Choice{
new IfElseStatement(
new AnyNode("c"),
new AnyNode("s"),
new BlockStatement{new Backreference("s")}),
new IfElseStatement(
new AnyNode("c"),
new AnyNode("s"),
new Backreference("s")),
new IfElseStatement(
new AnyNode("c"),
new BlockStatement{new AnyNode("s")},
new Backreference("s"))
};

public override void VisitIfElseStatement(IfElseStatement ifElseStatement)
{
base.VisitIfElseStatement(ifElseStatement);
var m = pattern.Match(ifElseStatement);

if (!m.Success)
return;

AddIssue(ifElseStatement.ElseToken, ctx.TranslateString("Blocks in if/else or switch branches can be simplified to any of the branches if they have the same block."), ctx.TranslateString("Change if/else statement to statements"),
script =>
{
IfElseStatement newStatement = new IfElseStatement(ifElseStatement.Condition.Clone(), ifElseStatement.TrueStatement.Clone());
script.Replace(ifElseStatement, newStatement);
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
<Compile Include="CodeIssues\Synced\RedundanciesInCode\RedundantLogicalConditionalExpressionOperandIssue.cs" />
<Compile Include="CodeIssues\Synced\RedundanciesInCode\ForStatementConditionIsTrueIssue.cs" />
<Compile Include="CodeIssues\Synced\Opportunities\ConvertToStaticTypeIssue.cs" />
<Compile Include="CodeIssues\Uncategorized\RedundantBlockInDifferentBranchesIssue.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using ICSharpCode.NRefactory.CSharp.Refactoring;
using NUnit.Framework;

namespace ICSharpCode.NRefactory.CSharp.CodeIssues
{
[TestFixture]
public class RedundantBlockInDifferentBranchesTests : InspectionActionTestBase
{
[Test]
public void TestConditionalExpression1()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
{
foo = foo + 1;
foo = foo + foo;
}
else
{
foo = foo + 1;
foo = foo + foo;
}
}
}";
var output = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5) {
foo = foo + 1;
foo = foo + foo;
}
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 1, output);
}

[Test]
public void TestConditionalExpression2()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
foo = foo + 1;
else
foo = foo + 1;
}
}";
var output = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
foo = foo + 1;
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 1, output);
}

[Test]
public void TestConditionalExpression3()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
foo = foo + 1;
else
{
foo = foo + 1;
}
}
}";
var output = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
foo = foo + 1;
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 1, output);
}

[Test]
public void TestConditionalExpression4()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
{
}
else
{}
}
}";
var output = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5) {
}
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 1 , output);
}

[Test]
public void TestNoProblem2()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
if (foo > 5)
{
foo = foo + 1;
return 2;
}
else
return 5;
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 0);
}

[Test]
public void TestResharperDisableRestore()
{
var input = @"
class TestClass
{
void TestMethod (int foo)
{
//Resharper disable RedundantBlockInDifferentBranches
if (foo > 5)
return 2;
else
return 5;
//Resharper restore RedundantBlockInDifferentBranches
}
}";
Test<RedundantBlockInDifferentBranchesIssue>(input, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@
<Compile Include="CSharp\CodeActions\SplitIfActionTests.cs" />
<Compile Include="CSharp\CodeIssues\RedundantLogicalConditionalExpressionOperandIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\ConvertToStaticTypeTests.cs" />
<Compile Include="CSharp\CodeIssues\RedundantBlockInDifferentBranchesTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">
Expand Down