"Parameter names used into ArgumentException constructors should match an existing one" with records

This code in C# 9.0/.NET 5.0:

public record Data(string Code)
{ 
  public string Code { get; } = Code ?? throw new ArgumentNullException(nameof(Code)); 
}

Result in csharpsquid:S3928/“Parameter names used into ArgumentException constructors should match an existing one”.

The message is “Code Smell: The parameter name ‘Code’ is not declared in the argument list.” but ‘Code’ is the constructor argument used to initialise the Code property.".

I believe this to be a false positive because Code is defined in the record constructor parameters.

This code:

using System;
					
public class Program
{
	public static void Main() => new Data(null!);
}

public record Data(string Code)
{ 
	public string Code { get; } = Code ?? throw new ArgumentNullException(nameof(Code)); 
}

Results in “System.ArgumentNullException: Value cannot be null. (Parameter ‘Code’)” as expected.

2 Likes

Hi @simonvane and welcome to our community!

I can confirm the false positive, the rule does not check the record parameter list. I’ve created an issue and you can follow the progress here.

Best,
Costin

1 Like

Many thanks @costin.zaharia.

Any idea of timescale?

We are doing periodically sprints to handle bugs and fix false positives and this should be handled in one of the next sprints. Unfortunately, I cannot provide an ETA at this point.

OK, thanks.

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