Search code examples
c#visual-studio-coderefactoringanalysis

No C# Code Analysis in Visual Studio Code


I'm currently using VS Code for C# development because I find it to be much more lightweight than Visual Studio itself, but there doesn't seem to be any code analysis tools available. This is a problem as I am not getting any warnings about unused functions (I am able to get warnings about unused local variables, however).

Some sample code:

namespace MyProject
{
    // No warning for this unused class.
    public class Dog
    {
        private int _age;
        private string _name;

        // No warning for this unused field.
        private string _furColor;

        public Dog(int age, string name)
        {
            // This unused variable generates a warning as expected.
            int unusedVariable = 2;

            _age = age;
            _name = name;
        }

        // No warning for this unused private function.
        private int Age()
        {
            return _age;
        }

        // No warning for this unused public function.
        public string Name()
        {
            return _name;
        }
    }
}

I already have the official C# extension by Microsoft installed, but it doesn't seem to help. I can't find any code analysis extensions in the VS Code Marketplace either, and searching online only points me to extensions for Visual Studio. I am also aware that FxCop can be installed via NuGet, but it seems to only be for Visual Studio as well.

Are there any C# code analysis tools that can be used with VS Code? Even external tools or dotnet commands would help.


Solution

  • Special thanks to Martheen for suggesting this link.

    All I had to do was add the following to my VS Code settings to enable better code analysis:

    "omnisharp.enableRoslynAnalyzers": true // Enable C# code analysis.