Skip to content

Commit eae2771

Browse files
committed
Improve the accuracy of IsValidNewMemberName
1 parent 9c207c4 commit eae2771

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.Helpers
55
{
6+
using System.Collections.Immutable;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis;
@@ -31,10 +32,19 @@ public static async Task<Solution> RenameSymbolAsync(Document document, SyntaxNo
3132

3233
public static bool IsValidNewMemberName(SemanticModel semanticModel, ISymbol symbol, string name)
3334
{
34-
var members = (symbol as INamedTypeSymbol)?.GetMembers(name);
35-
if (members.HasValue && !members.Value.IsDefaultOrEmpty)
35+
if (symbol.Kind == SymbolKind.NamedType)
3636
{
37-
return false;
37+
TypeKind typeKind = ((INamedTypeSymbol)symbol).TypeKind;
38+
39+
// If the symbol is a class or struct, the name can't be the same as any of its members.
40+
if (typeKind == TypeKind.Class || typeKind == TypeKind.Struct)
41+
{
42+
var members = (symbol as INamedTypeSymbol)?.GetMembers(name);
43+
if (members.HasValue && !members.Value.IsDefaultOrEmpty)
44+
{
45+
return false;
46+
}
47+
}
3848
}
3949

4050
var containingSymbol = symbol.ContainingSymbol as INamespaceOrTypeSymbol;
@@ -50,16 +60,21 @@ public static bool IsValidNewMemberName(SemanticModel semanticModel, ISymbol sym
5060
}
5161
else if (containingSymbol.Kind == SymbolKind.NamedType)
5262
{
53-
// The name can't be the same as the name of the containing type
54-
if (containingSymbol.Name == name)
63+
TypeKind typeKind = ((INamedTypeSymbol)containingSymbol).TypeKind;
64+
65+
// If the containing type is a class or struct, the name can't be the same as the name of the containing
66+
// type.
67+
if ((typeKind == TypeKind.Class || typeKind == TypeKind.Struct)
68+
&& containingSymbol.Name == name)
5569
{
5670
return false;
5771
}
5872
}
5973

60-
// The name can't be the same as the name of an other member of the same type
61-
members = containingSymbol.GetMembers(name);
62-
if (!members.Value.IsDefaultOrEmpty)
74+
// The name can't be the same as the name of an other member of the same type. At this point no special
75+
// consideration is given to overloaded methods.
76+
ImmutableArray<ISymbol> siblings = containingSymbol.GetMembers(name);
77+
if (!siblings.IsDefaultOrEmpty)
6378
{
6479
return false;
6580
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1302UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ public interface IFoo { }";
209209
}
210210

211211
[Fact]
212-
public async Task TestInterfaceDeclarationDoesNotStartWithIWithMemberConflictAsync()
212+
public async Task TestInterfaceDeclarationDoesNotStartWithIWithMemberMatchingTargetNameAsync()
213213
{
214214
string testCode = @"
215215
public interface Foo
216216
{
217217
int IFoo { get; }
218218
}";
219219
string fixedCode = @"
220-
public interface IFoo1
220+
public interface IFoo
221221
{
222222
int IFoo { get; }
223223
}";

0 commit comments

Comments
 (0)