question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullable warning isn't emitted when passing default to a struct parameter

See original GitHub issue

Repro:

using System;
#nullable enable

public class Person
{
    public string Name { get; }
    
    public Person(string name) =>
        Name = name ?? throw new ArgumentNullException(nameof(name));
}

public struct MyStruct
{
    public Person person;
}

public static class Program
{
    public static void Foo(MyStruct s)
    {
        Console.WriteLine(s.person.Name);
    }
    
    public static void Main() => Foo(default); // Expected warning here. Because MyStruct contains a non-nullable reference type.
}

The previous example throws NullReferenceException.

I think any struct containing a non-nullable reference type should have a warning when it takes the value “default”.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

0reactions
Youssef1313commented, Oct 13, 2020

The current behavior is “By Design”.

I wouldn’t classify this as “blocked” because generally I associate that with bug fixes or design decisions. Essentially I internalize “blocked” as “yes this is a bug but we can’t make progress due to a related work item or decision from LDM”.

In this particular case though the language is operating as designed. The use of default with struct is a known hole in the C# nullable analysis, similar to null in array elements. The referenced csharplang issue isn’t a proposal to change this design. Rather it’s a proposal to add a new feature which users could then take advantage of to potentially work around this hole.

That’s clear enough. Let me close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong compiler warning when comparing struct to null
I discovered this error independently while implementing lifted operator behaviour in Roslyn, and I fixed it in Roslyn before I left.
Read more >
Nullable reference types
A reference isn't supposed to be null. The default state of a nonnullable reference variable is not-null. The compiler enforces rules that ...
Read more >
C# Nullable - Usage with Generics #3060
So in a project or code block that enables the C# nullable feature, ... warning: passing a nullable value to a non-nullable parameter....
Read more >
Digging Into Nullable Reference Types in C#
By enabling Nullable Reference Types, all reference types (by default) do not support Null unless you use the define them with the ...
Read more >
New features - Manual
Attempting to use a void function's return value simply evaluates to null , with no warnings emitted. The reason for this is because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found