Skip to content

Commit cbe44ec

Browse files
committed
Add enum members to the list of elements checked by SA1300
Fixes #1859
1 parent b401bdf commit cbe44ec

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
9090
}
9191

9292
bool usedSuffix = false;
93-
if (declaredSymbol.Kind == SymbolKind.Field && !await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
93+
if (declaredSymbol.Kind == SymbolKind.Field
94+
&& declaredSymbol.ContainingType?.TypeKind != TypeKind.Enum
95+
&& !await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
9496
{
9597
usedSuffix = true;
9698
newName = newName + Suffix;

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,57 @@ public async Task TestLowerCaseEnumWithMemberMatchingTargetNameAsync()
271271
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
272272
}
273273

274+
[Fact]
275+
public async Task TestUpperCaseEnumMemberAsync()
276+
{
277+
var testCode = @"public enum Test
278+
{
279+
Member
280+
}";
281+
282+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
283+
}
284+
285+
[Fact]
286+
public async Task TestLowerCaseEnumMemberAsync()
287+
{
288+
var testCode = @"public enum Test
289+
{
290+
member
291+
}";
292+
var fixedCode = @"public enum Test
293+
{
294+
Member
295+
}";
296+
297+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("member").WithLocation(3, 5);
298+
299+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
300+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
301+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
302+
}
303+
304+
[Fact]
305+
public async Task TestLowerCaseEnumMemberWithConflictAsync()
306+
{
307+
var testCode = @"public enum Test
308+
{
309+
member,
310+
Member
311+
}";
312+
var fixedCode = @"public enum Test
313+
{
314+
Member1,
315+
Member
316+
}";
317+
318+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("member").WithLocation(3, 5);
319+
320+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
321+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
322+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
323+
}
324+
274325
[Fact]
275326
public async Task TestUpperCaseDelegateAsync()
276327
{

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1300ElementMustBeginWithUpperCaseLetter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal class SA1300ElementMustBeginWithUpperCaseLetter : DiagnosticAnalyzer
4848
private static readonly Action<SyntaxNodeAnalysisContext> NamespaceDeclarationAction = HandleNamespaceDeclaration;
4949
private static readonly Action<SyntaxNodeAnalysisContext> ClassDeclarationAction = HandleClassDeclaration;
5050
private static readonly Action<SyntaxNodeAnalysisContext> EnumDeclarationAction = HandleEnumDeclaration;
51+
private static readonly Action<SyntaxNodeAnalysisContext> EnumMemberDeclarationAction = HandleEnumMemberDeclaration;
5152
private static readonly Action<SyntaxNodeAnalysisContext> StructDeclarationAction = HandleStructDeclaration;
5253
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
5354
private static readonly Action<SyntaxNodeAnalysisContext> EventDeclarationAction = HandleEventDeclaration;
@@ -72,6 +73,7 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
7273
context.RegisterSyntaxNodeActionHonorExclusions(NamespaceDeclarationAction, SyntaxKind.NamespaceDeclaration);
7374
context.RegisterSyntaxNodeActionHonorExclusions(ClassDeclarationAction, SyntaxKind.ClassDeclaration);
7475
context.RegisterSyntaxNodeActionHonorExclusions(EnumDeclarationAction, SyntaxKind.EnumDeclaration);
76+
context.RegisterSyntaxNodeActionHonorExclusions(EnumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
7577
context.RegisterSyntaxNodeActionHonorExclusions(StructDeclarationAction, SyntaxKind.StructDeclaration);
7678
context.RegisterSyntaxNodeActionHonorExclusions(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
7779
context.RegisterSyntaxNodeActionHonorExclusions(EventDeclarationAction, SyntaxKind.EventDeclaration);
@@ -121,6 +123,11 @@ private static void HandleEnumDeclaration(SyntaxNodeAnalysisContext context)
121123
CheckElementNameToken(context, ((EnumDeclarationSyntax)context.Node).Identifier);
122124
}
123125

126+
private static void HandleEnumMemberDeclaration(SyntaxNodeAnalysisContext context)
127+
{
128+
CheckElementNameToken(context, ((EnumMemberDeclarationSyntax)context.Node).Identifier);
129+
}
130+
124131
private static void HandleStructDeclaration(SyntaxNodeAnalysisContext context)
125132
{
126133
CheckElementNameToken(context, ((StructDeclarationSyntax)context.Node).Identifier);

documentation/KnownChanges.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ involving members of a type.
161161

162162
## Naming Rules
163163

164+
### SA1300
165+
166+
StyleCop Analyzers adds enum members to the list of elements which must start with an upper-case letter, and reports
167+
SA1300 for violations. StyleCop Classic did not report any messages for enum members that did not start with an
168+
upper-case letter.
169+
164170
### SA1305
165171

166172
This rule is disabled by default in StyleCop Analyzers, but can be enabled by users via a rule set file.

documentation/SA1300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ following types of elements should use an upper-case letter as the first letter
2727
* Namespaces
2828
* Classes
2929
* Enums
30+
* Enum members
3031
* Structs
3132
* Delegates
3233
* Events

0 commit comments

Comments
 (0)