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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantThisIssue.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
 
// RedundantThisInspector.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2012 Xamarin <http://xamarin.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;
28
 
using ICSharpCode.NRefactory.PatternMatching;
29
 
using System.Collections.Generic;
30
 
using ICSharpCode.NRefactory.TypeSystem;
31
 
using ICSharpCode.NRefactory.Semantics;
32
 
using ICSharpCode.NRefactory.CSharp.Resolver;
33
 
using System.Linq;
34
 
 
35
 
 
36
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
37
 
{
38
 
        /// <summary>
39
 
        /// Finds redundant namespace usages.
40
 
        /// </summary>
41
 
        [IssueDescription("Remove redundant 'this.'",
42
 
               Description= "Removes 'this.' references that are not required.",
43
 
               Category = IssueCategories.Redundancies,
44
 
               Severity = Severity.Hint,
45
 
               IssueMarker = IssueMarker.GrayOut)]
46
 
        public class RedundantThisIssue : ICodeIssueProvider
47
 
        {
48
 
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
49
 
                {
50
 
                        return new GatherVisitor(context, this).GetIssues();
51
 
                }
52
 
 
53
 
                class GatherVisitor : GatherVisitorBase
54
 
                {
55
 
                        readonly RedundantThisIssue inspector;
56
 
                        
57
 
                        public GatherVisitor (BaseRefactoringContext ctx, RedundantThisIssue inspector) : base (ctx)
58
 
                        {
59
 
                                this.inspector = inspector;
60
 
                        }
61
 
 
62
 
                        static IMember GetMember (ResolveResult result)
63
 
                        {
64
 
                                if (result is MemberResolveResult) {
65
 
                                        return ((MemberResolveResult)result).Member;
66
 
                                } else if (result is MethodGroupResolveResult) {
67
 
                                        return ((MethodGroupResolveResult)result).Methods.FirstOrDefault ();
68
 
                                }
69
 
 
70
 
                                return null;
71
 
                        }
72
 
 
73
 
                        public override void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression)
74
 
                        {
75
 
                                base.VisitThisReferenceExpression(thisReferenceExpression);
76
 
                                var memberReference = thisReferenceExpression.Parent as MemberReferenceExpression;
77
 
                                if (memberReference == null) {
78
 
                                        return;
79
 
                                }
80
 
 
81
 
                                var state = ctx.GetResolverStateAfter(thisReferenceExpression);
82
 
                                var wholeResult = ctx.Resolve(memberReference);
83
 
                        
84
 
                                IMember member = GetMember(wholeResult);
85
 
                                if (member == null) { 
86
 
                                        return;
87
 
                                }
88
 
 
89
 
                                var result = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList<IType>.Instance, NameLookupMode.Expression);
90
 
                        
91
 
                                bool isRedundant;
92
 
                                if (result is MemberResolveResult) {
93
 
                                        isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region);
94
 
                                } else if (result is MethodGroupResolveResult) {
95
 
                                        isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region));
96
 
                                } else {
97
 
                                        return;
98
 
                                }
99
 
 
100
 
                                if (isRedundant) {
101
 
                                        AddIssue(thisReferenceExpression.StartLocation, memberReference.MemberNameToken.StartLocation, ctx.TranslateString("Remove redundant 'this.'"), script => {
102
 
                                                script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
103
 
                                        }
104
 
                                        );
105
 
                                }
106
 
                        }
107
 
                }
108
 
        }
109
 
}
 
 
b'\\ No newline at end of file'