Muokkaa

Jaa


Remove unnecessary value assignment (IDE0059)

Property Value
Rule ID IDE0059
Title Remove unnecessary value assignment
Category Style
Subcategory Unnecessary code rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options csharp_style_unused_value_assignment_preference
visual_basic_style_unused_value_assignment_preference

Overview

This rule flags unnecessary value assignments. For example:

// IDE0059: value written to 'v' is never
// read, so assignment to 'v' is unnecessary.
int v = Compute();
v = Compute2();

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

  • If the expression on the right side of the assignment has no side effects, remove the expression or the entire assignment statement. This improves performance by avoiding unnecessary computation.

    int v = Compute2();
    
  • If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a discard (C# only) or a local variable that's never used. Discards improve code clarity by explicitly showing the intent to discard an unused value.

    _ = Compute();
    int v = Compute2();
    

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_assignment_preference

Property Value Description
Option name csharp_style_unused_value_assignment_preference
Applicable languages C#
Option values discard_variable Prefer to use a discard when assigning a value that's not used
unused_local_variable Prefer to use a local variable when assigning a value that's not used
Default option value discard_variable
// csharp_style_unused_value_assignment_preference = discard_variable
int GetCount(Dictionary<string, int> wordCount, string searchWord)
{
    _ = wordCount.TryGetValue(searchWord, out var count);
    return count;
}

// csharp_style_unused_value_assignment_preference = unused_local_variable
int GetCount(Dictionary<string, int> wordCount, string searchWord)
{
    var unused = wordCount.TryGetValue(searchWord, out var count);
    return count;
}

visual_basic_style_unused_value_assignment_preference

Property Value Description
Option name visual_basic_style_unused_value_assignment_preference
Applicable languages Visual Basic
Option values unused_local_variable Prefer to use a local variable when assigning a value that's not used
Default option value unused_local_variable
' visual_basic_style_unused_value_assignment_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 IDE0059
// The code that's violating the rule is on this line.
#pragma warning restore IDE0059

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

[*.{cs,vb}]
dotnet_diagnostic.IDE0059.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.

Property Value
Rule ID IDE0059
Title The value is unused
Category Style
Applicable languages F#
Options None

Overview

This rule flags unnecessary value assignments. For example, answer is unused in the following snippet:

type T() =
    let answer = 42

See also