Back to Devexpress

CRR0045 - Local variable can be replaced with discard

coderushforroslyn-401170-static-code-analysis-analyzers-library-crr0045-local-variable-can-be-replaced-with-discard.md

latest1.2 KB
Original Source

CRR0045 - Local variable can be replaced with discard

  • Feb 28, 2025

This analyzer detects unused or assigned variables which can be replaced with a discard.

csharp
public static class ReplaceWithDiscard {
    public static bool HasAssignment(this SimpleNameSyntax identifier) {
        SyntaxNode assignment;
        return identifier.HasAssignment(out assignment);
    }
}

To fix the issue, replace the local variable with discard:

csharp
public static class ReplaceWithDiscard {
    public static bool HasAssignment(this SimpleNameSyntax identifier) {
        return identifier.HasAssignment(out _);
    }
}

Call the Discard Variable refactoring to replace unused or assigned variables with a discard. To do it in different places at once, use the Code Cleanup feature with the Replace unused variables with discard code cleanup rule enabled.

See Also

Suppress Analyzers