Underscore

Underscore

Summary

Avoid including an underscore character (_) in the identifier of local variables, local functions, and parameters.

Default severity

Warning

Description

Don't use underscores in identifies.

Discards

This analyzer ignores discards [1]. So it does not emit diagnostics to the following code:

// standalone: be ignored as long as no identifier named '_' is in scope.
 _ = "hello".Length;

// tuple
(int, int) NewPoint(int x, int y) => (x, y);
var (one, _) = NewPoint(1, 2);

// out parameter
void Out(out int x) => x = 3;
Out(out _);

// pattern matching (is)
if ("hello" is string _)
{
    ...
}

// pattern matching (switch)
switch ("hello")
{
case string _:
    break;
...
}

Lambda parameters

This analyzer also checks the input parameters of lambda expressions [2]. So it emits diagnostics to the following code:

Func<int, int, int> f = (a, _) => a; 

Code fix

The code fix provides an option replacing the identifier with underscore if the identifier contains only _ (a single underscore character). Otherwise, it provides an option of eliminating underscores in the identifier and concatenating words in the camel case style.

Example

Diagnostic

public void Method(int _param)
{
    var _ = 0;
    var max_retry_count = 100;
    if (TryToGet(out var return_value))
    {
    }
    if (this is object _o)
    {
        ⋮
    }
    void Local_Function()
    {
    }
    ⋮

Code fix

public void Method(int param)
{
    var underscore = 0;
    var maxRetryCount = 100;
    if (TryToGet(out var returnValue))
    {
    }
    if (this is object o)
    {
        ⋮
    }
    void LocalFunction()
    {
    }
    ⋮

References

[1] Microsoft, Discards (C# Reference)

[2] Microsoft, Lambda expressions (C# Programming Guide)