Skip to main content

MisusedOrDefault

Diagnostic Rule Overview

FieldValue
IDSHIMMER1100
Analyzer titleOrDefault()! is redundant
Analyzer messageReplace '0!' with '1'
Code fix titleSimplify 'OrDefault()!' method call
Default severityWarning
Minimum framework/language versionN/A
CategoryShimmeringUsage
Link to codeMisusedOrDefaultAnalyzer.cs
Code fix exists?Yes

Detailed Explanation

This analyzer detects instances where a built-in LINQ OrDefault method is immediately followed by the null-forgiving operator (!). Such usage is redundant because the non-OrDefault counterpart of the method exists and should be used instead, when you expect there will always be such an element.

If you believe the OrDefault method could return null but the null-forgiving operator (!) may have been added by mistake, you should remove the null-forgiving operator instead.

Examples

Flagged code:

using System.Linq;

namespace Tests;
class Test
{
static int[] array = [1];
void Do()
{
var a = array.SingleOrDefault()!;
}
}

Fixed code:

using System.Linq;

namespace Tests;
class Test
{
static int[] array = [1];
void Do()
{
var a = array.Single();
}
}

Justification of the Severity

Assuming that the null-forgiving operator (!) was intentional, updated code will always be clearer and shorter, while the behavior is exactly the same.

When to suppress

This should not be suppressed, although you may fix instead by removing an unnecessary !.