coderushforroslyn-401168-static-code-analysis-analyzers-library-crr0044-unused-local-variable.md
This analyzer detects variables that are never used in your code and can be deleted.
class RemoveVariable {
BlockSyntax GetGetterBody(IMemberDescriptor field) {
var statementsList = new List<StatementSyntax>();
return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
}
}
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:
class RemoveVariable {
BlockSyntax GetGetterBody(IMemberDescriptor field) {
return SyntaxFactory.Block(new StatementSyntax[] { FieldToReturnStatement(field) });
}
}
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