feO2x / Light.GuardClauses

A lightweight .NET library for expressive Guard Clauses.
MIT License
85 stars 8 forks source link

Provide ValidatedNotNullAttribute to all assertions that check for null #74

Closed WizX20 closed 3 years ago

WizX20 commented 3 years ago

I, I would like to use the .editorconfig to point to light guard for null checks. But I can't seem to get it to work.

See https://github.com/dotnet/roslyn-analyzers/blob/master/docs/Analyzer%20Configuration.md#null-check-validation-methods

I have tried

feO2x commented 3 years ago

Hey @WizX20, unfortunately, I have never used .editorconfig so far, so I can't give you a definitive answer. I will try to recreate this scenario tomorrow. I had a quick look at the IDs that you provided and I think, you are missing the Check class: Light.GuardClauses.Check.MustNotBeNull``(``0, System.String, System.String) should be the fully qualified ID, according to the symbol's documentation ID format.

You might also check whether the simplified versions MustNotBeNull or Light.GuardClauses.Check.MustNotBeNull work, too. Could you please tell me if that worked for you?

WizX20 commented 3 years ago

Hi @feO2x I tried your suggestions, but it does not seem to work. Might also be an issue with the analyzer... I also tried dotnet_code_quality.CA1062.null_check_validation_methods = M:Light.GuardClauses.Check.MustNotBeNull(System.Object,System.String,System.String)

Also, I think spaces are not allowed (the symbol resolver is sensitive to formatting spaces), so your first suggestion would become: Light.GuardClauses.Check.MustNotBeNull``(``0,System.String,System.String)

WizX20 commented 3 years ago

I did some searching, and perhaps it might help adding a ValidatedNotNull attribute to the MustNotBeNull methods.

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class ValidatedNotNullAttribute : Attribute {}

public static void ThrowIfNull([ValidatedNotNull] this object value, string parameterName)
{
    if (value == null)
    {
        throw new ArgumentNullException(parameterName);
    }
}
feO2x commented 3 years ago

Thanks for your investigations. I finally have some time now to reconstruct this issue for myself, and you are right, the best way to solve this problem is to apply the ValidatedNotNullAttribute. I'll try to implement this in the upcoming days. Will be part of version 8.2.0.

feO2x commented 3 years ago

I've just released v9.0.0 of Light.GuardClauses which has support for ValidatedNotNullAttribute on all corresponding assertions. Happy St. Nick's Day!

If you have further questions, please feel free to open a new issue or re-open this one.

WizX20 commented 3 years ago

@feO2x Yes one question, where is the "buy me a beer" button?

feO2x commented 3 years ago

Nice question! I have enough beer, please donate money to a charity, e.g. your local Doctors Without Borders.

feO2x commented 3 years ago

Or maybe you could somehow support the personnel in your local hospital - they probably have a pretty stressful job now that more and more intensive care beds are filled with COVID patients.