sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Fix S1144 FP: recognize usages of private members in partial Blazor components on Rider

Open naefp opened this issue 1 year ago • 5 comments

Description

When declaring private member or method in a razor.cs file, but only accessing it in the related razor file, sonar marks it as not used. Sonar should check the related razor file for S1144 as well.

Repro steps

Create a Blazor component with two files:

MyBlazorComponent.razor

<Button Clicked="@OnButtonOkClicked">
  OK
</Button>

MyBlazorComponent.razor.cs

public partial class MyBlazorComponent
{
  private Task OnButtonOkClicked()  // Sonar marks this method as not used
  {
      // Do something
  }
}

Expected behavior

Recognize that the method actually is being used in razor file.

Actual behavior

False positive for S1144.

Known workarounds

Mark every occurrence as false positive, or disable rule csharpsquid:S1144 for **/*.razor.cs completely.

Probably not using code in separate *razor.cs file but declare the private field in the razor file itself. Did not test that.

Related information

  • C# 12
  • Rider 2023.3.2
  • .Net 8
  • SonarLint plugin 10.2.1.77304 connected to SonarCloud
  • Windows 11

naefp avatar Jan 15 '24 21:01 naefp

Hi @naefp! Quick question, is the rule raising on SonarCloud, just in the IDE, or both?

I'm not sure, I saw it in the IDE but didn't explicitly check it on SonarCloud since we started using Sonar for an existing project and there are some hundreds of code smells that I'm trying to filter and clean step by step.

naefp avatar Jan 16 '24 20:01 naefp

I reproduced this using JetBrains Rider 2023.3.2

I confirm this as a FP. To provide additional context, the issue does not reproduce in the following:

  • SL4VS
  • local analysis run with SonarQube
  • analyzer UTs

Furthermore, this issue may be related to #8535, as they both exhibit similar behavior and share the same implementation (see here).

The issue seems to be related to SonarLint only. Since it is not yet able to analyze Razor generated files, it is not able to see the method usage in the component. We have an internal ticket to tackle the support of Razor in SonarLint which should fix this issue.

sebastien-marichal avatar Jan 23 '24 14:01 sebastien-marichal

I can confirm this is the case with SonarAnalyzer.CSharp 9.21.0.86780 as well when calling the analyzer via dotnet format

avidenic avatar Mar 19 '24 15:03 avidenic