visual-studio-2022

Where is the setting for highlighting unused variables and constants in Visual Studio 2022?


I am working in a C# project where this has been turned off.

I can't find out how to turn it on again.

It's pretty hard to google as it's default behavior, and no sane person would turn it off...


Solution

  • Did you perhaps change your compiler warning level?

    Unused variables should have the warning of CS0168. If you look at that page, the title is "Compiler Warning (level 3) CS0168".

    In solution explorer, right click your project and go to Build and check your Warning Level:

    Warning Level

    If you set it to anything lower than three, then the CS0168 warnings will never be shown. Also, just below the drop down is a suppress warnings text box. Make sure you don't have CS0168 in that text box.

    Are you using an .editorconfig file?

    If you are using an .editorconfig file, you may have suppressed this particular warning. Removing that suppression should resolve this as well. It would look something like this in your .editorconfig file:

    [*.cs]
    
    # CS0168: Variable is declared but never used
    dotnet_diagnostic.CS0168.severity = none