2
// RedundantTypeCastIssue.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.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
27
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.Semantics;
30
using ICSharpCode.NRefactory.TypeSystem;
31
using ICSharpCode.NRefactory.CSharp.Resolver;
33
namespace ICSharpCode.NRefactory.CSharp.Refactoring
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
43
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
45
return new GatherVisitor (context).GetIssues ();
48
class GatherVisitor : GatherVisitorBase<RedundantTypeCastIssue>
50
public GatherVisitor (BaseRefactoringContext ctx)
55
public override void VisitCastExpression (CastExpression castExpression)
57
base.VisitCastExpression (castExpression);
59
CheckTypeCast (castExpression, castExpression.Expression, castExpression.StartLocation,
60
castExpression.Expression.StartLocation);
63
public override void VisitAsExpression (AsExpression asExpression)
65
base.VisitAsExpression (asExpression);
67
CheckTypeCast (asExpression, asExpression.Expression, asExpression.Expression.EndLocation,
68
asExpression.EndLocation);
71
IType GetExpectedType (Expression typeCastNode)
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;
82
var memberResolveResult = ctx.Resolve (memberRefExpr) as MemberResolveResult;
83
if (memberResolveResult != null) {
84
return memberResolveResult.Member.DeclaringType;
88
return ctx.GetExpectedType (typeCastNode);
91
bool IsExplicitImplementation(IType exprType, IType interfaceType, Expression typeCastNode)
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)) {
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))) {
117
void AddIssue (Expression typeCastNode, Expression expr, TextLocation start, TextLocation end)
119
AddIssue (start, end, ctx.TranslateString ("Remove redundant type cast"),
120
script => script.Replace (typeCastNode, expr.Clone ()));
123
void CheckTypeCast (Expression typeCastNode, Expression expr, TextLocation castStart, TextLocation castEnd)
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))
131
if (exprType.GetAllBaseTypes ().Any (t => t.Equals(expectedType)))
132
AddIssue (typeCastNode, expr, castStart, castEnd);