Rule S1542 works in one project of my VS 2017 solution, but not the other

  • Operating system: Windows 10 Education: 10.0.19044 Build 19044
  • SonarLint plugin version: SonarLint for Visual Studio 2017 5.5.0.43817
  • Connected to: SonarQube Version 9.4 Build 54424

My solution has two projects. Both projects have rule sets that simply include the rule set in the .sonarlint folder of my solution. Both projects appear to be working with almost all of the rules in that rule set.

For example: Rules S108, S1125, and S1481 work in both of my projects.

Rule S1542 works only in one of my projects. By “works” I mean the error is underlined and shown in the error list window. In the other project, it seems that this rule is simply ignored for some reason.

As far as I can tell, there are not multiple sonar rule sets in my solution.

If there’s any more information I can collect to assist with resolving this issue, I would be very glad to do that.

Hey there.

A good start would be providing sample(s) of code where the issue is raised, and where it isn’t raised (that you would expect it to be)

Hi Colin,

Thanks for the willingness to help!

Below is an improperly named method per rule S1542:

Private Sub TESTMETHOD()
    '
End Sub

I have pasted this method into both of my projects, but only one of the projects recognizes and reports this in the error list.

However, if I remove the apostrophe, both projects will report a violation of rule S1186.

I’ve also just noticed that my project that isn’t working with rule S1542 also doesn’t work with rules S2364, S2366, or S101. I suspect the project also doesn’t work with the other various naming related rules.

I just noticed that rule S2178 also only works in one of my projects.

Another update: not all naming related rules are nonfunctioning. I can confirm that rule S117 is working in both of my projects.

Another update: I’ve been able to upgrade to Visual Studio 2022 and all of the issues described above are still present with the updated IDE and updated SonarLint extension.

@Justin_Nelson is one of the projects a test project and the other a non-test project?
If so, this thread might explain what is going on.

Hi Duncan,

Firstly, thanks for taking the time to help!

Both projects are marked as not test projects (as in: SonarQubeTestProject = false).

Hi Justin,

regarding rule S1186: the rule works as expected since the comment is handled as an exception and will make the method compliant. In the documentation it’s the first case from the Compliant solution.

Compliant Solution

Sub DoSomething()
    ' Not implemented because of reason
End Sub

Regarding the other rules, could you provide some code examples where the behavior is not as expected?

Hi Costin,

Just to clarify my comment about rule S1186: I wasn’t implying that that rule was broken. I was using it as an example of a successful rule application in both projects. I meant to show that because that rule worked in both projects, both projects are at least capable of applying some of the rules, but not all (S1542).

My original code example is an example of rule S1542 failing to apply.

All following examples have been pasted into both of my projects and are failing to apply in only one:

S2364:
Private TEST As Integer

S2366:
Public Property TEST As Integer

Upon further examination of rule S101 I’ve found that it does work for both projects. I was fooled into believing that rule didn’t work because I was looking at an improperly named module, not an improperly named class.

Hi Justin, thanks for clarification. I misunderstood.

Regarding S2364 and S2366: both rules are designed to work only on production code and to be ignored on test projects.

Could you please run an analysis in verbose mode (by passing /d:sonar.verbose=true parameter in the begin step) and attach the logs of the end step?

I want to see if the project with this issue is correctly identified as MAIN.

Thanks,
Costin

Hi Costin,

Do you mean running an analysis in Visual Studio in verbose mode?

I don’t know how to pass parameters, or what I would even be passing parameters to.

Can you provide some additional details?

Hi Justin,

Sorry I forgot to mention, I was thinking to reproduce this issue using the SonarScanner for .NET and not SonarLint. The verbose parameter can be passed only to the scanner.

By the way, is the issue reproducible only with SonarLint or with the SonarScanner for .NET too?

Does the project with missing issues reference any test framework assemblies or other test related dependencies: mocking or assertion frameworks? Currently SonarQubeTestProject property is not read by the SonarLint.

Hi Costin,

The issue is only present with SonarLint. SonarScanner for .NET is working exactly as expected so we can still see any issues introduced, but we have to wait until someone pushes new code and wait for a build to complete until we see the complete set of issues.

I just searched through the project references and found that it was referencing a unit testing framework called NUnit. After removing those references, it seems that all the expected rules are working just as intended! Thank you!

Do you know why these references would be causing only some rules to fail?

I just searched through the project references and found that it was referencing a unit testing framework called NUnit. After removing those references, it seems that all the expected rules are working just as intended! Thank you!

Great. Happy that it solved the problem for you.

Do you know why these references would be causing only some rules to fail?

Yes. We have some rules that are designed to work only for not test projects and some rules that are designed to work only for test projects. We also have an automatic detection heuristic on our rules that detects the project type and enable/disable the rules depending on it. Since the SonarQubeTestProject property is not read by the SonarLint (currently it’s read only by the scanner), we used the heuristic instead.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.