diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index 26c93a9da0..ba6e2a6cb4 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -373,6 +373,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRedundantCatchTypeAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRedundantCatchTypeAction.cs new file mode 100644 index 0000000000..507f672a9f --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRedundantCatchTypeAction.cs @@ -0,0 +1,92 @@ +// +// RemoveRedundantCatchTypeAction.cs +// +// Author: +// Simon Lindgren +// +// Copyright (c) 2012 Simon Lindgren +// +// 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 ICSharpCode.NRefactory.TypeSystem; +using System; +using ICSharpCode.NRefactory.Semantics; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + [ContextAction("Remove redundant type", + Description = "Removes a redundant exception type specifier from catch clauses.")] + public class RemoveRedundantCatchTypeAction : ICodeActionProvider + { + #region ICodeActionProvider implementation + public System.Collections.Generic.IEnumerable GetActions(RefactoringContext context) + { + var catchClause = context.GetNode(); + if (catchClause == null) + yield break; + if (catchClause.Type.IsNull) + yield break; + var exceptionType = context.ResolveType(catchClause.Type); + if (exceptionType != context.Compilation.FindType(typeof(Exception))) + yield break; + var exceptionIdentifierRR = context.Resolve(catchClause.VariableNameToken) as LocalResolveResult; + if (exceptionIdentifierRR != null && + IsReferenced(exceptionIdentifierRR.Variable, catchClause.Body, context)) + yield break; + yield return new CodeAction(context.TranslateString("Remove type specifier"), script => { + script.Replace(catchClause, new CatchClause() { + Body = catchClause.Body.Clone() as BlockStatement + }); + }); + } + + bool IsReferenced(IVariable variable, AstNode node, RefactoringContext context) + { + var visitor = new ReferenceFinderVisitor(context, variable); + node.AcceptVisitor(visitor); + return visitor.FoundReference; + } + + class ReferenceFinderVisitor: DepthFirstAstVisitor + { + RefactoringContext context; + IVariable variable; + + public ReferenceFinderVisitor(RefactoringContext context, IVariable variable) + { + this.context = context; + this.variable = variable; + } + + public bool FoundReference { get; private set; } + + public override void VisitIdentifierExpression(IdentifierExpression identifierExpression) + { + var resolvedIdentifier = context.Resolve(identifierExpression) as LocalResolveResult; + if (resolvedIdentifier != null) + FoundReference |= resolvedIdentifier.Variable == variable; + base.VisitIdentifierExpression(identifierExpression); + } + } + + #endregion + } +} + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs new file mode 100644 index 0000000000..c5c55a2d7e --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs @@ -0,0 +1,108 @@ +// +// RemoveRedundantCatchTypeTests.cs +// +// Author: +// Simon Lindgren +// +// Copyright (c) 2012 Simon Lindgren +// +// 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 NUnit.Framework; +using ICSharpCode.NRefactory.CSharp.Refactoring; + +namespace ICSharpCode.NRefactory.CSharp.CodeActions +{ + + [TestFixture] + public class RemoveRedundantCatchTypeTests : ContextActionTestBase + { + [Test] + public void RemovesSimpleExceptionMatch() + { + Test(@" +class TestClass +{ + public void F() + { + try { + } + catch $(System.Exception) { + } + } +}", @" +class TestClass +{ + public void F() + { + try { + } + catch { + } + } +}"); + } + + [Test] + public void PreservesBody() + { + Test(@" +class TestClass +{ + public void F() + { + try { + } + catch $(System.Exception e) { + System.Console.WriteLine (""Hi""); + } + } +}", @" +class TestClass +{ + public void F() + { + try { + } + catch { + System.Console.WriteLine (""Hi""); + } + } +}"); + } + + [Test] + public void IgnoresReferencedExceptionMatch() + { + TestWrongContext(@" +class TestClass +{ + public void F() + { + try { + } + catch $(System.Exception e) { + System.Console.WriteLine (e); + } + } +}"); + } + } +} + diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index fcba04c29c..4583bc3358 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -269,6 +269,7 @@ +