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

Null suppression doesn't work when the body has a null suppression #679

Closed
RenderMichael opened this issue Jan 22, 2024 · 1 comment · Fixed by #704
Closed

Null suppression doesn't work when the body has a null suppression #679

RenderMichael opened this issue Jan 22, 2024 · 1 comment · Fixed by #704
Assignees
Milestone

Comments

@RenderMichael
Copy link

A null suppression throws the null warning suppression out of whack.

One might say why is str not getting checked for null. The following is a minimal repro, the actual scenarios I've ran into are less trivial. Suffice it to say, the existence of the null-suppression operator should be evidence enough that it needs to be used sometimes.

internal static string GetString(HasString str)
{
    Assert.That(str.Inner, Is.Not.Null);
    return str.Inner; // works great
}

internal static string GetStringSuppress(HasString? str) // argument is nullable
{
    Assert.That(str!.Inner, Is.Not.Null);
    return str.Inner; // warning: possible null reference return
}

public record HasString(string? Inner);
@manfred-brands
Copy link
Member

In this case Assert.That(str?.Inner, Is.Not.Null); would have been a safer statement as it asserts both str and str.Inner.

But as you said, your real code is more complicated.

The null-suppression-operator indicates that the writer knows more about the code than the compiler can deduct.
I created a PR to ignore '!' when matching diagnostics with expressions tested with Is.Not.Null

@mikkelbu mikkelbu added this to the Release 4.1 milestone Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants