Skip to content

Commit

Permalink
Update RenameToUpperCaseCodeFixProvider to not offer a code fix if th…
Browse files Browse the repository at this point in the history
…e identifier only consists of underscores

#3636
  • Loading branch information
bjornhellander committed Apr 12, 2023
1 parent e1fa460 commit 11f9ddc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 11f9ddc

Please sign in to comment.