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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantTypeCastIssue.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
// RedundantTypeCastIssue.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.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.Collections.Generic;
 
28
using System.Linq;
 
29
using ICSharpCode.NRefactory.Semantics;
 
30
using ICSharpCode.NRefactory.TypeSystem;
 
31
using ICSharpCode.NRefactory.CSharp.Resolver;
 
32
 
 
33
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
34
{
 
35
        [IssueDescription ("Redundant type cast",
 
36
                                                Description = "Redundant type cast.",
 
37
                                                Category = IssueCategories.Redundancies,
 
38
                                                Severity = Severity.Warning,
 
39
                                                IssueMarker = IssueMarker.GrayOut,
 
40
                        ResharperDisableKeyword = "RedundantCast")]
 
41
        public class RedundantTypeCastIssue : ICodeIssueProvider
 
42
        {
 
43
                public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
 
44
                {
 
45
                        return new GatherVisitor (context).GetIssues ();
 
46
                }
 
47
 
 
48
                class GatherVisitor : GatherVisitorBase<RedundantTypeCastIssue>
 
49
                {
 
50
                        public GatherVisitor (BaseRefactoringContext ctx)
 
51
                                : base (ctx)
 
52
                        {
 
53
                        }
 
54
 
 
55
                        public override void VisitCastExpression (CastExpression castExpression)
 
56
                        {
 
57
                                base.VisitCastExpression (castExpression);
 
58
 
 
59
                                CheckTypeCast (castExpression, castExpression.Expression, castExpression.StartLocation, 
 
60
                                        castExpression.Expression.StartLocation);
 
61
                        }
 
62
 
 
63
                        public override void VisitAsExpression (AsExpression asExpression)
 
64
                        {
 
65
                                base.VisitAsExpression (asExpression);
 
66
 
 
67
                                CheckTypeCast (asExpression, asExpression.Expression, asExpression.Expression.EndLocation,
 
68
                                        asExpression.EndLocation);
 
69
                        }
 
70
 
 
71
                        IType GetExpectedType (Expression typeCastNode)
 
72
                        {
 
73
                                var memberRefExpr = typeCastNode.Parent as MemberReferenceExpression;
 
74
                                if (memberRefExpr != null) {
 
75
                                        var invocationExpr = memberRefExpr.Parent as InvocationExpression;
 
76
                                        if (invocationExpr != null && invocationExpr.Target == memberRefExpr) {
 
77
                                                var invocationResolveResult = ctx.Resolve (invocationExpr) as InvocationResolveResult;
 
78
                                                if (invocationResolveResult != null) {
 
79
                                                        return invocationResolveResult.Member.DeclaringType;
 
80
                                                }
 
81
                                        } else {
 
82
                                                var memberResolveResult = ctx.Resolve (memberRefExpr) as MemberResolveResult;
 
83
                                                if (memberResolveResult != null) {
 
84
                                                        return memberResolveResult.Member.DeclaringType;
 
85
                                                }
 
86
                                        }
 
87
                                }
 
88
                                return ctx.GetExpectedType (typeCastNode);
 
89
                        }
 
90
 
 
91
                        bool IsExplicitImplementation(IType exprType, IType interfaceType, Expression typeCastNode)
 
92
                        {
 
93
                                var memberRefExpr = typeCastNode.Parent as MemberReferenceExpression;
 
94
                                if (memberRefExpr != null) {
 
95
                                        var rr = ctx.Resolve(memberRefExpr);
 
96
                                        var memberResolveResult = rr as MemberResolveResult;
 
97
                                        if (memberResolveResult != null) {
 
98
                                                foreach (var member in exprType.GetMembers (m => m.EntityType == memberResolveResult.Member.EntityType)) {
 
99
                                                        if (member.IsExplicitInterfaceImplementation && member.ImplementedInterfaceMembers.Contains (memberResolveResult.Member)) {
 
100
                                                                return true;
 
101
                                                        }
 
102
                                                }
 
103
                                        }
 
104
 
 
105
                                        var methodGroupResolveResult = rr as MethodGroupResolveResult;
 
106
                                        if (methodGroupResolveResult != null) {
 
107
                                                foreach (var member in exprType.GetMethods ()) {
 
108
                                                        if (member.IsExplicitInterfaceImplementation && member.ImplementedInterfaceMembers.Any (m => methodGroupResolveResult.Methods.Contains ((IMethod)m))) {
 
109
                                                                return true;
 
110
                                                        }
 
111
                                                }
 
112
                                        }
 
113
                                }
 
114
                                return false;
 
115
                        }
 
116
 
 
117
                        void AddIssue (Expression typeCastNode, Expression expr, TextLocation start, TextLocation end)
 
118
                        {
 
119
                                AddIssue (start, end, ctx.TranslateString ("Remove redundant type cast"),
 
120
                                        script => script.Replace (typeCastNode, expr.Clone ()));
 
121
                        }
 
122
 
 
123
                        void CheckTypeCast (Expression typeCastNode, Expression expr, TextLocation castStart, TextLocation castEnd)
 
124
                        {
 
125
                                while (typeCastNode.Parent != null && typeCastNode.Parent is ParenthesizedExpression)
 
126
                                        typeCastNode = (Expression)typeCastNode.Parent;
 
127
                                var expectedType = GetExpectedType (typeCastNode);
 
128
                                var exprType = ctx.Resolve (expr).Type;
 
129
                                if (expectedType.Kind == TypeKind.Interface && IsExplicitImplementation (exprType, expectedType, typeCastNode))
 
130
                                        return;
 
131
                                if (exprType.GetAllBaseTypes ().Any (t => t.Equals(expectedType)))
 
132
                                        AddIssue (typeCastNode, expr, castStart, castEnd);
 
133
                        }
 
134
                }
 
135
        }
 
136
}