diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs index 3d9e262962..8a120134e5 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs @@ -32,13 +32,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring [ContextAction("Convert 'as' to cast.", Description = "Convert 'as' to cast.")] public class ConvertAsToCastAction : SpecializedCodeAction { - + static InsertParenthesesVisitor insertParentheses = new InsertParenthesesVisitor (); protected override CodeAction GetAction (RefactoringContext context, AsExpression node) { if (!node.AsToken.Contains (context.Location)) return null; - return new CodeAction (context.TranslateString ("Convert 'as' to cast"), - script => script.Replace (node, new CastExpression (node.Type.Clone (), node.Expression.Clone ()))); + return new CodeAction (context.TranslateString ("Convert 'as' to cast"), script => { + var castExpr = new CastExpression (node.Type.Clone (), node.Expression.Clone ()); + var parenthesizedExpr = node.Parent as ParenthesizedExpression; + if (parenthesizedExpr != null && parenthesizedExpr.Parent is Expression) { + // clone parent expression and replace the ParenthesizedExpression with castExpr to remove + // parentheses, then insert parentheses if necessary + var parentExpr = (Expression)parenthesizedExpr.Parent.Clone (); + parentExpr.GetNodeContaining (parenthesizedExpr.StartLocation, parenthesizedExpr.EndLocation) + .ReplaceWith (castExpr); + parentExpr.AcceptVisitor (insertParentheses); + script.Replace (parenthesizedExpr.Parent, parentExpr); + } else { + castExpr.AcceptVisitor (insertParentheses); + script.Replace (node, castExpr); + } + }); } } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAsToCastTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAsToCastTests.cs index 721bf18e98..90c3468fe2 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAsToCastTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAsToCastTests.cs @@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions public class ConvertAsToCastTests : ContextActionTestBase { [Test] - public void Test() + public void Test () { Test (@" using System; @@ -54,5 +54,45 @@ class TestClass } }"); } + + [Test] + public void TestRemoveParentheses () + { + string input = @" +class TestClass { + void TestMethod (object o) + { + var b = 1 + (o $as TestClass); + } +}"; + string output = @" +class TestClass { + void TestMethod (object o) + { + var b = 1 + (TestClass)o; + } +}"; + Test (input, output); + } + + [Test] + public void TestInsertParentheses () + { + string input = @" +class TestClass { + void TestMethod (object o) + { + var b = 1 + o $as TestClass; + } +}"; + string output = @" +class TestClass { + void TestMethod (object o) + { + var b = (TestClass)(1 + o); + } +}"; + Test (input, output); + } } }