You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This rule flags variables (local variables, foreach variables, for loop variables, using variables, and out parameters) that are named exactly `_` (single underscore).
16
+
17
+
## Reason
18
+
19
+
Variables named exactly `_` can be confused with C# discards. This can lead to unintended behavior where developers think they are using a discard but are actually creating a variable that captures the value.
20
+
21
+
## How to fix violations
22
+
23
+
Either use a proper discard (if you don't need the value) or use a more descriptive variable name (if you do need the value).
24
+
25
+
## Examples
26
+
27
+
### Incorrect
28
+
29
+
```csharp
30
+
// Local variable - looks like discard but is actually a variable
31
+
byte[] _=ExtractData(reader);
32
+
33
+
// ForEach variable - confusing
34
+
foreach (var_initems)
35
+
{
36
+
// code
37
+
}
38
+
39
+
// Out parameter - looks like discard
40
+
if (int.TryParse(input, outint_))
41
+
{
42
+
// code
43
+
}
44
+
```
45
+
46
+
### Correct
47
+
48
+
```csharp
49
+
// Use a proper discard if you don't need the value
50
+
_=ExtractData(reader);
51
+
52
+
// Or use a descriptive name if you do need the value
53
+
byte[] data=ExtractData(reader);
54
+
55
+
// ForEach with discard
56
+
foreach (var_initems)
57
+
{
58
+
// Use discard syntax instead
59
+
}
60
+
61
+
// Out parameter with discard
62
+
if (int.TryParse(input, out_))
63
+
{
64
+
// code
65
+
}
66
+
67
+
// Or with a descriptive name
68
+
if (int.TryParse(input, outintresult))
69
+
{
70
+
// code using result
71
+
}
72
+
```
73
+
74
+
## Configuration
75
+
76
+
This rule is enabled by default with Error severity.
77
+
78
+
## Suppression
79
+
80
+
```csharp
81
+
[SuppressMessage("Philips.CodeAnalysis.MaintainabilityAnalyzers", "PH2147:Avoid variables named exactly '_'", Justification="Reviewed.")]
privateconststringTitle=@"Avoid variables named exactly '_'";
16
+
privateconststringMessageFormat=@"Variable '{0}' is named '_' which can be confused with a discard. Consider using a discard or a more descriptive variable name";
17
+
privateconststringDescription=@"Variables named exactly '_' can be confused with C# discards. Use either a proper discard or a more descriptive variable name.";
Copy file name to clipboardExpand all lines: Philips.CodeAnalysis.MsTestAnalyzers/Philips.CodeAnalysis.MsTestAnalyzers.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,7 @@
16
16
|[PH2017](../Documentation/Diagnostics/PH2017.md)| Avoid ClassInitialize attribute | This attribute is unnecessary in the C# programming language. They circumvent TestTimeouts, allowing for slower Test Runs. They can contribute to non-deterministic test execution order, resulting complications while debugging. |
17
17
|[PH2018](../Documentation/Diagnostics/PH2018.md)| Avoid ClassCleanup attribute | This attribute is unnecessary in the C# programming language. They circumvent TestTimeouts, allowing for slower Test Runs. They can contribute to non-deterministic test execution order, resulting complications while debugging. |
18
18
|[PH2019](../Documentation/Diagnostics/PH2019.md)| Avoid TestCleanup attribute | This attribute is unnecessary in the C# programming language. They circumvent TestTimeouts, allowing for slower Test Runs. They can contribute to non-deterministic test execution order, resulting complications while debugging. |
19
-
|[PH2034](../Documentation/Diagnostics/PH2034.md)| TestMethod requires TestClass | TestMethods not inside a TestClass are not executed. They are dead code. |
20
-
|[PH2035](../Documentation/Diagnostics/PH2035.md)| DataTestMethod requires correct parameter count | DataTestMethods must have the same number of parameters of the DataRows, TestMethods should have no arguments. |
19
+
|[PH2034](../Documentation/Diagnostics/PH2034.md)| TestMethod requires TestClass | TestMethods not inside a TestClass are not executed. They are dead code. |
21
20
|[PH2036](../Documentation/Diagnostics/PH2036.md)| TestMethod is public | TestMethods must be public. |
22
21
|[PH2037](../Documentation/Diagnostics/PH2037.md)| TestMethods and DataTestMethods require unique names | TestMethods and DataTestMethods require unique names. |
23
22
|[PH2038](../Documentation/Diagnostics/PH2038.md)| TestClass is public | TestClass must be public. |
0 commit comments