Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Compiler message validate parameter is non-null before using #38

Closed
StevenTCramer opened this issue Nov 23, 2019 · 3 comments
Closed

Compiler message validate parameter is non-null before using #38

StevenTCramer opened this issue Nov 23, 2019 · 3 comments
Labels

Comments

@StevenTCramer
Copy link

I replace the following:

if (aNext is null)
{
  throw new ArgumentNullException(nameof(aNext));
}

with:

 Guard.Argument(aNext, nameof(aNext)).NotNull();

But I get the following message:

image

CA1062 In externally visible method 'Task MyBehavior<TRequest, TResponse>.Handle(TRequest aRequest, CancellationToken aCancellationToken, RequestHandlerDelegate aNext)', validate parameter 'aNext' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code econdition asserting non-null argument.

It's late at night so maybe I'm just sleepy and don't understand?

@StevenTCramer
Copy link
Author

So this is determined by dotnet code quality analyzier FxCop.
On can configure these in .editorconfig file.
https://github.com/dotnet/roslyn-analyzers/blob/master/docs/Analyzer%20Configuration.md#null-check-validation-methods

dotnet_code_quality.null_check_validation_methods = NotNull

But adding NotNull there does still not get it to go away. :(

@safakgur
Copy link
Owner

safakgur commented Dec 7, 2019

Hi Steven, sorry for the late response.

The actual method you pass aNext is Argument, you then pass the initialized ArgumentInfo<T> object to NotNull. So using "Argument" instead of "NotNull" may work. It would, however, consider every guard as a null guard, making the analysis somewhat less useful.

I'm not sure how I can statically prove to FxCop that the argument can't be null if the NotNull method returns (instead of throwing) on my end. Using Code Contracts may have worked but it's dead for some time now. I for one really wish it had taken off, I probably wouldn't develop Guard at the first place :/

I believe using C# 8's nullable reference types are the official way to go now. I understand it may not be feasible for you to just move your whole project to C# 8, so the best compromise I can think of is to use dotnet_code_quality.null_check_validation_methods = Argument if it works.

I'm open to suggestions and as always, thank you for using Guard.

@safakgur
Copy link
Owner

safakgur commented Jan 9, 2020

Closing this issue as it has been inactive for 46 days. Feel free to re-open if needed.

@safakgur safakgur closed this as completed Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants