Skip to content

Commit 5b9ca04

Browse files
authored
Merge pull request #2367 from bjornhellander/Issue2360_Sa1303SuggestsToRename_1varTo1var
Updated SA1309CodeFixProvider to not provide a code fix if the resulting identifier would be illegal
2 parents 36d2485 + 3855c91 commit 5b9ca04

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1309CodeFixProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.NamingRules
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CodeActions;
1212
using Microsoft.CodeAnalysis.CodeFixes;
13+
using Microsoft.CodeAnalysis.CSharp;
1314

1415
/// <summary>
1516
/// Implements a code fix for <see cref="SA1309FieldNamesMustNotBeginWithUnderscore"/>.
@@ -45,10 +46,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4546
{
4647
var newName = token.ValueText.TrimStart(new[] { '_' });
4748

48-
if (string.IsNullOrEmpty(newName))
49+
if (!SyntaxFacts.IsValidIdentifier(newName))
4950
{
50-
// The variable consisted of only underscores. In this case we cannot
51-
// generate a valid variable name and thus will not offer a code fix.
51+
// The proposed name was not legal, so no code fix will be offered.
5252
continue;
5353
}
5454

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1309UnitTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ public async Task TestFieldStartingWithMultipleUnderscoresAsync()
132132
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
133133
}
134134

135+
// This a regression test for issue #2360.
136+
[Fact]
137+
public async Task VerifyThatAFieldStartingWithAnUnderscoreAndADigitIsNotAffectedByCodeFixAsync()
138+
{
139+
var testCode = @"public class Foo
140+
{
141+
private string _1bar = ""baz"";
142+
}";
143+
144+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("_1bar").WithLocation(3, 20);
145+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
146+
147+
// no changes will be made
148+
var fixedCode = testCode;
149+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
150+
}
151+
152+
[Fact]
153+
public async Task VerifyThatAFieldStartingWithUnderscoreAndFollowedByKeywordTriggersDiagnosticAndIsCorrectedByCodefixAsync()
154+
{
155+
var testCode = @"public class Foo
156+
{
157+
private string _int = ""baz"";
158+
}";
159+
160+
var fixedCode = @"public class Foo
161+
{
162+
private string @int = ""baz"";
163+
}";
164+
165+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("_int").WithLocation(3, 20);
166+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
167+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
168+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
169+
}
170+
135171
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
136172
{
137173
yield return new SA1309FieldNamesMustNotBeginWithUnderscore();

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1303ConstFieldNamesMustBeginWithUpperCaseLetter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace StyleCop.Analyzers.NamingRules
55
{
66
using System;
7-
using System.Collections.Concurrent;
87
using System.Collections.Immutable;
98
using System.Linq;
109
using Microsoft.CodeAnalysis;

0 commit comments

Comments
 (0)