Skip to content

Commit c011dd1

Browse files
committed
Merge pull request #2002 from sharwell/rm-duplicate-filtering
Remove duplicate filtering
2 parents 7a4fcf8 + 7fe5c05 commit c011dd1

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -710,20 +710,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
710710
processedUsing = processedUsing.WithAdditionalAnnotations(FileHeaderStrippedAnnotation);
711711
}
712712

713-
// filter duplicate using declarations, preferring to keep the one with an alias
714-
var existingUsing = result.Find(u => string.Equals(u.Name.ToNormalizedString(), processedUsing.Name.ToNormalizedString(), StringComparison.Ordinal));
715-
if (existingUsing != null)
716-
{
717-
if (!existingUsing.HasNamespaceAliasQualifier() && processedUsing.HasNamespaceAliasQualifier())
718-
{
719-
result.Remove(existingUsing);
720-
result.Add(processedUsing);
721-
}
722-
}
723-
else
724-
{
725-
result.Add(processedUsing);
726-
}
713+
result.Add(processedUsing);
727714
}
728715

729716
result.Sort(this.CompareUsings);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ namespace Food
138138
{
139139
using Food;
140140
using global::Food;
141+
using global::Food;
141142
using global::System;
142143
using global::System.IO;
143144
using global::System.Linq;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ namespace Foo
198198
using corlib::System;
199199
using Foo;
200200
using global::Foo;
201+
using global::Foo;
201202
using global::System;
202203
using global::System.IO;
203204
using global::System.Linq;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1211UnitTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,38 @@ public async Task TestPreprocessorDirectivesAsync()
196196
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
197197
}
198198

199+
/// <summary>
200+
/// Verifies that the analyzer will not combine using directives that refer to the same type. This is a
201+
/// regression test for DotNetAnalyzers/StyleCopAnalyzers#2000.
202+
/// </summary>
203+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
204+
[Fact]
205+
public async Task TestSameUsingWithDifferentAliasAsync()
206+
{
207+
string testCode = @"namespace NamespaceName
208+
{
209+
using Func2 = System.Func<int>;
210+
using Func1 = System.Func<int>;
211+
212+
internal delegate void DelegateName(Func1 arg1, Func2 arg2);
213+
}
214+
";
215+
string fixedCode = @"namespace NamespaceName
216+
{
217+
using Func1 = System.Func<int>;
218+
using Func2 = System.Func<int>;
219+
220+
internal delegate void DelegateName(Func1 arg1, Func2 arg2);
221+
}
222+
";
223+
224+
var expected = this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("Func1", "Func2");
225+
226+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
227+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
228+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
229+
}
230+
199231
/// <inheritdoc/>
200232
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
201233
{

0 commit comments

Comments
 (0)