Skip to content

Commit d45a445

Browse files
authored
Merge pull request #3637 from bjornhellander/feature/sa1300-fix-crash
Update RenameToUpperCaseCodeFixProvider to not offer a code fix if the identifier only consists of underscores
2 parents abf41d7 + eeec603 commit d45a445

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5959
{
6060
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
6161
var tokenText = token.ValueText.TrimStart('_');
62+
if (tokenText == string.Empty)
63+
{
64+
// Skip this one, since we can't create a new identifier from this
65+
continue;
66+
}
67+
6268
var baseName = char.ToUpper(tokenText[0]) + tokenText.Substring(1);
6369
var newName = baseName;
6470
var memberSyntax = RenameHelper.GetParentDeclaration(token);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,5 +982,30 @@ public async Task TestUnderscoreExclusionAsync()
982982

983983
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
984984
}
985+
986+
[Theory]
987+
[InlineData("_")]
988+
[InlineData("__")]
989+
[WorkItem(3636, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3636")]
990+
public async Task TestUnderscoreMethodAsync(string name)
991+
{
992+
var testCode = $@"
993+
public class TestClass
994+
{{
995+
public void [|{name}|]()
996+
{{
997+
}}
998+
}}";
999+
1000+
var fixedCode = $@"
1001+
public class TestClass
1002+
{{
1003+
public void [|{name}|]()
1004+
{{
1005+
}}
1006+
}}";
1007+
1008+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
1009+
}
9851010
}
9861011
}

0 commit comments

Comments
 (0)