8

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.

Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48
  • 1
    Have you tried this https://www.strathweb.com/2019/04/roslyn-analyzers-in-code-fixes-in-omnisharp-and-vs-code/ ? It appears to be a WIP back then, not sure if it's already available for mainstream use today – Martheen Sep 10 '20 at 10:45
  • @Martheen Hey, this worked! Could you post this as an answer so I can accept it? Many thanks! :) – Floating Sunfish Sep 10 '20 at 10:52
  • 1
    Feel free to post the answer yourself, especially since you can point out exactly which configuration to enable. I don't have VS Code right now, and I'm not confident they haven't move things around. – Martheen Sep 10 '20 at 10:58
  • @Martheen Will do! ;) – Floating Sunfish Sep 10 '20 at 10:58

2 Answers2

14

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.
Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48
1

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)

schneemax
  • 11
  • 1