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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantNullCheckIssue.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
// RedundantNullCheckIssue.cs
 
3
//
 
4
// Author:
 
5
//         Ji Kun <jikun.nus@gmail.com>
 
6
//
 
7
// Copyright (c) 2013 Ji Kun
 
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.using System;
 
26
using System.Collections.Generic;
 
27
using ICSharpCode.NRefactory.PatternMatching;
 
28
using ICSharpCode.NRefactory.TypeSystem;
 
29
using ICSharpCode.NRefactory.CSharp.Resolver;
 
30
using ICSharpCode.NRefactory.Semantics;
 
31
using System.Linq;
 
32
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
33
 
 
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
35
{
 
36
        [IssueDescription("Redundant null check",
 
37
                                          Description = "When 'is' keyword is used, which implicitly check null.",
 
38
                                          Category = IssueCategories.Redundancies,
 
39
                                          Severity = Severity.Suggestion,
 
40
                          ResharperDisableKeyword = "RedundantNullCheck",
 
41
                                          IssueMarker = IssueMarker.GrayOut)]
 
42
        public class RedundantNullCheckIssue : ICodeIssueProvider
 
43
        {
 
44
                private static readonly Pattern pattern1
 
45
                        = new Choice {
 
46
                        //  x is Record && x!= null      
 
47
                        new BinaryOperatorExpression(
 
48
                                new IsExpression
 
49
                                        {
 
50
                                                Expression = new AnyNode("a"),
 
51
                                                Type = new AnyNode("t")
 
52
                                        }, BinaryOperatorType.ConditionalAnd,
 
53
                                PatternHelper.CommutativeOperator(new Backreference("a"),
 
54
                                                                                                        BinaryOperatorType.
 
55
                                                                                                                InEquality,
 
56
                                                                                                        new NullReferenceExpression
 
57
                                                                                                                ())
 
58
                                )
 
59
                };
 
60
                private static readonly Pattern pattern2
 
61
                   = new Choice {
 
62
                        //  x != null && x is Record
 
63
                        new BinaryOperatorExpression (
 
64
                                PatternHelper.CommutativeOperator (new AnyNode("a"), 
 
65
                                                                                                                BinaryOperatorType.InEquality, 
 
66
                                                                                                        new NullReferenceExpression())                  
 
67
                                , BinaryOperatorType.ConditionalAnd,
 
68
                                new IsExpression {
 
69
                                        Expression = new Backreference("a"),
 
70
                                        Type = new AnyNode("t")
 
71
                                }
 
72
                        )       
 
73
                };
 
74
 
 
75
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
76
                {
 
77
                        return new GatherVisitor(context).GetIssues();
 
78
                }
 
79
 
 
80
                class GatherVisitor : GatherVisitorBase<RedundantNullCheckIssue>
 
81
                {
 
82
                        public GatherVisitor(BaseRefactoringContext ctx)
 
83
                                : base(ctx)
 
84
                        {
 
85
                        }
 
86
 
 
87
                        public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
 
88
                        {
 
89
                                base.VisitBinaryOperatorExpression(binaryOperatorExpression);
 
90
                                Match m1 = pattern1.Match(binaryOperatorExpression);
 
91
                                if (m1.Success) {
 
92
                                        AddIssue(binaryOperatorExpression, ctx.TranslateString("Remove redundant IsNULL check"), script => {
 
93
                                                Expression expr = binaryOperatorExpression.Left;
 
94
                                                script.Replace(binaryOperatorExpression, expr);
 
95
                                        });
 
96
                                        return;
 
97
                                }
 
98
 
 
99
                                Match m2 = pattern2.Match(binaryOperatorExpression);
 
100
                                if (m2.Success) {
 
101
                                        AddIssue(binaryOperatorExpression, ctx.TranslateString("Remove redundant IsNULL check"), script => {
 
102
                                                Expression expr = binaryOperatorExpression.Right;
 
103
                                                script.Replace(binaryOperatorExpression, expr);
 
104
                                        });
 
105
                                        return;
 
106
                                }
 
107
                        }
 
108
                }
 
109
        }
 
110
}
 
111