From 3372603015c1002fb2aecdc7334fc53002c72e34 Mon Sep 17 00:00:00 2001 From: Mansheng Yang Date: Sun, 1 Jul 2012 20:41:46 +0800 Subject: [PATCH] [CodeIssue] Added ForControlVariableNotModifiedIssue --- .../ICSharpCode.NRefactory.CSharp.csproj | 1 + .../ForControlVariableNotModifiedIssue.cs | 141 ++++++++++++++++++ ...ForControlVariableNotModifiedIssueTests.cs | 133 +++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 4 files changed, 276 insertions(+) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ForControlVariableNotModifiedIssue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ForControlVariableNotModifiedIssueTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index 13bbb64d84..7a7d7ef576 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -262,6 +262,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ForControlVariableNotModifiedIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ForControlVariableNotModifiedIssue.cs new file mode 100644 index 0000000000..35802b8585 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ForControlVariableNotModifiedIssue.cs @@ -0,0 +1,141 @@ +// +// ForControlVariableNotModifiedIssue.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// 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 System.Collections.Generic; +using System.Linq; +using ICSharpCode.NRefactory.CSharp.Resolver; +using ICSharpCode.NRefactory.PatternMatching; +using ICSharpCode.NRefactory.Semantics; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + [IssueDescription ("'for' loop control variable is never modified", + Description = "'for' loop control variable is never modified.", + Category = IssueCategories.CodeQualityIssues, + Severity = Severity.Warning, + IssueMarker = IssueMarker.Underline)] + public class ForControlVariableNotModifiedIssue : ICodeIssueProvider + { + static FindReferences refFinder = new FindReferences (); + + public IEnumerable GetIssues (BaseRefactoringContext context) + { + var unit = context.RootNode as CompilationUnit; + if (unit == null) + return Enumerable.Empty (); + + return new GatherVisitor (context, unit).GetIssues (); + } + + class GatherVisitor : GatherVisitorBase + { + CompilationUnit unit; + public GatherVisitor(BaseRefactoringContext ctx, CompilationUnit unit) + : base (ctx) + { + this.unit = unit; + } + + static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl, + BinaryOperatorExpression condition) + { + var controlVariables = variableDecl.Variables.Where ( + v => + { + var identifier = new IdentifierExpression (v.Name); + return condition.Left.Match (identifier).Success || + condition.Right.Match (identifier).Success; + }).ToList (); + return controlVariables.Count == 1 ? controlVariables [0] : null; + } + + static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl, + UnaryOperatorExpression condition) + { + var controlVariables = variableDecl.Variables.Where ( + v => + { + var identifier = new IdentifierExpression (v.Name); + return condition.Expression.Match (identifier).Success; + }).ToList (); + return controlVariables.Count == 1 ? controlVariables [0] : null; + } + + public override void VisitForStatement (ForStatement forStatement) + { + base.VisitForStatement (forStatement); + + if (forStatement.Initializers.Count != 1) + return; + var variableDecl = forStatement.Initializers.First () as VariableDeclarationStatement; + if (variableDecl == null) + return; + + VariableInitializer controlVariable = null; + if (forStatement.Condition is BinaryOperatorExpression) { + controlVariable = GetControlVariable (variableDecl, (BinaryOperatorExpression)forStatement.Condition); + } else if (forStatement.Condition is UnaryOperatorExpression) { + controlVariable = GetControlVariable (variableDecl, (UnaryOperatorExpression)forStatement.Condition); + } else if (forStatement.Condition is IdentifierExpression) { + controlVariable = variableDecl.Variables.FirstOrDefault ( + v => v.Name == ((IdentifierExpression)forStatement.Condition).Identifier); + } + + if (controlVariable == null) + return; + + var localResolveResult = ctx.Resolve (controlVariable) as LocalResolveResult; + if (localResolveResult == null) + return; + + var modified = false; + refFinder.FindLocalReferences (localResolveResult.Variable, ctx.ParsedFile, unit, ctx.Compilation, + (node, resolveResult) => + { + if (modified) + return; + + var unary = node.Parent as UnaryOperatorExpression; + if (unary != null && unary.Expression == node) { + modified = unary.Operator == UnaryOperatorType.Decrement || + unary.Operator == UnaryOperatorType.PostDecrement || + unary.Operator == UnaryOperatorType.Increment || + unary.Operator == UnaryOperatorType.PostIncrement; + return; + } + + var assignment = node.Parent as AssignmentExpression; + modified = assignment != null && assignment.Left == node; + }, ctx.CancellationToken); + + if (!modified) + AddIssue (controlVariable.NameToken, + ctx.TranslateString ("'for' loop control variable is never modified")); + + } + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ForControlVariableNotModifiedIssueTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ForControlVariableNotModifiedIssueTests.cs new file mode 100644 index 0000000000..679550ca04 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ForControlVariableNotModifiedIssueTests.cs @@ -0,0 +1,133 @@ +// +// ForControlVariableNotModifiedIssueTests.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// 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 NUnit.Framework; + +namespace ICSharpCode.NRefactory.CSharp.CodeIssues +{ + [TestFixture] + public class ForControlVariableNotModifiedIssueTests : InspectionActionTestBase + { + [Test] + public void TestBinaryOpConditionNotModified () + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (int i = 0, j = 0; i < 10; j++) + { + } + } +}"; + Test (input, 1); + } + + [Test] + public void TestBinaryOpConditionModified () + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (int i = 0, j = 0; i < 10; i++) + { + } + } +}"; + Test (input, 0); + } + + [Test] + public void TestUnaryOpConditionNotModified () + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (bool x = true; !x;) + { + } + } +}"; + Test (input, 1); + } + + [Test] + public void TestUnaryOpConditionModified() + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (bool x = true; !x;) + { + x = false; + } + } +}"; + Test (input, 0); + } + + [Test] + public void TestIdentifierConditionNotModified () + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (bool x = true; x;) + { + } + } +}"; + Test (input, 1); + } + + [Test] + public void TestIdentifierConditionModified () + { + var input = @" +class TestClass +{ + void TestMethod () + { + for (bool x = false; x;) + { + x = true; + } + } +}"; + Test (input, 0); + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index de88219ffd..d2ab30666f 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -101,6 +101,7 @@ +