Anders Sewerin Johansen
Anders Sewerin Johansen

Reputation: 1666

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...

Upvotes: 5

Views: 9919

Answers (2)

Saeb Amini
Saeb Amini

Reputation: 24449

From my investigation, it seems this analyzer was somehow missed in Microsoft porting from FxCop to shipping roslyn-analyzers with .NET. I've raised an issue for it on their GitHub.

The only way I found to capture this error correctly (as of writing this) is to use the StyleChecker NuGet package which supports it:

https://github.com/maroontress/StyleChecker/blob/main/doc/rules/UnusedVariable.md

Upvotes: 1

Timothy G.
Timothy G.

Reputation: 9195

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

Upvotes: 1

Related Questions