Edit

Share via


Remove unnecessary expression value (IDE0058)

Property Value
Rule ID IDE0058
Title Remove unnecessary expression value
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options csharp_style_unused_value_expression_statement_preference
visual_basic_style_unused_value_expression_statement_preference

Overview

This rule flags unused expression values. For example:

void M()
{
  Compute(); // IDE0058: computed value is never used.
}

int Compute();

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.

    _ = Compute();
    

Options

The options for this specify whether to prefer the use of a discard or an unused local variable:

For information about configuring options, see Option format.

csharp_style_unused_value_expression_statement_preference

Property Value Description
Option name csharp_style_unused_value_expression_statement_preference
Applicable languages C#
Option values discard_variable Prefer to assign an unused expression to a discard
unused_local_variable Prefer to assign an unused expression to a local variable that's never used
Default option value discard_variable
// 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");

visual_basic_style_unused_value_expression_statement_preference

Property Value Description
Option name visual_basic_style_unused_value_expression_statement_preference
Applicable languages Visual Basic
Option values unused_local_variable Prefer to assign an unused expression to a local variable that's never used
Default option value unused_local_variable
' visual_basic_style_unused_value_expression_statement_preference = unused_local_variable
Dim unused = Computation()

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0058
// The code that's violating the rule is on this line.
#pragma warning restore IDE0058

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0058.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also