0

I am trying to customize some rules, particularly S134 but it does not take my deviation from the default max parameter into account

Repro: Create new Console App in Visual Studio, Add SonarAnalyzer.CSharp NuGet package

Add file SonarLint.xml, set Build Action to Additional Analyzer file

<?xml version="1.0" encoding="utf-8"?>
<AnalysisInput>
    <Settings>
    </Settings>
    <Rules>
        <Rule>
            <Key>S134</Key>
            <Parameters>
                <Parameter>
                    <Key>max</Key>
                    <Value>4</Value>
                </Parameter>
            </Parameters>
        </Rule>
    </Rules>
</AnalysisInput>

Code:

internal class Program
{
    protected Program(){}

    static Random rnd = new Random();

    static void Main(string[] args)
    {
        if (Random())
        {
            if (!Random())
            {
                if (Random())
                {
                    if (!Random())
                    {
                        Console.WriteLine("Wow you're lucky");
                    }
                }
            }
        }
    }

    static bool Random()
    {
        return rnd.Next(2) == 0;
    }
}

Add .editorconfig

[*.cs]
dotnet_diagnostic.S134.severity = error

Result is that it still uses the default parameter of 3

enter image description here

metacircle
  • 2,438
  • 4
  • 25
  • 39

1 Answers1

0

After digging around int the SonarAnalyzer sources I found that the parameter name is not what was displayed on my SonarQube rule description. It is not called maxbut maximumNestingLevel.

<?xml version="1.0" encoding="UTF-8"?>
<AnalysisInput>
  <Settings>
  </Settings>
  <Rules>
    <Rule>
      <Key>S134</Key>
      <Parameters>
        <Parameter>
          <Key>maximumNestingLevel</Key>
          <Value>4</Value>
        </Parameter>
      </Parameters>
    </Rule>
  </Rules>
  <Files>
  </Files>
</AnalysisInput>
metacircle
  • 2,438
  • 4
  • 25
  • 39