Some warnings not shown for files in "Editor" folder in Unity project

  • Operating system: Windows 10 Pro, 22H2, 19045.5011
  • Visual Studio version: Community 2022, 17.11.5
  • SonarLint plugin version: 8.6.0.10679
  • Programming language you’re coding in: C#
  • Is connected mode used: No

There are at least a pair of SonarLint rules that are not being shown for Unity engine projects (checked in two of them) on files that are inside an “Editor” folder, but that are shown for other files. The rules I’ve found are “S1449” and “S1698”; I don’t know if it’s relevant, but those rules seem to be disabled by default, and I had added them as warnings to an “.editorconfig” file:

[*]
dotnet_diagnostic.S1449.severity = warning
dotnet_diagnostic.S1698.severity = warning

This is some code that shows the issue; the exact same code in different folders has different warnings shown:

public class _TEST_
{
    enum Stuff
    {
        Zero,
        One
    }

    class MyClass  // S1206 shown on all files.
    {
        public override bool Equals(object obj)
        {
            return true;
        }
    }

    public bool Whatever()
    {
        int meow;  // S1481 shown on all files.
        Stuff something = Stuff.One;
        string text;

        switch (something)
        {
            case Stuff.Zero:  // S2583 shown on all files.
                text = "0";
                break;

            case Stuff.One:  // S2589 and S3458 shown on all files.
            default:
                text = "1";
                break;
        }

        MyClass a = new();
        MyClass b = new();

        return a == b                   // S1698 not shown for files in "Editor" folder.
            && text.IndexOf("a") == 0;  // S1449 not shown for files in "Editor" folder.
    }
}

Hey there.

In my tests, having the files in an Editor folder didn’t affect the issues raised.

Is the Editor folder a different project? If so, could you share the .csproj for that project?

In Unity projects, code inside “Editor” folders is added to a separate project file (with some exceptions). After some digging I’ve found the cause: there’s a reference to the Unity package for NUnit (“Library\PackageCache\com.unity.ext.nunit@1.0.6\net35\unity-custom\nunit.framework.dll”) in that editor project file, and removing the reference (or the dll file itself) makes the warnings appear correctly.

So I’m assuming this is the same issue as SonarLint and Sonarqube show different issues - #6 by Damien_Urruty, right? Is there no way to avoid this (preferably without changing the project file)?