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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ConstantConditionIssue.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
// ConstantConditionIssue.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
 
 
29
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
30
{
 
31
        [IssueDescription ("Condition is always 'true' or always 'false'",
 
32
                                           Description = "Condition is always 'true' or always 'false'.",
 
33
                                           Category = IssueCategories.CodeQualityIssues,
 
34
                                           Severity = Severity.Warning,
 
35
                                           IssueMarker = IssueMarker.Underline)]
 
36
        public class ConstantConditionIssue : ICodeIssueProvider
 
37
        {
 
38
                public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
 
39
                {
 
40
                        return new GatherVisitor (context).GetIssues ();
 
41
                }
 
42
 
 
43
                class GatherVisitor : GatherVisitorBase<ConstantConditionIssue>
 
44
                {
 
45
                        public GatherVisitor (BaseRefactoringContext ctx)
 
46
                                : base (ctx)
 
47
                        {
 
48
                        }
 
49
 
 
50
                        public override void VisitConditionalExpression (ConditionalExpression conditionalExpression)
 
51
                        {
 
52
                                base.VisitConditionalExpression (conditionalExpression);
 
53
 
 
54
                                CheckCondition (conditionalExpression.Condition);
 
55
                        }
 
56
 
 
57
                        public override void VisitIfElseStatement (IfElseStatement ifElseStatement)
 
58
                        {
 
59
                                base.VisitIfElseStatement (ifElseStatement);
 
60
 
 
61
                                CheckCondition (ifElseStatement.Condition);
 
62
                        }
 
63
 
 
64
                        public override void VisitWhileStatement (WhileStatement whileStatement)
 
65
                        {
 
66
                                base.VisitWhileStatement (whileStatement);
 
67
 
 
68
                                CheckCondition (whileStatement.Condition);
 
69
                        }
 
70
 
 
71
                        public override void VisitDoWhileStatement (DoWhileStatement doWhileStatement)
 
72
                        {
 
73
                                base.VisitDoWhileStatement (doWhileStatement);
 
74
 
 
75
                                CheckCondition (doWhileStatement.Condition);
 
76
                        }
 
77
 
 
78
                        public override void VisitForStatement (ForStatement forStatement)
 
79
                        {
 
80
                                base.VisitForStatement (forStatement);
 
81
 
 
82
                                CheckCondition (forStatement.Condition);
 
83
                        }
 
84
 
 
85
                        void CheckCondition (Expression condition)
 
86
                        {
 
87
                                if (condition is PrimitiveExpression)
 
88
                                        return;
 
89
 
 
90
                                var resolveResult = ctx.Resolve (condition);
 
91
                                if (!(resolveResult.IsCompileTimeConstant && resolveResult.ConstantValue is bool))
 
92
                                        return;
 
93
 
 
94
                                var value = (bool)resolveResult.ConstantValue;
 
95
                                var conditionalExpr = condition.Parent as ConditionalExpression;
 
96
                                var ifElseStatement = condition.Parent as IfElseStatement;
 
97
                                var valueStr = value.ToString ().ToLower ();
 
98
 
 
99
                                CodeAction action;
 
100
                                if (conditionalExpr != null) {
 
101
                                        var replaceExpr = value ? conditionalExpr.TrueExpression : conditionalExpr.FalseExpression;
 
102
                                        action = new CodeAction (
 
103
                                                string.Format (ctx.TranslateString ("Replace '?:' with '{0}' branch"), valueStr),
 
104
                                                script => script.Replace (conditionalExpr, replaceExpr.Clone ()),
 
105
                                                condition);
 
106
                                } else if (ifElseStatement != null) {
 
107
                                        action = new CodeAction (
 
108
                                                string.Format (ctx.TranslateString ("Replace 'if' with '{0}' branch"), valueStr),
 
109
                                                script => {
 
110
                                                        var statement = value ? ifElseStatement.TrueStatement : ifElseStatement.FalseStatement;
 
111
                                                        var blockStatement = statement as BlockStatement;
 
112
                                                        if (statement.IsNull || (blockStatement != null && blockStatement.Statements.Count == 0)) {
 
113
                                                                script.Remove (ifElseStatement);
 
114
                                                                return;
 
115
                                                        }
 
116
 
 
117
                                                        TextLocation start, end;
 
118
                                                        if (blockStatement != null) {
 
119
                                                                start = blockStatement.Statements.FirstOrNullObject ().StartLocation;
 
120
                                                                end = blockStatement.Statements.LastOrNullObject ().EndLocation;
 
121
                                                        } else {
 
122
                                                                start = statement.StartLocation;
 
123
                                                                end = statement.EndLocation;
 
124
                                                        }
 
125
                                                        RemoveText (script, ifElseStatement.StartLocation, start);
 
126
                                                        RemoveText (script, end, ifElseStatement.EndLocation);
 
127
                                                        script.FormatText (ifElseStatement.Parent);
 
128
                                                }, condition);
 
129
                                } else {
 
130
                                        action = new CodeAction (
 
131
                                                string.Format (ctx.TranslateString ("Replace expression with '{0}'"), valueStr),
 
132
                                                script => script.Replace (condition, new PrimitiveExpression (value)), 
 
133
                                                condition
 
134
                                        );
 
135
                                }
 
136
                                AddIssue (condition, string.Format (ctx.TranslateString ("Condition is always '{0}'"), valueStr), 
 
137
                                        new [] { action });
 
138
                        }
 
139
 
 
140
                        void RemoveText (Script script, TextLocation start, TextLocation end)
 
141
                        {
 
142
                                var startOffset = script.GetCurrentOffset (start);
 
143
                                var endOffset = script.GetCurrentOffset (end);
 
144
                                if (startOffset < endOffset)
 
145
                                        script.RemoveText (startOffset, endOffset - startOffset);
 
146
                        }
 
147
                }
 
148
        }
 
149
}