2
// ParameterCanBeIEnumerableIssue.cs
5
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
7
// Copyright (c) 2013 Ciprian Khlud
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;
28
using ICSharpCode.NRefactory.TypeSystem;
29
using ICSharpCode.NRefactory.Semantics;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
35
[IssueDescription("A parameter can IEnumerable/ICollection/IList<T>",
36
Description = "Finds parameters that can be demoted to a generic list.",
37
Category = IssueCategories.Opportunities,
38
Severity = Severity.Suggestion
40
public class ParameterCanBeIEnumerableIssue : ICodeIssueProvider
42
readonly bool tryResolve;
44
public ParameterCanBeIEnumerableIssue() : this (true)
48
public ParameterCanBeIEnumerableIssue(bool tryResolve)
50
this.tryResolve = tryResolve;
53
#region ICodeIssueProvider implementation
54
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
56
var gatherer = new GatherVisitor(context, tryResolve);
57
var issues = gatherer.GetIssues();
62
class GatherVisitor : GatherVisitorBase<ParameterCanBeDemotedIssue>
66
public GatherVisitor(BaseRefactoringContext context, bool tryResolve) : base (context)
68
this.tryResolve = tryResolve;
71
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
73
methodDeclaration.Attributes.AcceptVisitor(this);
74
if (HasEntryPointSignature(methodDeclaration))
76
var eligibleParameters = methodDeclaration.Parameters
77
.Where(p => p.ParameterModifier != ParameterModifier.Out && p.ParameterModifier != ParameterModifier.Ref)
79
if (eligibleParameters.Count == 0)
81
var declarationResolveResult = ctx.Resolve(methodDeclaration) as MemberResolveResult;
82
if (declarationResolveResult == null)
84
var member = declarationResolveResult.Member;
85
if (member.IsOverride || member.IsOverridable || member.ImplementedInterfaceMembers.Any())
88
var collector = new TypeCriteriaCollector(ctx);
89
methodDeclaration.AcceptVisitor(collector);
91
foreach (var parameter in eligibleParameters) {
92
ProcessParameter(parameter, methodDeclaration.Body, collector);
96
bool HasEntryPointSignature(MethodDeclaration methodDeclaration)
98
if (!methodDeclaration.Modifiers.HasFlag(Modifiers.Static))
100
var returnType = ctx.Resolve(methodDeclaration.ReturnType).Type;
101
if (!returnType.IsKnownType(KnownTypeCode.Int32) && !returnType.IsKnownType(KnownTypeCode.Void))
103
var parameterCount = methodDeclaration.Parameters.Count;
104
if (parameterCount == 0)
106
if (parameterCount != 1)
108
var parameterType = ctx.Resolve(methodDeclaration.Parameters.First()).Type as ArrayType;
109
return parameterType != null && parameterType.ElementType.IsKnownType(KnownTypeCode.String);
112
void ProcessParameter(ParameterDeclaration parameter, AstNode rootResolutionNode, TypeCriteriaCollector collector)
114
var localResolveResult = ctx.Resolve(parameter) as LocalResolveResult;
115
if (localResolveResult == null)
117
var variable = localResolveResult.Variable;
118
var typeKind = variable.Type.Kind;
119
if (!(typeKind == TypeKind.Class ||
120
typeKind == TypeKind.Struct ||
121
typeKind == TypeKind.Interface ||
122
typeKind == TypeKind.Array) ||
123
!collector.UsedVariables.Contains(variable))
128
var candidateTypes = localResolveResult.Type.GetAllBaseTypes()
129
.Where(t => t.IsParameterized)
133
(from type in candidateTypes
134
where !tryResolve || ParameterCanBeDemotedIssue.TypeChangeResolvesCorrectly(ctx, parameter, rootResolutionNode, type)
135
select type).ToList();
136
if (!validTypes.Any()) return;
138
var foundType = validTypes.FirstOrDefault();
139
if (foundType == null)
142
AddIssue(parameter.NameToken, string.Format(ctx.TranslateString("Parameter can be {0}"),
144
script => script.Replace(parameter.Type, CreateShortType(ctx, foundType, parameter)));
147
public static AstType CreateShortType(BaseRefactoringContext refactoringContext, IType expressionType, AstNode node)
150
var csResolver = refactoringContext.Resolver.GetResolverStateBefore(node);
151
var builder = new TypeSystemAstBuilder(csResolver);
152
return builder.ConvertType(expressionType);