~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ForControlVariableNotModifiedIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ForControlVariableNotModifiedIssue.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
using ICSharpCode.NRefactory.PatternMatching;
 
30
using ICSharpCode.NRefactory.Semantics;
 
31
 
 
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
33
{
 
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
 
40
        {
 
41
                public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
 
42
                {
 
43
                        return new GatherVisitor (context).GetIssues ();
 
44
                }
 
45
 
 
46
                class GatherVisitor : GatherVisitorBase<ForControlVariableNotModifiedIssue>
 
47
                {
 
48
                        public GatherVisitor (BaseRefactoringContext ctx)
 
49
                                : base (ctx)
 
50
                        {
 
51
                        }
 
52
 
 
53
                        static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl, 
 
54
                                                                                                                  BinaryOperatorExpression condition)
 
55
                        {
 
56
                                var controlVariables = variableDecl.Variables.Where (
 
57
                                        v =>
 
58
                                        {
 
59
                                                var identifier = new IdentifierExpression (v.Name);
 
60
                                                return condition.Left.Match (identifier).Success ||
 
61
                                                        condition.Right.Match (identifier).Success;
 
62
                                        }).ToList ();
 
63
                                return controlVariables.Count == 1 ? controlVariables [0] : null;
 
64
                        }
 
65
 
 
66
                        static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl,
 
67
                                                                                                                  UnaryOperatorExpression condition)
 
68
                        {
 
69
                                var controlVariables = variableDecl.Variables.Where (
 
70
                                        v =>
 
71
                                        {
 
72
                                                var identifier = new IdentifierExpression (v.Name);
 
73
                                                return condition.Expression.Match (identifier).Success;
 
74
                                        }).ToList ();
 
75
                                return controlVariables.Count == 1 ? controlVariables [0] : null;
 
76
                        }
 
77
 
 
78
                        public override void VisitForStatement (ForStatement forStatement)
 
79
                        {
 
80
                                base.VisitForStatement (forStatement);
 
81
 
 
82
                                if (forStatement.Initializers.Count != 1)
 
83
                                        return;
 
84
                                var variableDecl = forStatement.Initializers.First () as VariableDeclarationStatement;
 
85
                                if (variableDecl == null)
 
86
                                        return;
 
87
 
 
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);
 
96
                                }
 
97
 
 
98
                                if (controlVariable == null)
 
99
                                        return;
 
100
 
 
101
                                var localResolveResult = ctx.Resolve (controlVariable) as LocalResolveResult;
 
102
                                if (localResolveResult == null)
 
103
                                        return;
 
104
 
 
105
                                var results = ctx.FindReferences (forStatement, localResolveResult.Variable);
 
106
                                var modified = false;
 
107
                                foreach (var result in results) {
 
108
                                        if (modified)
 
109
                                                break;
 
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;
 
117
                                                continue;
 
118
                                        }
 
119
 
 
120
                                        var assignment = node.Parent as AssignmentExpression;
 
121
                                        modified = assignment != null && assignment.Left == node;
 
122
                                }
 
123
 
 
124
                                if (!modified)
 
125
                                        AddIssue (controlVariable.NameToken,
 
126
                                                ctx.TranslateString ("'for' loop control variable is never modified"));
 
127
 
 
128
                        }
 
129
                }
 
130
        }
 
131
}