S112 not firing

I’m evaluating SonarLint (v.4.27.0.22695 with a local ruleset) in Visual Studio Enterprise 2019 (v16.7.6).

S112 General exceptions should never be thrown is checked and specified as Error.

I expect SonarLint to raise an S112 error for the following C# code, but it does not do so.

public class mysonartest
{
    public void Foo()
    {
        throw new Exception();
    }
}

I do get a S101 error about the class name.

Have I misunderstood something?

Hi Marin,

Welcome to our community forum! Your code example is correct to trigger the rule S112. So the problem is probably related to the ruleset configuration.

Can you try to remove your configuration files to see it in action in clean environment and start from there?

Thank you for your reply, Pavel.

Yes, S101 should be thrown. I left that error in my code to demonstrate that SonarLint is functioning. My issue is that I don’t understand why S112 is not firing.

What configuration files are you referring to? I have not added any config files. Are you referring to settings.json? When I load my test project I get the message:

Settings file does not exist at “C:\Users\xxxxxxxx\AppData\Roaming\SonarLint for Visual Studio\settings.json”.
Default settings will be used

All I have done so far is to:

  • install the Visual Studio extension which automatically added SonarAnalyzer.CSharp to the ruleset
  • set S100, S101 and S112 to Errors

That is all.

Pavel, Do you need any more information from me, please?

I’m sorry, there was a typo in my previous answer. I edited it to S112. When I copy/paste your example to empty project with SonarLint, I get both S101 and S112.

When you modify the rule type to Error a new *.ruleset file is created in your project’s directory. In theory, there might be multiple colliding ruleset configuration files.

Can you try to create an new empty solution only with this file in it? You should see the rule S112 triggered as a warning.
Then try to change the type of the rule to the Error. New ruleset file should be added by VS. And verify if the rule is trigged.

If it works, then there’s something in your original project preventing the rule from triggering. Either colliding ruleset or a rule suppression in .editorconfig file.

Thanks for your update, Pavel.

I’ve done as you suggested – a clean solution with just 1 class and an untouched Microsoft AllRules.ruleset. S112 is still not being triggered:

Thanks for the screenshot. Your project name has a “Test” word in it. That forces our analyzer to classify your project as a “Test Code”. But rule S112 as a “Main Code” scope so it doesn’t trigger.

If you rename the project or the assembly name in the Project Properties page the rule should start triggering.

Same applies to many other rules that are not relevant for Test Code.

Thank you, Pavel. I have renamed the project and S112 is now being triggered.

However, my original project was called ConsoleApplication1 so there must be another reason that S112 is not being triggered in that, so I need to look further to understand that. Are there other reasons that your analyzer will classify a project as ‘Test Code’?

The test detection checks for word “Test” in assembly name or presence of any of these project references:

  • Microsoft.VisualStudio.TestPlatform.TestFramework
  • Microsoft.VisualStudio.QualityTools.UnitTestFramework
  • xUnit
  • xUnit.Core
  • nUnit.Framework