diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs index 4269ea96a..e7581fc74 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs @@ -59,6 +59,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var token = root.FindToken(diagnostic.Location.SourceSpan.Start); var tokenText = token.ValueText.TrimStart('_'); + if (tokenText == string.Empty) + { + // Skip this one, since we can't create a new identifier from this + continue; + } + var baseName = char.ToUpper(tokenText[0]) + tokenText.Substring(1); var newName = baseName; var memberSyntax = RenameHelper.GetParentDeclaration(token); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs index f29906582..2146b87cd 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1300UnitTests.cs @@ -982,5 +982,30 @@ public async Task TestUnderscoreExclusionAsync() await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } + + [Theory] + [InlineData("_")] + [InlineData("__")] + [WorkItem(3636, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3636")] + public async Task TestUnderscoreMethodAsync(string name) + { + var testCode = $@" +public class TestClass +{{ + public void [|{name}|]() + {{ + }} +}}"; + + var fixedCode = $@" +public class TestClass +{{ + public void [|{name}|]() + {{ + }} +}}"; + + await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false); + } } }