False positive for unused variable using Razor pages

  • Operating system: Windows 11
  • SonarQube for IntelliJ plugin version: 10.15.0.80347
  • IntelliJ version: JetBrains Rider 2024.3.4
  • Programming language you’re coding in: C# Razor pages
  • Is connected mode used: No

And a thorough description of the problem / question:
I ma getting a lot of “unused variable” notifications which i am not getting in the visual studio 22 version of the plugin. I have a partial class backing a razor page, and the only use of a parameter is on the razor page itself (linked to a button for example)
Razor uses reflection, so the read/write is implied, but SonarQube assumes “never read, remove unused variable” or “Unassigned field” warnings. As well as sometimes “Make ReadOnly”

It sadly is quite annoying whem comitting these files.

Hello @Vince.scholt, and thanks for reaching out to us about this.

We expressly exclude analysis of razor files, but as I understand from your post, it’s a partial class backing a razor page. Could you please share a basic example of such a file with us?

Of course, here is a small version of a component we made. It is based on Blazorise, but it should get the point across.

EnumField.razor

@using Company.Domain.Localization
@inherits FormFieldBase
@typeparam TEnum where TEnum : struct, Enum

<Field Horizontal="Horizontal">
    @if (!HideLabel)
    {
        <FieldLabel RequiredIndicator="IsRequired"
                    ColumnSize="DynamicColumnSize">
            @Label
        </FieldLabel>
    }
    <FieldBody ColumnSize="DynamicColumnSize">
        <Select Disabled=Disabled
                SelectedValue="Value"
                SelectedValueChanged="ValueChanged"
                TValue="TEnum">
            @foreach (var value in AvailableValues)
            {
                <SelectItem Value="@value">@L[value.ToResourceName()]</SelectItem>
            }
        </Select>
    </FieldBody>
</Field>

And it’s backing file, EnumField.razor.cs (Code behind, is what they call it, instead of using a massive code{} block)

using Microsoft.AspNetCore.Components;
using System.Linq.Expressions;
using Blazorise;

namespace Company.Blazor.Components.Form;

public partial class EnumField<TEnum>
    where TEnum : struct, Enum
{
    [Parameter]
    public bool IsRequired { get; set; }
    
    [Parameter]
    public IEnumerable<TEnum> AvailableValues { get; set; } = Enum.GetValues<TEnum>();

    [Parameter]
    public bool HideLabel { get; set; }
    
    [Parameter]
    public bool Horizontal { get; set; }

    [Parameter]
    [EditorRequired]
    public TEnum Value { get; set; }

    [Parameter]
    public EventCallback<TEnum> ValueChanged { get; set; }

    [Parameter]
    public Expression<Func<TEnum>>? ValueExpression { get; set; }

    private IFluentColumn DynamicColumnSize => Horizontal ? ColumnSize.Is6 : ColumnSize.Is12;
}

In this example, the private IFluentColumn DynamicColumnSize will obviously be marked as “unused” where the visual studio variant of the SonarCube plugin does not.

In reality it is used, in the FieldLabel parameters to fill our a ColumnSize on the fly.

The visual studio version of the plugin does not mix it up, or ignores it just fine, i am not sure which it is, but at least i do not get false positives for unused variables

I hope this helps!