Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Skip nullable parameters when doing GuardClauseAssertion #1328

Open
zvirja opened this issue Feb 3, 2022 · 4 comments
Open

✨ Skip nullable parameters when doing GuardClauseAssertion #1328

zvirja opened this issue Feb 3, 2022 · 4 comments
Labels
feature request An idea for a change, aimed at impoving quality of life Idioms An issue related to idiomatic assertions
Milestone

Comments

@zvirja
Copy link
Member

zvirja commented Feb 3, 2022

Description

With the most recent Reflection API it's possible to retrieve nullability info about code. So we can use that nullability info in GuardClauseAssertion to not assert that nullable parameters must be null.

Example

#nullable enable

public class Sut
{
    public void Method(string arg1, string? arg2) { }
}

We can be smart about arg2 and skip null verification for it, as we know it can be null.

@Kralizek
Copy link
Contributor

Kralizek commented Feb 3, 2022

Would it also make sense/be possible for optional parameters?

@zvirja
Copy link
Member Author

zvirja commented Feb 3, 2022

Wouldn't it be covered automatically? If optional parameter is nullable - we would skip it because it's nullable. If it's not nullable - well, then it should be guarded even if you supply the default value.

All of it makes sense ofc only if you have null guards at all and do not rely purely on compiler nullability checks 😉

@rvazqueziglesias
Copy link

rvazqueziglesias commented Nov 28, 2023

I have just come across this topic right now as I was indeed trying to check the code and verify if there was some way of skipping null verification for these parameters.

The problem I saw is that Constructor.GetParameters(); in https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixture/Kernel/ConstructorMethod.cs doesn't return a type of Nullable < T> for reference types.
That works if you're dealing with value types (for example Guid) but doesn't work when dealing with reference types.
That being said, optional parameters can be potentially excluded by reading the properties HasDefaultValue and DefaultValue (and compare against null) that are returned for each parameter with the GetParameters() method

@aivascu aivascu added this to the v6.0 milestone Nov 28, 2023
@aivascu
Copy link
Member

aivascu commented Nov 29, 2023

I think the optional parameters change can go into the next version, however the nullable reference type change will have to go into v6 since only after .NET 6 the reflection API makes it possible to reliably detect these types.

@aivascu aivascu added feature request An idea for a change, aimed at impoving quality of life Idioms An issue related to idiomatic assertions labels Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request An idea for a change, aimed at impoving quality of life Idioms An issue related to idiomatic assertions
Projects
None yet
Development

No branches or pull requests

4 participants