33
44namespace 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 }
0 commit comments