[BUG] "S1116: Remove this empty statement" false positive when using C# 12 collection expressions

I’m using C# 12 collection expressions to pass in a collection of items to a constructor:

// Pass an empty collection as an argument
var cache = new CustomFormatCache([]);

The primary constructor signature is:

public class CustomFormatCache(IEnumerable<TrashIdMapping> mappings) {...}

The analysis warning I get puts a yellow-squiggly under the semicolon, as shown in the screenshot below:

image

My expectation is that this warning is not presented, because this is clearly not an empty statement. I suspect the SonarLint plugin doesn’t know how to parse collection expressions.

  • Plugin version: 10.0.1.77000
  • Rider version: 2023.3 RC 1

Hi @voidpointer!
We support .Net 8 and C# 12 syntax starting from version 9.3 of the analyzer.
SonarLint for IntelliJ recently embedded this version on release 10.1.0.77128.

Can you please try again with the latest plugin version?

Thanks for the reply. I’m using version 10.1.1.77150 and I still see the issue.

Hi, sorry for the delay.
So I started looking into this and it appears not to be on our side.
The rule implementation is trivial and it’s raising for every EmptyStatementSyntax.
Looking at the syntax with the current Roslyn main branch on Sharplab there is no EmptyStatement that could trigger our rule. Instead, with a less recent version suddenly an EmptyStatement appears after the LocalDeclarationStatement. This might explain why the rule was raising in your instance.

Are you using the latest version of the .Net 8 SDK from November 14?

Hi @voidpointer,
A user reported a similar issue, and it appears to be fixed by updating to SonarLint version 10.2.0.77238. Can you confirm?

I apologize for the very late response! I just checked and it does appear the latest version fixes this issue. Thank you very much for your help!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.