Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
<Compile Include="Rules\HasNoUnusedUsingsRuleTests.cs" />
<Compile Include="Rules\NewLineAboveRuleTests.cs" />
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Xunit;

namespace Microsoft.DotNet.CodeFormatting.Tests
{
public sealed class HasNoUnusedUsingsRuleTests : LocalSemanticRuleTestBase
{
internal override ILocalSemanticFormattingRule Rule
{
get { return new Rules.HasNoUnusedUsingsRule(); }
}

[Fact]
public void SimpleMove()
{
var source = @"
using System;
namespace NS1
{
class C1 { }
}";

var expected = @"
namespace NS1
{
class C1 { }
}";

Verify(source, expected);
}

[Fact]
public void RemoveUnusedUsingWithLeadingComment()
{
var source = @"
// copyright

using System;
namespace NS2
{
class C1 { }
}";

var expected = @"
// copyright

namespace NS2
{
class C1 { }
}";

Verify(source, expected);
}

[Fact]
public void RemoveUnusedUsingsWithBeforeAfterComments()
{
var source = @"
// before
using System;
// after
namespace NS2
{
class C1 { }
}";

var expected = @"
// before
// after
namespace NS2
{
class C1 { }
}";

Verify(source, expected);
}

/// <summary>
/// In the case a using directive is inside of a #if directive there is no
/// way to safely remove the using.
/// </summary>
[Fact]
public void KeepUnusedUsingsInDirectives()
{
var source = @"
#if false
using System;
#else
using System;
#endif

namespace N
{
class C { }
}";

Verify(source, source);
}

[Fact]
public void KeepUsedUsingDirective()
{
var source = @"
using System;

namespace N
{
class C
{
public void Method()
{
Console.WriteLine(""Hello, world!"");
}
}
}
";
Verify(source, source);
}

[Fact]
public void KeepSomeUsingDirectives()
{
var source = @"
using System;
using System.Diagnostics;
using System.Collections.Generic;

namespace N
{
class C
{
public void Method()
{
Console.WriteLine(new List<string>());
}
}
}
";
var expected = @"
using System;
using System.Collections.Generic;

namespace N
{
class C
{
public void Method()
{
Console.WriteLine(new List<string>());
}
}
}
";
Verify(source, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="ResponseFileWorkspace.cs" />
<Compile Include="Rules\MarkReadonlyFieldsRule.cs" />
<Compile Include="SemaphoreLock.cs" />
<Compile Include="Rules\HasNoUnusedUsingsRule.cs" />
<Compile Include="SyntaxUtil.cs" />
<Compile Include="Filters\FilenameFilter.cs" />
<Compile Include="Filters\UsableFileFilter.cs" />
Expand Down
49 changes: 49 additions & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/HasNoUnusedUsingsRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;

namespace Microsoft.DotNet.CodeFormatting.Rules
{
[LocalSemanticRule(Name, Description, LocalSemanticRuleOrder.HasNoUnusedUsingsRule)]
internal sealed class HasNoUnusedUsingsRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
{
internal const string Name = "HasNoUnusedUsings";
internal const string Description = "Removed unused using declarations.";
private const string UnusedUsingDiagnosticId = "CS8019";

public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken)
{
var root = syntaxRoot as CompilationUnitSyntax;
if (root == null)
return syntaxRoot;

if (root.DescendantTrivia().Any(x => x.Kind() == SyntaxKind.IfDirectiveTrivia))
return syntaxRoot;

var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var diagnostics = semanticModel.GetDiagnostics(null, cancellationToken);

var unusedUsingDiagnostics = diagnostics
.Where(x => x.Id == UnusedUsingDiagnosticId)
.Select(x => x.Location.SourceSpan.Start)
.ToList()
.AsReadOnly();

if (unusedUsingDiagnostics.Count == 0)
return syntaxRoot;

var unusedUsings = root.Usings
.Where(x => unusedUsingDiagnostics.Any(unusedUsing => x.Span.IntersectsWith(unusedUsing)))
.ToList()
.AsReadOnly();

return syntaxRoot.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepLeadingTrivia);
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal static class LocalSemanticRuleOrder
public const int IsFormattedFormattingRule = 3;
public const int RemoveExplicitThisRule = 4;
public const int AssertArgumentOrderRule = 5;
public const int HasNoUnusedUsingsRule = 6;
}

// Please keep these values sorted by number, not rule name.
Expand Down