2
// RedundantUsingInspector.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;
35
namespace ICSharpCode.NRefactory.CSharp.Refactoring
38
/// Finds redundant using declarations.
40
[IssueDescription("Remove unused usings",
41
Description = "Removes used declarations that are not required.",
42
Category = IssueCategories.Redundancies,
43
Severity = Severity.Hint,
44
IssueMarker = IssueMarker.GrayOut,
45
ResharperDisableKeyword = "RedundantUsingDirective"
47
public class RedundantUsingIssue : ICodeIssueProvider
49
List<string> namespacesToKeep = new List<string>();
52
/// The list of namespaces that should be kept even if they are not being used.
53
/// Used in SharpDevelop to always keep the "System" namespace around.
55
public IList<string> NamespacesToKeep {
56
get { return namespacesToKeep; }
59
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
61
var visitor = new GatherVisitor (context, this);
62
context.RootNode.AcceptVisitor (visitor);
64
return visitor.FoundIssues;
67
class GatherVisitor : GatherVisitorBase<RedundantUsingIssue>
69
Dictionary<UsingDeclaration, bool> isInUse = new Dictionary<UsingDeclaration, bool>();
70
Dictionary<string, UsingDeclaration> namespaceToUsingDecl = new Dictionary<string, UsingDeclaration>();
72
public GatherVisitor (BaseRefactoringContext ctx, RedundantUsingIssue issueProvider) : base (ctx, issueProvider)
78
foreach (var u in isInUse.Where (u => !u.Value)) {
80
AddIssue(decl, ctx.TranslateString("Remove redundant usings"), script => {
81
foreach (var u2 in isInUse.Where (a => !a.Value)) {
82
script.Remove (u2.Key);
89
public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration)
91
base.VisitUsingDeclaration(usingDeclaration);
92
if (IsSuppressed(usingDeclaration.StartLocation))
94
var nrr = ctx.Resolve(usingDeclaration.Import) as NamespaceResolveResult;
96
isInUse[usingDeclaration] = IssueProvider.namespacesToKeep.Contains(nrr.NamespaceName);
97
namespaceToUsingDecl[nrr.NamespaceName] = usingDeclaration;
101
public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration)
103
var oldNamespaceToUsingDecl = new Dictionary<string, UsingDeclaration>(namespaceToUsingDecl);
104
base.VisitNamespaceDeclaration(namespaceDeclaration);
105
namespaceToUsingDecl = oldNamespaceToUsingDecl;
108
void UseNamespace(string ns)
110
UsingDeclaration decl;
111
if (namespaceToUsingDecl.TryGetValue(ns, out decl)) {
112
isInUse[decl] = true;
116
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
118
base.VisitIdentifierExpression(identifierExpression);
119
var trr = ctx.Resolve(identifierExpression) as TypeResolveResult;
121
UseNamespace(trr.Type.Namespace);
125
public override void VisitSimpleType(SimpleType simpleType)
127
base.VisitSimpleType(simpleType);
128
UseNamespace(ctx.Resolve(simpleType).Type.Namespace);
131
public override void VisitInvocationExpression (InvocationExpression invocationExpression)
133
base.VisitInvocationExpression (invocationExpression);
134
UseExtensionMethod(ctx.Resolve(invocationExpression));
137
void UseExtensionMethod(ResolveResult rr)
139
var mg = rr as CSharpInvocationResolveResult;
140
if (mg != null && mg.IsExtensionMethodInvocation) {
141
UseNamespace (mg.Member.DeclaringType.Namespace);
145
public override void VisitQueryExpression(QueryExpression queryExpression)
147
base.VisitQueryExpression(queryExpression);
148
foreach (var clause in queryExpression.Clauses) {
149
UseExtensionMethod(ctx.Resolve(clause));