Skip to content

Commit 368468b

Browse files
Copilotbcollamore
andcommitted
Fix PH2144: Handle DiscardDesignationSyntax for out parameters and update documentation severity to Error
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
1 parent 0ca9aa6 commit 368468b

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

Documentation/Diagnostics/PH2144.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# PH2144: Avoid variables named exactly '_'
22

3-
| Property | Value |
4-
|-----------------------------|----------------------------------------------------------|
5-
| **Rule ID** | PH2144 |
6-
| **Title** | Avoid variables named exactly '_' |
7-
| **Category** | Naming |
8-
| **Default severity** | Warning |
9-
10-
## Cause
3+
| Property | Value |
4+
|--|--|
5+
| Package | [Philips.CodeAnalysis.MaintainabilityAnalyzers](https://www.nuget.org/packages/Philips.CodeAnalysis.MaintainabilityAnalyzers) |
6+
| Diagnostic ID | PH2144 |
7+
| Category | [Naming](../Naming.md) |
8+
| Analyzer | [AvoidVariableNamedUnderscoreAnalyzer](https://github.com/philips-software/roslyn-analyzers/blob/main/Philips.CodeAnalysis.MaintainabilityAnalyzers/Naming/AvoidVariableNamedUnderscoreAnalyzer.cs)
9+
| CodeFix | No |
10+
| Severity | Error |
11+
| Enabled By Default | Yes |
12+
13+
## Introduction
1114

1215
This rule flags variables (local variables, foreach variables, for loop variables, using variables, and out parameters) that are named exactly `_` (single underscore).
1316

@@ -70,7 +73,7 @@ if (int.TryParse(input, out int result))
7073

7174
## Configuration
7275

73-
This rule is enabled by default with Warning severity.
76+
This rule is enabled by default with Error severity.
7477

7578
## Suppression
7679

Philips.CodeAnalysis.MaintainabilityAnalyzers/Naming/AvoidVariableNamedUnderscoreAnalyzer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// © 2019 Koninklijke Philips N.V. See License.md in the project root for license information.
22

3+
#pragma warning disable IDE0055 // Fix formatting
4+
35
using System.Linq;
46
using Microsoft.CodeAnalysis;
57
using Microsoft.CodeAnalysis.CSharp;
@@ -97,7 +99,7 @@ private void AnalyzeArgument(SyntaxNodeAnalysisContext context)
9799
return;
98100
}
99101

100-
// Check if it's a variable declaration (out int _)
102+
// Check if it's a variable declaration
101103
if (argument.Expression is DeclarationExpressionSyntax declaration)
102104
{
103105
if (declaration.Designation is SingleVariableDesignationSyntax variable)
@@ -111,6 +113,12 @@ private void AnalyzeArgument(SyntaxNodeAnalysisContext context)
111113
var diagnostic = Diagnostic.Create(Rule, location, variable.Identifier.ValueText);
112114
context.ReportDiagnostic(diagnostic);
113115
}
116+
else if (declaration.Designation is DiscardDesignationSyntax discard)
117+
{
118+
Location location = discard.UnderscoreToken.GetLocation();
119+
var diagnostic = Diagnostic.Create(Rule, location, discard.UnderscoreToken.ValueText);
120+
context.ReportDiagnostic(diagnostic);
121+
}
114122
}
115123
}
116124
}

0 commit comments

Comments
 (0)