There is an issue with csharpsquid:S1144 (unused code detection) with razor pages

The rule which detects unused code (csharpsquid:S1144) does not work for razor pages/code behind files. Any method/member which is actually referenced in the razor page for event is flagged in sonar lint. I’m not sure this is a problem outside of sonar lint because I don’t see it listed as an issue once the code has been uploaded to sonarcloud.

Unfortunately, I have to remove this rule for now so that it doesn’t cause developers so much grief/paranoia when writing code.

edit: The same applies to rule csharpsquid:S4487

Is it at least possible for me to exclude these rules on any files matching this wildcard pattern: *.razor* ?

HI @SD321. Just to check - are you using Rider?

@duncanp Yes I am

@duncanp Hi, did that help you find the issue?

Hi @SD321

Sorry for the very late reply, for some reason this thread was missed by our team.

I struggle to reproduce because when I create a Razor page in Rider, it is using MVC pattern (so the page can only access public methods of the model).

For reference, here is a reproducer:

Event.razor

@page "/event"

<h1>@currentHeading</h1>

<p>
    <label>
        New title
        <input @bind="newHeading" />
    </label>
    <button @onclick="UpdateHeading">
        Update heading
    </button>
</p>

<p>
    <label>
        <input type="checkbox" @onchange="CheckChanged" />
        @checkedMessage
    </label>
</p>

Event.razor.cs

namespace RazorPagesMovie.Pages;

public partial class Event
{
    private string currentHeading = "Initial heading";
    private string? newHeading;
    private string checkedMessage = "Not changed yet";

    private void UpdateHeading()
    {
        currentHeading = $"{newHeading}!!!";
    }

    private void CheckChanged()
    {
        checkedMessage = $"Last changed at {DateTime.Now}";
    }
}

Yes, you can configure complex rule exclusion patterns in SonarCloud, and it will be picked up by SonarLint.

I will also see if there is something we can do to better handle this case in our analyzer.

Thanks for the update.
Can you try it with a blazor server app? Not sure if that makes a difference. I believe I did something to ignore this rule alrogether, temporarily, so I’ll enable it again and look to see if it’s still happening if you still don’t see any issues in the latest version of Rider.

1 Like

Sorry if I was not clear: I did manage to reproduce :slight_smile: Just it took me some time.

1 Like

Hi

Thanks for sharing this. We are currently exploring including the C# in .cshtml and .razor files in our analysis and I hope that this will resolve this issue. As you can imagine this is quite a big step and we are in the early stages but initial signs look promising. I’ll try and update this post as things progress.

Tom
.NET Ecosystem Product Manager

2 Likes

Hi there,

any update on this topic?

I have installed the SonarLint extension via Rider and am in Connected Mode.

I experience the same issue as mentioned here:

  • a {ComponentName}.razor.cs file with a partial class with a private property, a bool/flag
  • a {ComponentName}.razor file that uses that private property to show/hide some html

SonarLint says “Unread “private” fields should be removed” csharsquid:S4487.
But the property is in fact used in the .razor file.

Kind regards,

Joeri

Hey @Joeri_Ceulemans

While our C# analysis on SonarQube and SonarCloud now supports razor analysis, we do not support it in SonarLint (it’s challenging to do so with our current implementation of OmniSharp). Still, we’ve passed your feedback along to the relevant folks (the more traction, the more likely we move on it).