2
// ForControlVariableNotModifiedIssue.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.PatternMatching;
30
using ICSharpCode.NRefactory.Semantics;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[IssueDescription ("'for' loop control variable is never modified",
35
Description = "'for' loop control variable is never modified.",
36
Category = IssueCategories.CodeQualityIssues,
37
Severity = Severity.Warning,
38
IssueMarker = IssueMarker.Underline)]
39
public class ForControlVariableNotModifiedIssue : ICodeIssueProvider
41
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
43
return new GatherVisitor (context).GetIssues ();
46
class GatherVisitor : GatherVisitorBase<ForControlVariableNotModifiedIssue>
48
public GatherVisitor (BaseRefactoringContext ctx)
53
static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl,
54
BinaryOperatorExpression condition)
56
var controlVariables = variableDecl.Variables.Where (
59
var identifier = new IdentifierExpression (v.Name);
60
return condition.Left.Match (identifier).Success ||
61
condition.Right.Match (identifier).Success;
63
return controlVariables.Count == 1 ? controlVariables [0] : null;
66
static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl,
67
UnaryOperatorExpression condition)
69
var controlVariables = variableDecl.Variables.Where (
72
var identifier = new IdentifierExpression (v.Name);
73
return condition.Expression.Match (identifier).Success;
75
return controlVariables.Count == 1 ? controlVariables [0] : null;
78
public override void VisitForStatement (ForStatement forStatement)
80
base.VisitForStatement (forStatement);
82
if (forStatement.Initializers.Count != 1)
84
var variableDecl = forStatement.Initializers.First () as VariableDeclarationStatement;
85
if (variableDecl == null)
88
VariableInitializer controlVariable = null;
89
if (forStatement.Condition is BinaryOperatorExpression) {
90
controlVariable = GetControlVariable (variableDecl, (BinaryOperatorExpression)forStatement.Condition);
91
} else if (forStatement.Condition is UnaryOperatorExpression) {
92
controlVariable = GetControlVariable (variableDecl, (UnaryOperatorExpression)forStatement.Condition);
93
} else if (forStatement.Condition is IdentifierExpression) {
94
controlVariable = variableDecl.Variables.FirstOrDefault (
95
v => v.Name == ((IdentifierExpression)forStatement.Condition).Identifier);
98
if (controlVariable == null)
101
var localResolveResult = ctx.Resolve (controlVariable) as LocalResolveResult;
102
if (localResolveResult == null)
105
var results = ctx.FindReferences (forStatement, localResolveResult.Variable);
106
var modified = false;
107
foreach (var result in results) {
110
var node = result.Node;
111
var unary = node.Parent as UnaryOperatorExpression;
112
if (unary != null && unary.Expression == node) {
113
modified = unary.Operator == UnaryOperatorType.Decrement ||
114
unary.Operator == UnaryOperatorType.PostDecrement ||
115
unary.Operator == UnaryOperatorType.Increment ||
116
unary.Operator == UnaryOperatorType.PostIncrement;
120
var assignment = node.Parent as AssignmentExpression;
121
modified = assignment != null && assignment.Left == node;
125
AddIssue (controlVariable.NameToken,
126
ctx.TranslateString ("'for' loop control variable is never modified"));