Back to Devexpress

CRR0044 - Unused local variable

coderushforroslyn-401168-static-code-analysis-analyzers-library-crr0044-unused-local-variable.md

latest2.0 KB
Original Source

CRR0044 - Unused local variable

  • Feb 28, 2025

This analyzer detects variables that are never used in your code and can be deleted.

csharp
class RemoveVariable {
    BlockSyntax GetGetterBody(IMemberDescriptor field) {
        var statementsList = new List<StatementSyntax>();
        return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
    }
}
vb
Class RemoveVariable
    Private Function GetGetterBody(ByVal field As IMemberDescriptor) As BlockSyntax
        Dim statementsList = New List(Of StatementSyntax)()
        Return SyntaxFactory.Block(New StatementSyntax() {FieldToReturnStatement(field)})
    End Function
End Class

To fix the issue, remove the unused variable:

csharp
class RemoveVariable {
    BlockSyntax GetGetterBody(IMemberDescriptor field) {
        return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
    }
}
vb
Class RemoveVariable
    Private Function GetGetterBody(ByVal field As IMemberDescriptor) As BlockSyntax
        Return SyntaxFactory.Block(New StatementSyntax() {FieldToReturnStatement(field)})
    End Function
End Class

Call one of the Remove Variable, Remove Variable with its Initializer, and Remove Discard Variable refactorings to remove the unused variable from your code. To do it in different places at once, use the Code Cleanup feature with the Remove unused variables rule enabled.

See Also

Suppress Analyzers