2
// RedundantNullCheckIssue.cs
5
// Ji Kun <jikun.nus@gmail.com>
7
// Copyright (c) 2013 Ji Kun
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
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;
32
using ICSharpCode.NRefactory.TypeSystem.Implementation;
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
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
44
private static readonly Pattern pattern1
46
// x is Record && x!= null
47
new BinaryOperatorExpression(
50
Expression = new AnyNode("a"),
51
Type = new AnyNode("t")
52
}, BinaryOperatorType.ConditionalAnd,
53
PatternHelper.CommutativeOperator(new Backreference("a"),
56
new NullReferenceExpression
60
private static readonly Pattern pattern2
62
// x != null && x is Record
63
new BinaryOperatorExpression (
64
PatternHelper.CommutativeOperator (new AnyNode("a"),
65
BinaryOperatorType.InEquality,
66
new NullReferenceExpression())
67
, BinaryOperatorType.ConditionalAnd,
69
Expression = new Backreference("a"),
70
Type = new AnyNode("t")
75
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
77
return new GatherVisitor(context).GetIssues();
80
class GatherVisitor : GatherVisitorBase<RedundantNullCheckIssue>
82
public GatherVisitor(BaseRefactoringContext ctx)
87
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
89
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
90
Match m1 = pattern1.Match(binaryOperatorExpression);
92
AddIssue(binaryOperatorExpression, ctx.TranslateString("Remove redundant IsNULL check"), script => {
93
Expression expr = binaryOperatorExpression.Left;
94
script.Replace(binaryOperatorExpression, expr);
99
Match m2 = pattern2.Match(binaryOperatorExpression);
101
AddIssue(binaryOperatorExpression, ctx.TranslateString("Remove redundant IsNULL check"), script => {
102
Expression expr = binaryOperatorExpression.Right;
103
script.Replace(binaryOperatorExpression, expr);