Skip to content

Commit

Permalink
Fix UsingCodeFixProvider handling of aliases
Browse files Browse the repository at this point in the history
This change can result in the output of this code fix containing duplicate
using directives, but this situation does not break the semantics of code
and only produces a build warning.

Fixes DotNetAnalyzers#1770
  • Loading branch information
sharwell committed Nov 29, 2015
1 parent 6b7ba3d commit fb4a829
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
.WithAdditionalAnnotations(UsingCodeFixAnnotation);

// filter duplicate using declarations, preferring to keep the one with an alias
var existingUsing = result.Find(u => string.Equals(u.Name.ToUnaliasedString(), processedUsing.Name.ToUnaliasedString(), StringComparison.Ordinal));
var existingUsing = result.Find(u => string.Equals(u.Name.ToNormalizedString(), processedUsing.Name.ToNormalizedString(), StringComparison.Ordinal));
if (existingUsing != null)
{
if (!existingUsing.HasNamespaceAliasQualifier() && processedUsing.HasNamespaceAliasQualifier())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace Food
using global::System;
using global::System.IO;
using global::System.Linq;
using System;
using System.Threading;
using XYZ = System.IO;
}";
Expand Down Expand Up @@ -135,10 +136,12 @@ namespace Food

var fixedTestCode = @"namespace Food
{
using Food;
using global::Food;
using global::System;
using global::System.IO;
using global::System.Linq;
using System;
using System.Threading;
}";

Expand Down Expand Up @@ -225,6 +228,13 @@ protected override string GetSettings()
return CombinedUsingDirectivesTestSettings;
}

/// <inheritdoc/>
protected override IEnumerable<string> GetDisabledDiagnostics()
{
// Using directive appeared previously in this namespace
yield return "CS0105";
}

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ namespace Foo

var fixedTestCode = @"namespace Foo
{
using System;
using System.Threading;
using global::Foo;
using global::System;
Expand All @@ -173,7 +174,9 @@ namespace Foo
[Fact]
public async Task TestInvalidOrderedUsingDirectivesWithNamespaceAliasQualifierAsync()
{
var testCode = @"using System.Threading;
var testCode = @"extern alias corlib;
using System.Threading;
using corlib::System;
using global::System.IO;
using global::System.Linq;
using global::System;
Expand All @@ -186,9 +189,13 @@ namespace Foo
using System;
}";

var fixedTestCode = @"namespace Foo
var fixedTestCode = @"extern alias corlib;
namespace Foo
{
using System;
using System.Threading;
using corlib::System;
using Foo;
using global::Foo;
using global::System;
using global::System.IO;
Expand All @@ -197,9 +204,9 @@ namespace Foo

DiagnosticResult[] expected =
{
this.CSharpDiagnostic().WithLocation(3, 1),
this.CSharpDiagnostic().WithLocation(4, 1),
this.CSharpDiagnostic().WithLocation(5, 1)
this.CSharpDiagnostic().WithLocation(5, 1),
this.CSharpDiagnostic().WithLocation(6, 1),
this.CSharpDiagnostic().WithLocation(7, 1)
};

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -308,6 +315,13 @@ public async Task TestPreprocessorDirectivesAsync()
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override IEnumerable<string> GetDisabledDiagnostics()
{
// Using directive appeared previously in this namespace
yield return "CS0105";
}

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down

0 comments on commit fb4a829

Please sign in to comment.