- 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!
Hey, @Vince.scholt, to give you an update on the investigation. We currently exclude razor files because we do not support them in Rider. However, since this file is a partial class, it is considered a standard C# one and not excluded. We don’t have an easy way today to handle this case; until we fully support razor in Rider, we can either wholly exclude these partial files (that could likely hide some relevant C# issues) or include them (but you could end up with false positives as in your case). This is still in discussion internally, and I will keep you updated if there’s anything new.
Hey, good to hear you are still working on this.
I’ll give you my two cents:
For me, a false positive is way more annoying to work with, as it keeps pestering me about issues that do not actually exist.
If only full include or full exclude are the options, i would say fully exclude, keeping in mind that these are “razor backing files” they should not contain any business logic, or process massive amounts of things. And thus i rarely had any sonar feedback on those files in the first place, while i was still working in VS2022.
If you have the option of “exclude these 4 rules for razor backing .cs” i would highly suggest to add that for the time being. Exclude just the “unused usings” warnings for these files, so the amount of false positives, if any, is minimized. (as if i added ignore tags to those files manually, if you catch my drift) though i can imagine that is harder to communicate, but as a user i would expect it as part of not supporting razor files i think.
Let me know what you internally decide on, and if there is any, what the roadmap is to razor support in RiderIDE, as it would be nice 
Thank you for your valuable feedback. Avoiding false positives is definitely one of our priorities. For the time being, we are heading towards disabling analysis on such partial classes. I created this ticket for reference.
Hey @Vince.scholt, we have just released a new version of SonarQube for IntelliJ. These files should not be analyzed anymore until we provide proper support for them. Thank you again for your help on this. If you enjoy SonarQube for IntelliJ, feel free to leave a review on our marketplace page; it’s always appreciated!
1 Like
I got the notification this morning, thanks for getting this sorted this quickly! I’ll be looking forward to full Razor page support in the further future.
1 Like