Skip to content

Commit f50a6df

Browse files
committed
Fixed static class collisions in SA1101
1 parent 3cdcb61 commit f50a6df

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1101UnitTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,45 @@ public async Task TestPrefixLocalCallsWithThisCodeFixAsync()
306306
await this.VerifyCSharpFixAsync(ReferenceCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
307307
}
308308

309+
/// <summary>
310+
/// Verifies that a collision between a member and a static member is handled properly.
311+
/// This is a regression test for #2093
312+
/// </summary>
313+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
314+
[Fact]
315+
public async Task TestNameCollisionForStaticMethodAsync()
316+
{
317+
var testCode = @"
318+
using System;
319+
320+
public class TestClass
321+
{
322+
public DateTime DateTime => DateTime.FromFileTime(0);
323+
}
324+
";
325+
326+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
327+
}
328+
329+
/// <summary>
330+
/// Verifies that a collision between a member and a static property is handled properly.
331+
/// </summary>
332+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
333+
[Fact]
334+
public async Task TestNameCollisionForStaticPropertyAsync()
335+
{
336+
var testCode = @"
337+
using System;
338+
339+
public class TestClass
340+
{
341+
public DateTime DateTime => DateTime.UtcNow;
342+
}
343+
";
344+
345+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
346+
}
347+
309348
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
310349
{
311350
yield return new SA1101PrefixLocalCallsWithThis();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1101PrefixLocalCallsWithThis.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,25 @@ private static void HandleIdentifierNameImpl(SyntaxNodeAnalysisContext context,
188188
return;
189189
}
190190

191-
// This is a workaround for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1501 and can
192-
// be removed when the underlying bug in roslyn is resolved
191+
// This is a workaround for:
192+
// - https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1501
193+
// - https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2093
194+
// and can be removed when the underlying bug in roslyn is resolved
193195
if (nameExpression.Parent is MemberAccessExpressionSyntax)
194196
{
195-
var parentSymbol = context.SemanticModel.GetSymbolInfo(nameExpression.Parent, context.CancellationToken).Symbol as IFieldSymbol;
197+
var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(nameExpression.Parent, context.CancellationToken).Symbol;
196198

197-
if (parentSymbol != null
198-
&& parentSymbol.IsStatic
199-
&& parentSymbol.ContainingType.Name == symbol.Name)
199+
switch (memberAccessSymbol.Kind)
200200
{
201-
return;
201+
case SymbolKind.Field:
202+
case SymbolKind.Method:
203+
case SymbolKind.Property:
204+
if (memberAccessSymbol.IsStatic && (memberAccessSymbol.ContainingType.Name == symbol.Name))
205+
{
206+
return;
207+
}
208+
209+
break;
202210
}
203211
}
204212

0 commit comments

Comments
 (0)