From 69d4fd1ff13cc281ab98dbbe1ebe11a12fb4d22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Wed, 27 Feb 2013 13:46:41 +0100 Subject: [PATCH] RedundantAssignment issue is now more careful when removing invocations. --- .../CodeIssues/RedundantAssignmentIssue.cs | 28 +++++++++++++++++++ .../RedundantAssignmentIssueTests.cs | 16 +++++++++++ 2 files changed, 44 insertions(+) diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs index 3b6ad417de..23e7a1da6e 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs @@ -140,12 +140,40 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring ProcessNodes (startNode); } + class SearchInvocationsVisitor : DepthFirstAstVisitor + { + bool foundInvocations; + + public bool ContainsInvocations(AstNode node) + { + foundInvocations = false; + node.AcceptVisitor (this); + return foundInvocations; + } + + protected override void VisitChildren (AstNode node) + { + AstNode next; + for (var child = node.FirstChild; child != null && !foundInvocations; child = next) { + next = child.NextSibling; + child.AcceptVisitor (this); + } + } + + public override void VisitInvocationExpression(InvocationExpression invocationExpression) + { + foundInvocations = true; + } + } + void AddIssue (AstNode node) { var title = ctx.TranslateString ("Remove redundant assignment"); var variableInitializer = node as VariableInitializer; if (variableInitializer != null) { + if (new SearchInvocationsVisitor ().ContainsInvocations (variableInitializer.Initializer)) + return; AddIssue (variableInitializer.Initializer, title, script => { var replacement = (VariableInitializer)node.Clone (); diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs index b8ebcd516b..f21bfc9897 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs @@ -390,6 +390,22 @@ class TestClass // nothing } } +}"; + Test (input, 0); + } + + [Test] + public void TestAssignmentWithFunction () + { + var input = @"using System; +class TestClass +{ + void Test(TestClass a) { } + TestClass Func () { return null; } + void TestMethod () + { + var a = Func (); + } }"; Test (input, 0); }