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.

Support non-nullable value types with the InitRequiredForNotNullAttribute

See original GitHub issue

The InitRequiredForNotNullAttribute is really great, we’ve activated it in all our projects but there is one missing piece in my opinion: the non-nullable value types!

Consider this code:

using System;

class Program
{
    static void Main()
    {
        _ = new Person
        {
            Name = "Foo"
        };
    }
}

class Person
{
    public string Name { get; set; }

    public DateTime BirthDate { get; set; }
}

I’m expecting to get the CSE001 error but actually not because BirthDate is a value type.

Would that be possible to implement the same behavior as for non-nullable reference types? That would be awesome.

Thank you

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
vsilvarcommented, Sep 28, 2021

@cezarypiatek Tested this change and it’s working, but I found something I wouldn’t expect.

Given the following code:

using SmartAnalyzers.CSharpExtensions.Annotations;

[assembly: InitRequiredForNotNull]
namespace AnalyzerTest
{
    class Program
    {
        static void Main()
        {
            _ = new Person
            {
                Name = "Foo",
                Age = 18
            };
        }
    }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        //[InitOptional]
        public bool IsAdmin { get; set; } = false;
    }
}

I get CSE001 - Missing initialization for properties: - IsAdmin, which I wouldn’t expect as a default value is being set. At the very least an [InitOptional] attribute could exist to allow disabling the initialization checks for a single property.

– Edit: Thinking about this again, it’s probably only an issue when using the attribute globally in the assembly/type, as otherwise you just wouldn’t specify the attribute for the property. Anyway, I think it still makes sense to have a [InitOptional] for such cases.

1reaction
cezarypiatekcommented, Sep 29, 2021

@jmevel @vsilvar thanks for the feedback. I’ve just added [InitOptional] which allows for excluding a specific member or whole type from the mandatory init imposed on the higher level by [InitRequired] or [InitRequiredForNotNullAttribute]. Should be available in v4.1

Please let me know how do you find the current behavior. I’m not sure if CSE004: InitOnlyOptional requires default value should be reported for fields marked with [InitOptional as well. I haven’t implemented it but I can do it. What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Non-nullable reference types' default values VS ...
A Value property of type T An instance for which HasValue is true is said to be non-null. A non-null instance contains a...
Read more >
Configure non-nullable types as required · Issue #2036
In my project we do not distinguish between required and nullable: false. If a type is non-nullable, it is also required.
Read more >
Nullable reference types
This article provides an overview of nullable reference types. You'll learn how the feature provides safety against null reference ...
Read more >
C# 8.0 nullable references: NotNull
The NotNull attribute enables C#8's nullable references feature to provide more useful warnings, by helping it infer information about a ...
Read more >
Digging Into Nullable Reference Types in C#
Classes and Nullable Reference Types​​ Properties that aren't nullable are expected to be set before the end of the constructor. There are two ......
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