sonarlint-visualstudio
sonarlint-visualstudio copied to clipboard
Fix S2259 FP: Null-conditional operator + LINQ check
Description
A rule S2259 is triggered in the next code snippet however if dataTable?.Columns.Contains("ID")
equals true
(not null
) the dataTable is guaranteed not to be null.
Repro steps
if (dataTable?.Columns.Contains("ID") == true) { dataTable.Columns["ID"].ColumnName = "Id"; // Here S2259 is triggered for the dataTable variable }
Expected behavior
The rule is not triggered
Actual behavior
The rule is triggered
Related information
- C#/VB.NET Plugins version 6.16.0.69538
- Visual Studio 2022 (17.6.4)
- MSBuild / dotnet version .Net Framework 4.7.2
- Operating System Windows 11
Hello @ivorobkalo,
Thanks for reaching out.
I noticed you are using version 6.16.0.69538, an old version of our analyzer. This FP has been fixed in version 8.46.0.54807 (issue SonarSource/sonar-dotnet#4537) and is not raising anymore since then. It should be part of SonarQube starting version 9.7.
You can upgrade to a newer version of our analyzer to fix your issue.
Thanks @sebastien-marichal. I'm using the latest SonarLint extension that is available on a VS marketplace though - will wait for the updated version!
Hello @ivorobkalo,
I now realized the version you were mentioning was the SonarLint version. Sorry for the misunderstanding.
I am transferring your issue to the sonarlint-visualstudio repository.
@ivorobkalo This doesn't repro in SLVS v7.2, which embeds Sonar C# v9.7.
Having said that, SLVS v6.16 embedded SonarC# v9.0, and according to @sebastien-marichal the above FP was fixed in SonarC# v8.46.
SLVS does not have any effect on how or when Roslyn rules are executed by Visual Studio. If a rule is raising a false positive then that is down to the rule/the interaction between Visual Studio and the rule. One way to confirm this is to uninstall SonarLint and change the C# project to reference the SonarC# NuGet package directly.
I am no longer able to reproduce it with SonarLint 8.1: