Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RenameToUpperCaseCodeFixProvider to not offer a code fix if the identifier only consists of underscores #3637

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}