All rules
IDE0058Language rules (expression-level preferences)

Remove unnecessary expression value

Remove unnecessary expression value

Microsoft docs

Description

This rule flags unused expression values. For example:

You can take one of the following actions to fix this violation:

  • If the expression has no side effects, remove the entire statement. This improves performance by avoiding unnecessary computation.
  • If the expression has side effects, replace the left side of the assignment with a discard (C# only) or a local variable that's never used. This improves code clarity by explicitly showing the intent to discard an unused value.

Example

// Original code:
System.Convert.ToInt32("35");

// After code fix for IDE0058:

// csharp_style_unused_value_expression_statement_preference = discard_variable
_ = System.Convert.ToInt32("35");

// csharp_style_unused_value_expression_statement_preference = unused_local_variable
var unused = Convert.ToInt32("35");

Configurable options

Vote for the value each option should take in the generated .editorconfig.

csharp_style_unused_value_expression_statement_preference
default: discard_variable
visual_basic_style_unused_value_expression_statement_preference
default: unused_local_variable
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0