From ae42ed27e0343391f7f30c1ab250d729fda9f431 Mon Sep 17 00:00:00 2001 From: leoowen19 Date: Sat, 31 Aug 2013 20:37:24 +0800 Subject: [PATCH 1/2] Merge branch 'ConvertToStaticTypeDefinition', remote-tracking branch 'upstream/master' into ConvertToStaticTypeDefinition From b32791c358ca3a15480648081c8dad629f7eb643 Mon Sep 17 00:00:00 2001 From: leoowen19 Date: Sun, 1 Sep 2013 19:41:51 +0800 Subject: [PATCH 2/2] Redundant Block in IfElse branches --- .../RedundantBlockInDifferentBranchesIssue.cs | 84 +++++++++ ...pCode.NRefactory.CSharp.Refactoring.csproj | 1 + .../RedundantBlockInDifferentBranchesTests.cs | 163 ++++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 4 files changed, 249 insertions(+) create mode 100644 ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Uncategorized/RedundantBlockInDifferentBranchesIssue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantBlockInDifferentBranchesTests.cs diff --git a/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Uncategorized/RedundantBlockInDifferentBranchesIssue.cs b/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Uncategorized/RedundantBlockInDifferentBranchesIssue.cs new file mode 100644 index 000000000..1b3649826 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Uncategorized/RedundantBlockInDifferentBranchesIssue.cs @@ -0,0 +1,84 @@ +// RedundantBlockInDifferentBranchesIssue.cs +// +// Author: +// Ji Kun +// +// Copyright (c) 2013 Ji Kun +// +// 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 + { + 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); + }); + } + } + } +} \ No newline at end of file diff --git a/ICSharpCode.NRefactory.CSharp.Refactoring/ICSharpCode.NRefactory.CSharp.Refactoring.csproj b/ICSharpCode.NRefactory.CSharp.Refactoring/ICSharpCode.NRefactory.CSharp.Refactoring.csproj index 7993c0ab8..ce463b11e 100644 --- a/ICSharpCode.NRefactory.CSharp.Refactoring/ICSharpCode.NRefactory.CSharp.Refactoring.csproj +++ b/ICSharpCode.NRefactory.CSharp.Refactoring/ICSharpCode.NRefactory.CSharp.Refactoring.csproj @@ -395,6 +395,7 @@ + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantBlockInDifferentBranchesTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantBlockInDifferentBranchesTests.cs new file mode 100644 index 000000000..929b85864 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantBlockInDifferentBranchesTests.cs @@ -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(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(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(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(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(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(input, 0); + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index f5ddac05d..ceef5946a 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -566,6 +566,7 @@ +