sonarlint-visualstudio icon indicating copy to clipboard operation
sonarlint-visualstudio copied to clipboard

Fix S2259 FP: Null-conditional operator + LINQ check

Open ivorobkalo opened this issue 1 year ago • 4 comments

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

ivorobkalo avatar Jul 03 '23 08:07 ivorobkalo

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.

sebastien-marichal avatar Jul 04 '23 09:07 sebastien-marichal

Thanks @sebastien-marichal. I'm using the latest SonarLint extension that is available on a VS marketplace though - will wait for the updated version!

ivorobkalo avatar Jul 05 '23 14:07 ivorobkalo

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.

sebastien-marichal avatar Jul 05 '23 15:07 sebastien-marichal

@ivorobkalo This doesn't repro in SLVS v7.2, which embeds Sonar C# v9.7.

image

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.

duncanp-sonar avatar Aug 18 '23 08:08 duncanp-sonar

I am no longer able to reproduce it with SonarLint 8.1: image