From eeec60329a2754493f5b5128e698b22f1d288e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Thu, 13 Apr 2023 07:41:45 +0200 Subject: [PATCH] Update RenameToUpperCaseCodeFixProvider to not offer a code fix if the identifier only consists of underscores #3636 --- .../RenameToUpperCaseCodeFixProvider.cs | 6 +++++ .../NamingRules/SA1300UnitTests.cs | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) 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); + } } }