Turn off C# variable discard in EditorConfig
Asked Answered
D

2

7

When formatting and auto fixing "linting" errors in C# files in VSCode it seems to discard my unused variables. Basically it puts _ = in front of everything.

It does this because csharp_style_unused_value_assignment_preference = discard_variable is the default. https://learn.microsoft.com/da-dk/dotnet/fundamentals/code-analysis/style-rules/ide0059#csharp_style_unused_value_assignment_preference

// 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;
}

That's neat. But how do I turn it off? So when I apply formatting to my C# files in VSCode it doesn't add _ =.

My VSCode settings:

{
  "settings": {
    "[csharp]": {
      "editor.defaultFormatter": "csharpier.csharpier-vscode",
      "editor.codeActionsOnSave": {
        "source.fixAll.csharp": true
      }
    },
    "omnisharp.enableEditorConfigSupport": true,
    "omnisharp.enableRoslynAnalyzers": true,
  }
}
Deferent answered 27/4, 2022 at 13:38 Comment(1)
You can try to exclude these fixers in your code cleanup configuration: 'Apply unused value preferences', 'Fix analyzer warnings and errors set in EditorConfig' and 'Fix all warnings and errors set in EditorConfig'.Villiform
G
13

Short answer:

  • csharp_style_unused_value_assignment_preference = discard_variable:none

Or if you want to use a local variable instead:

  • csharp_style_unused_value_assignment_preference = unused_local_variable:suggestion

Long answer:

Possibly the simplest way to find that out for yourself:

  1. Open the error list / panel in your IDE.
  2. In Visual Studio (and possibly other IDE's) you can click on the code to open the documentation or search online for the code.
  3. Read the documentation

You have two options now:

  • Directly set a level for that code dotnet_diagnostic.[ErrorCode].severity = none

    You should add a comment when you use the code.

  • If there is a property definied you can use that property. propertie_name = value:severity

    I would recommand the second way for easier readability, you can also add the code as comment.

Hint: Here are the options for the severity level.

Gittle answered 6/7, 2022 at 8:6 Comment(1)
If you are reading this, you'll probably want to turn off csharp_style_unused_value_expression_statement_preference too (set it to discard_variable:none).Luminous
S
1

Combined answer from @Schmebi and @F.D.Castel.

Use these settings:

csharp_style_unused_value_assignment_preference = discard_variable:none
csharp_style_unused_value_expression_statement_preference = discard_variable:none

If you are in Visual Studio then right click on your .editorconfig file and select Open With... and then Common Language Editor Supporting TextMate Bundles. Now you can edit every value inside Visual Studio.

enter image description here

Siphonophore answered 5/12, 2023 at 13:15 Comment(5)
Just as a thought: There is no way to do this from the VS UI, is there? You can choose if you prefer discard or unused local, but you can't tell it NOT to do it at all.Monteith
@RanSagy Updated the answer on how to edit the file from VS UI.Siphonophore
I was referring to the fact the C# code naming/style preferences as in the UI options don't have the "none" option but point taken; Thanks for the additional info.Monteith
@RanSagy The UI is has bad naming when compared to what the .editorconfig needs. "none" is called "Refactoring only". So if you set the "Avoid expression statements that implicitly ignore value" to "Discard" and "Refactoring Only" it will set discard_value:none. The same goes for "Avoid unused assignments" as well.Wane
@RuneG I think i tried that and it didn't work, But as you said the naming is confusing, ill retry it and report back :)Monteith

© 2022 - 2025 — McMap. All rights reserved.