S3604 misunderstands primary constructors in C#

Operating system: Windows 10
SonarLint plugin version: 250.944
Programming language you’re coding in: C# latest & .Net 8 preview 6
Is connected mode used: No

IDE0290 suggests changing the following to use a primary constructor:

	public sealed class Data : IParseSegment
	{
		private readonly int _length;

		public readonly int Start;

		public int Length => _length & int.MaxValue;

		public Data(int start, int end)
		{
			Start = start;
			_length = end - start + 1;
		}
	}

Here’s the updated version:

	public sealed class Data(int start, int end) : IParseSegment
	{
		private readonly int _length = end - start + 1;

		public readonly int Start = start;

		public int Length => _length & int.MaxValue;
	}

Sonarlint wants to move the two member initializers = end - start + 1 and = start. This is obviously incorrect, and doing so triggers CS9113.

Hi,

Welcome to the community!

Could you re-check what version of SonarLint you’re using? It looks like 7.0.0.74072 is the latest.

 
:sweat_smile:
Ann

Hi, thank you! Not sure where I saw that number. I have 7.0.0.74072.

I noticed it understands primary constructors on struct but not class.

Hi,

Thanks for the confirmation. I’ve flagged this for the relevant folks.

 
Ann

1 Like

Hi @IRC. We created a Github issue for the future fix of the false positive. You can follow it if you want to keep track of the status of the fix.

2 Likes