2
// ConstantConditionIssue.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
namespace ICSharpCode.NRefactory.CSharp.Refactoring
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
38
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
40
return new GatherVisitor (context).GetIssues ();
43
class GatherVisitor : GatherVisitorBase<ConstantConditionIssue>
45
public GatherVisitor (BaseRefactoringContext ctx)
50
public override void VisitConditionalExpression (ConditionalExpression conditionalExpression)
52
base.VisitConditionalExpression (conditionalExpression);
54
CheckCondition (conditionalExpression.Condition);
57
public override void VisitIfElseStatement (IfElseStatement ifElseStatement)
59
base.VisitIfElseStatement (ifElseStatement);
61
CheckCondition (ifElseStatement.Condition);
64
public override void VisitWhileStatement (WhileStatement whileStatement)
66
base.VisitWhileStatement (whileStatement);
68
CheckCondition (whileStatement.Condition);
71
public override void VisitDoWhileStatement (DoWhileStatement doWhileStatement)
73
base.VisitDoWhileStatement (doWhileStatement);
75
CheckCondition (doWhileStatement.Condition);
78
public override void VisitForStatement (ForStatement forStatement)
80
base.VisitForStatement (forStatement);
82
CheckCondition (forStatement.Condition);
85
void CheckCondition (Expression condition)
87
if (condition is PrimitiveExpression)
90
var resolveResult = ctx.Resolve (condition);
91
if (!(resolveResult.IsCompileTimeConstant && resolveResult.ConstantValue is bool))
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 ();
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 ()),
106
} else if (ifElseStatement != null) {
107
action = new CodeAction (
108
string.Format (ctx.TranslateString ("Replace 'if' with '{0}' branch"), valueStr),
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);
117
TextLocation start, end;
118
if (blockStatement != null) {
119
start = blockStatement.Statements.FirstOrNullObject ().StartLocation;
120
end = blockStatement.Statements.LastOrNullObject ().EndLocation;
122
start = statement.StartLocation;
123
end = statement.EndLocation;
125
RemoveText (script, ifElseStatement.StartLocation, start);
126
RemoveText (script, end, ifElseStatement.EndLocation);
127
script.FormatText (ifElseStatement.Parent);
130
action = new CodeAction (
131
string.Format (ctx.TranslateString ("Replace expression with '{0}'"), valueStr),
132
script => script.Replace (condition, new PrimitiveExpression (value)),
136
AddIssue (condition, string.Format (ctx.TranslateString ("Condition is always '{0}'"), valueStr),
140
void RemoveText (Script script, TextLocation start, TextLocation end)
142
var startOffset = script.GetCurrentOffset (start);
143
var endOffset = script.GetCurrentOffset (end);
144
if (startOffset < endOffset)
145
script.RemoveText (startOffset, endOffset - startOffset);