AArnott / Validation

Method input validation and runtime checks that report errors or throw exceptions when failures are detected.
Microsoft Public License
131 stars 22 forks source link

Possible null reference argument warning? #62

Closed scottdorman closed 3 years ago

scottdorman commented 3 years ago

Given the following code

public class AuthConfiguration
{
   public string? RedirectPath { get; set; }
   public IEnumerable<string>? Scopes { get; set; }
}

internal static class AuthConfigurationValidator
{
   public static void Validate(AuthConfiguration configuration)
   {
      Requires.NotNullOrWhiteSpace(configuration.RedirectPath, nameof(configuration.RedirectPath));
      Requires.NotNullOrEmpty(configuration.Scopes, nameof(configuration.Scopes));
   }
}

Both calls to Requires generate a compiler warning:

Warning CS8604  Possible null reference argument for parameter 'value' in 'void Requires.NotNullOrWhiteSpace(string value, string? parameterName)'.
Warning CS8604  Possible null reference argument for parameter 'values' in 'void Requires.NotNullOrEmpty<string>(IEnumerable<string> values, string? parameterName)'.

If the signatures for those methods are changed so that the values parameter is marked as nullable, then this warning should go away.

Is this a valid scenario or am I missing something about how the nullability rules work?

AArnott commented 3 years ago

Interesting topic, @scottdorman. Please review https://github.com/microsoft/vs-validation/pull/37 (expand the resolved discussions there) to see why we settled on this.