Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 547
Floating Sunfish Avatar asked May 16 '26 09:05

Floating Sunfish


2 Answers

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.
like image 157
Floating Sunfish Avatar answered May 17 '26 23:05

Floating Sunfish


This might be obvious, but I actually screwed it up: You have to "enable" VS Code inside of your Unity Project https://vionixstudio.com/2021/11/02/unity-visual-studio-code/

  1. Open Unity Project
  2. Edit --> Preferences --> (Analysis) External Tools
  3. Change "External Script Editor" to VS Code
  4. Click on "Regenerate project files"

If you click on a script this should open VS Code and the code completion should work (at least as long as everything else is set up, worked for me)

like image 34
schneemax Avatar answered May 17 '26 23:05

schneemax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!