void AnalyzeThisReferenceExpression(ThisReferenceExpression thisReferenceExpression)
            {
                var memberReference = thisReferenceExpression.Parent as MemberReferenceExpression;

                if (memberReference == null)
                {
                    return;
                }

                var state       = ctx.GetResolverStateAfter(thisReferenceExpression);
                var wholeResult = ctx.Resolve(memberReference);

                IMember member = GetMember(wholeResult);

                if (member == null)
                {
                    return;
                }

                if (declarationsSpaceVisitor.GetDeclarationSpace(thisReferenceExpression).IsNameUsed(member.Name))
                {
                    return;
                }

                var result       = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList <IType> .Instance, NameLookupMode.Expression);
                var parentResult = ctx.Resolve(memberReference.Parent) as CSharpInvocationResolveResult;

                bool isRedundant;

                if (result is MemberResolveResult)
                {
                    isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region);
                }
                else if (parentResult != null && parentResult.IsExtensionMethodInvocation)
                {
                    // 'this.' is required for extension method invocation
                    isRedundant = false;
                }
                else if (result is MethodGroupResolveResult)
                {
                    isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region));
                }
                else
                {
                    return;
                }

                if (isRedundant)
                {
                    var issueDescription  = ctx.TranslateString("'this.' is redundant and can be removed safely.");
                    var actionDescription = ctx.TranslateString("Remove 'this.'");
                    AddIssue(new CodeIssue(thisReferenceExpression.StartLocation, memberReference.MemberNameToken.StartLocation, issueDescription, actionDescription, script => {
                        script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
                    })
                    {
                        IssueMarker = IssueMarker.GrayOut
                    });
                }
            }
Exemplo n.º 2
0
 public override void VisitMemberType(MemberType memberType)
 {
     base.VisitMemberType(memberType);
     HandleMemberReference(
         memberType, memberType.Target, memberType.MemberNameToken, memberType.TypeArguments, SimpleNameLookupMode.Type,
         script => {
         script.Replace(memberType, RefactoringAstHelper.RemoveTarget(memberType));
     });
 }
Exemplo n.º 3
0
 public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression)
 {
     base.VisitMemberReferenceExpression(memberReferenceExpression);
     HandleMemberReference(
         memberReferenceExpression, memberReferenceExpression.Target, memberReferenceExpression.MemberNameToken, memberReferenceExpression.TypeArguments, SimpleNameLookupMode.Expression,
         script => {
         script.Replace(memberReferenceExpression, RefactoringAstHelper.RemoveTarget(memberReferenceExpression));
     });
 }
            public override void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression)
            {
                base.VisitThisReferenceExpression(thisReferenceExpression);
                var memberReference = thisReferenceExpression.Parent as MemberReferenceExpression;

                if (memberReference == null)
                {
                    return;
                }

                var state       = ctx.GetResolverStateAfter(thisReferenceExpression);
                var wholeResult = ctx.Resolve(memberReference);

                IMember member = GetMember(wholeResult);

                if (member == null)
                {
                    return;
                }

                var result = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList <IType> .Instance, NameLookupMode.Expression);

                bool isRedundant;

                if (result is MemberResolveResult)
                {
                    isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region);
                }
                else if (result is MethodGroupResolveResult)
                {
                    isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region));
                }
                else
                {
                    return;
                }

                if (isRedundant)
                {
                    AddIssue(thisReferenceExpression.StartLocation, memberReference.MemberNameToken.StartLocation, ctx.TranslateString("Remove redundant 'this.'"), script =>
                    {
                        script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
                    }
                             );
                }
            }
            void AnalyzeThisReferenceExpression(BaseReferenceExpression baseReferenceExpression)
            {
                var memberReference = baseReferenceExpression.Parent as MemberReferenceExpression;

                if (memberReference == null)
                {
                    return;
                }

                var state       = ctx.GetResolverStateAfter(baseReferenceExpression);
                var wholeResult = ctx.Resolve(memberReference);

                IMember member = GetMember(wholeResult);

                if (member == null)
                {
                    return;
                }

                var localDeclarationSpace = declarationsSpaceVisitor.GetDeclarationSpace(baseReferenceExpression);

                if (localDeclarationSpace == null || localDeclarationSpace.IsNameUsed(member.Name))
                {
                    return;
                }

                var result       = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList <IType> .Instance, NameLookupMode.Expression);
                var parentResult = ctx.Resolve(memberReference.Parent) as CSharpInvocationResolveResult;

                bool isRedundant;

                if (result is MemberResolveResult)
                {
                    isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region);
                }
                else if (parentResult != null && parentResult.IsExtensionMethodInvocation)
                {
                    // 'this.' is required for extension method invocation
                    isRedundant = false;
                }
                else if (result is MethodGroupResolveResult)
                {
                    isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region));
                }
                else
                {
                    return;
                }

                var basicMembers    = state.CurrentTypeDefinition.DirectBaseTypes.First().GetMembers();
                var extendedMembers = state.CurrentTypeDefinition.GetMembers().Except(basicMembers);

                if (extendedMembers.Any(f => f.Name.Equals(member.Name)))
                {
                    return;
                }

                if (isRedundant)
                {
                    AddIssue(new CodeIssue(
                                 baseReferenceExpression.StartLocation,
                                 memberReference.MemberNameToken.StartLocation,
                                 ctx.TranslateString("'base.' is redundant and can be removed safely."),
                                 ctx.TranslateString("Remove 'base.'"),
                                 script => {
                        script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
                    }
                                 )
                    {
                        IssueMarker = IssueMarker.GrayOut
                    });
                }
            }