Roslyn-analyzers: CA1804: Remove unused locals - Doesn't Work

Created on 12 Feb 2019  路  13Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers
Microsoft.CodeQuality.Analyzers

This is probably the most fundamental code rule there is. Have I missed a NuGet package or something? If the original code rule isn't going to be implemented, there needs to be some kind of replacement.

Package Version

Example: v2.6.3

Diagnostic ID

CA1804: Remove unused locals

Repro steps

namespace ClassLibrary1
{
    public static class Class1
    {
        public static void test()
        {
            var asdasd = new object();
        }
    }
}

image

Expected behavior

An error/warning that says

"{0} declares a variable, {1}, of type {2}, which is never used or is only assigned to. Use this variable or remove it."

Actual behavior

No warning/error

Most helpful comment

So how can we ensure that compilation fails if there are unused local variables? (I actually want compilation failures for ANY unreferenced code/variables/properies etc., or code only called from tests, like I can get from Eclipse for java - it's also smart enough to recognise web controller endpoint functions).

All 13 comments

This has been ported over to Visual Studio 2019 as IDE0059 (message severity by default), which shows up by default in the IDE while typing. You can try this out at https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-preview.

In future, Visual Studio IDE analyzers will be available as a NuGet package to enforce these on CI. At present, we are not going to duplicate the rule in both the FxCop analyzers package and VS2019 IDE.

@mavasani this rule has very strange behavior. I realized just now that if the variable is a string, it will kick in. If it's an object, it will not kick in.

Fine:
image

Note fine:
image

@MelbourneDeveloper You probably again confused with CSxxxx error here - this is the one from the compiler which only flags locals assigned to _constant_ values as unused.

I actually meant that the VS IDE (not the compiler) now generates IDE0059 (see the 0 of 2 messages in the error list), and also fades out unused local. This diagnostic will not be generated during build OR break your build, it is just a live inbuilt analysis in Visual Studio.

@mavasani OK. Thanks

Also note that you need at least VS2019 Preview2 to see IDE0059 violations.

So how can we ensure that compilation fails if there are unused local variables? (I actually want compilation failures for ANY unreferenced code/variables/properies etc., or code only called from tests, like I can get from Eclipse for java - it's also smart enough to recognise web controller endpoint functions).

Tagging @sharwell. Once we move all or most of the IDE analyzers to a NuGet package, you should be able to enforce them on the build.

@mavasani @sharwell this is a major one for me. In fact, I would say that this is probably the most fundamental code rule of all. If variables or members are declared anywhere that are not being used, they should be the first things to get deleted from the code base.

@MelbourneDeveloper I am going to mark this as external as we have already a rule implementation in VS IDE, which will be used to detect these cases and offer a code fix to remove them

@mavasani Is there any timeline for moving the IDE analyzers to a NuGet package? Is there any project or something that I can subscribe to in order to track the progress of this? It's a pretty bad step backwards to lose these type of warnings in CI now that we're moving away from the legacy FxCop.

Is there any timeline for moving the IDE analyzers to a NuGet package? Is there any project or something that I can subscribe to in order to track the progress of this?

Tagging @sharwell @jinujoseph

A kind ping.

See update here: https://github.com/dotnet/roslyn/issues/33558#issuecomment-605974524. Adding a NuGet package reference to the CodeStyle NuGet package should enforce IDE0059 to flag all redundant locals and unnecessary assignments to locals/parameters on build/CI.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NtFreX picture NtFreX  路  3Comments

Grauenwolf picture Grauenwolf  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments