2
// RedundantThisInspector.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.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
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;
36
namespace ICSharpCode.NRefactory.CSharp.Refactoring
39
/// Finds redundant namespace usages.
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
48
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
50
return new GatherVisitor(context, this).GetIssues();
53
class GatherVisitor : GatherVisitorBase
55
readonly RedundantThisIssue inspector;
57
public GatherVisitor (BaseRefactoringContext ctx, RedundantThisIssue inspector) : base (ctx)
59
this.inspector = inspector;
62
static IMember GetMember (ResolveResult result)
64
if (result is MemberResolveResult) {
65
return ((MemberResolveResult)result).Member;
66
} else if (result is MethodGroupResolveResult) {
67
return ((MethodGroupResolveResult)result).Methods.FirstOrDefault ();
73
public override void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression)
75
base.VisitThisReferenceExpression(thisReferenceExpression);
76
var memberReference = thisReferenceExpression.Parent as MemberReferenceExpression;
77
if (memberReference == null) {
81
var state = ctx.GetResolverStateAfter(thisReferenceExpression);
82
var wholeResult = ctx.Resolve(memberReference);
84
IMember member = GetMember(wholeResult);
89
var result = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList<IType>.Instance, NameLookupMode.Expression);
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));
101
AddIssue(thisReferenceExpression.StartLocation, memberReference.MemberNameToken.StartLocation, ctx.TranslateString("Remove redundant 'this.'"), script => {
102
script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
b'\\ No newline at end of file'