Skip to content

Commit e76e74f

Browse files
Copilotbcollamore
andcommitted
Fix PH2030: Avoid variables named exactly "_"
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
1 parent c9559fd commit e76e74f

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

Documentation/Diagnostics/PH2030.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515
Fields look like _foo. Locals look like foo. (This analyzer does not respect IntelliSense settings in the .editorconfig. It assumes this is your naming convention.)
1616

17+
Variables named exactly `_` (a single underscore) are not allowed as they can be confused with discards.
18+
1719
## How to solve
1820

1921
Rename fields to start with an underscore.
22+
Rename variables that are exactly `_` to a meaningful name.
2023

2124
## Example
2225

@@ -25,6 +28,11 @@ Code that triggers a diagnostic:
2528
class BadExample
2629
{
2730
private int number;
31+
32+
private void ProcessData()
33+
{
34+
byte[] _ = ExtractData(reader); // Variable named exactly "_"
35+
}
2836
}
2937

3038
```
@@ -34,6 +42,11 @@ And the replacement code:
3442
class GoodExample
3543
{
3644
private int _number;
45+
46+
private void ProcessData()
47+
{
48+
byte[] data = ExtractData(reader); // Use a meaningful name
49+
}
3750
}
3851

3952
```

Philips.CodeAnalysis.MaintainabilityAnalyzers/Naming/VariableNamingConventionAnalyzer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ private bool IsNameValid(Regex validator, SyntaxToken identifier)
134134
{
135135
var name = identifier.ValueText;
136136

137+
// Variables named exactly "_" should be avoided as they can be confused with discards
138+
if (name == "_")
139+
{
140+
return false;
141+
}
142+
137143
return validator.IsMatch(name);
138144
}
139145
}

Philips.CodeAnalysis.Test/Maintainability/Naming/VariableNamingConventionAnalyzerTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,35 @@ public async Task GeneratedCodeFilesShouldIgnoreLocalVariable()
416416
await VerifySuccessfulCompilation(givenText, "GlobalSuppressions").ConfigureAwait(false);
417417
}
418418

419+
[TestMethod]
420+
[TestCategory(TestDefinitions.UnitTests)]
421+
public async Task VariableNamedExactlyUnderscoreShouldFail()
422+
{
423+
var givenText = @"class Foo
424+
{{
425+
private void Bar()
426+
{{
427+
byte[] _ = ExtractData(reader);
428+
}}
429+
430+
private byte[] ExtractData(object reader) => new byte[0];
431+
}}
432+
";
433+
await VerifyDiagnostic(givenText, DiagnosticId.VariableNamingConventions).ConfigureAwait(false);
434+
}
435+
436+
[TestMethod]
437+
[TestCategory(TestDefinitions.UnitTests)]
438+
public async Task FieldNamedExactlyUnderscoreShouldFail()
439+
{
440+
var givenText = @"class Foo
441+
{{
442+
private byte[] _ = new byte[0];
443+
}}
444+
";
445+
await VerifyDiagnostic(givenText, DiagnosticId.VariableNamingConventions).ConfigureAwait(false);
446+
}
447+
419448
#endregion
420449
}
421450
}

0 commit comments

Comments
 (0)