Skip to content

Commit fb4a829

Browse files
committed
Fix UsingCodeFixProvider handling of aliases
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 #1770
1 parent 6b7ba3d commit fb4a829

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
690690
.WithAdditionalAnnotations(UsingCodeFixAnnotation);
691691

692692
// filter duplicate using declarations, preferring to keep the one with an alias
693-
var existingUsing = result.Find(u => string.Equals(u.Name.ToUnaliasedString(), processedUsing.Name.ToUnaliasedString(), StringComparison.Ordinal));
693+
var existingUsing = result.Find(u => string.Equals(u.Name.ToNormalizedString(), processedUsing.Name.ToNormalizedString(), StringComparison.Ordinal));
694694
if (existingUsing != null)
695695
{
696696
if (!existingUsing.HasNamespaceAliasQualifier() && processedUsing.HasNamespaceAliasQualifier())

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ namespace Food
102102
using global::System;
103103
using global::System.IO;
104104
using global::System.Linq;
105+
using System;
105106
using System.Threading;
106107
using XYZ = System.IO;
107108
}";
@@ -135,10 +136,12 @@ namespace Food
135136

136137
var fixedTestCode = @"namespace Food
137138
{
139+
using Food;
138140
using global::Food;
139141
using global::System;
140142
using global::System.IO;
141143
using global::System.Linq;
144+
using System;
142145
using System.Threading;
143146
}";
144147

@@ -225,6 +228,13 @@ protected override string GetSettings()
225228
return CombinedUsingDirectivesTestSettings;
226229
}
227230

231+
/// <inheritdoc/>
232+
protected override IEnumerable<string> GetDisabledDiagnostics()
233+
{
234+
// Using directive appeared previously in this namespace
235+
yield return "CS0105";
236+
}
237+
228238
/// <inheritdoc/>
229239
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
230240
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210UnitTests.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ namespace Foo
155155

156156
var fixedTestCode = @"namespace Foo
157157
{
158+
using System;
158159
using System.Threading;
159160
using global::Foo;
160161
using global::System;
@@ -173,7 +174,9 @@ namespace Foo
173174
[Fact]
174175
public async Task TestInvalidOrderedUsingDirectivesWithNamespaceAliasQualifierAsync()
175176
{
176-
var testCode = @"using System.Threading;
177+
var testCode = @"extern alias corlib;
178+
using System.Threading;
179+
using corlib::System;
177180
using global::System.IO;
178181
using global::System.Linq;
179182
using global::System;
@@ -186,9 +189,13 @@ namespace Foo
186189
using System;
187190
}";
188191

189-
var fixedTestCode = @"namespace Foo
192+
var fixedTestCode = @"extern alias corlib;
193+
namespace Foo
190194
{
195+
using System;
191196
using System.Threading;
197+
using corlib::System;
198+
using Foo;
192199
using global::Foo;
193200
using global::System;
194201
using global::System.IO;
@@ -197,9 +204,9 @@ namespace Foo
197204

198205
DiagnosticResult[] expected =
199206
{
200-
this.CSharpDiagnostic().WithLocation(3, 1),
201-
this.CSharpDiagnostic().WithLocation(4, 1),
202-
this.CSharpDiagnostic().WithLocation(5, 1)
207+
this.CSharpDiagnostic().WithLocation(5, 1),
208+
this.CSharpDiagnostic().WithLocation(6, 1),
209+
this.CSharpDiagnostic().WithLocation(7, 1)
203210
};
204211

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

318+
/// <inheritdoc/>
319+
protected override IEnumerable<string> GetDisabledDiagnostics()
320+
{
321+
// Using directive appeared previously in this namespace
322+
yield return "CS0105";
323+
}
324+
311325
/// <inheritdoc/>
312326
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
313327
{

0 commit comments

Comments
 (0)