C# S2259 (is null on at least one execution path) - false negatives

Hi everybody,

I am using the 9.9 version of sonarqube and the last version of sonarscaner on Windows.

I have a problem with this nullable condition.

In this case sonar lint targets an S2259 :

var matchingClosingInline = matchingClosingInlines.FirstOrDefault();
  if (matchingClosingInline is not null)
  {
      var siblings = matchingClosingInline.GetNextSiblings().ToArray();
      matchingClosingInline.Remove(); // <--------------- S2259 here 
  }

And in this case sonar lint nothing :

var matchingClosingInline = matchingClosingInlines.FirstOrDefault();
  if (matchingClosingInline != null)
  {
      var siblings = matchingClosingInline.GetNextSiblings().ToArray();
      matchingClosingInline.Remove(); // 
  }

I don’t understand why sonar lint make difference between is not and != in this case.

Hi,

Welcome to the community!

I guess you mention your SonarQube version - even though your report is about SonarLint - because you’re in connected mode? If not, can you provide your SonarLint flavor and version?

And what language is this. I’m sure it’s obvious to those who code in your language, but… :smiley:

 
Ann

1 Like

Hi Ann,

Yes , i am in connected mode with a sefl-host instance of SonarQube.

My langauge is C#.

Quentin

Hi Quentin,

Thanks for the details. I’ve referred this to the language experts.

 
Ann

1 Like

Hello @QuentinArx

Can you please tell us if matchingClosingInline is a nullable value type (struct)? If so, this is a known issue and will be fixed in a future version of the analyzer.

Best, Martin

1 Like

Hi @Martin_Strecker ,

No matchingClosingInline is not a nullable type.

Best, Quentin

Hello @QuentinArx

Do you see the issue in Visual Studio? If so, can you please tell which version of Visual Studio you are using.

There is one odd thing in the code sample you posted: The issues is raised on the second line in the true branch of the if. I would have expected to see the issue on the first line, as there is already an invocation taking place. Is there something missing in your example?

Best Martin

1 Like

Hi @Martin_Strecker

Yes is on Visual Studio Professional, i am on 17.5.1 of Visual Studio.

Yes, me too but the issue is really raised on this second line. There are many lines below the part of the code, but matchingClosingInline is not use.

Best Quentin

Hi @Quentin,

Can you please update to the latest version of SonarLint? We did some updates to rule S2259 recently. Please keep in mind that the version of the analyzer used by SonarLint can be different from the version of the analyzer used in the pipeline, which might explain why you see different results.

Best Martin

1 Like