MemberNotNull on parameters and returns #4563
Replies: 6 comments
-
In my case, I needed to create an extension method to set a nullable property to something that is definitely not null public static T DoSomething<T>() where T : IMyInterface, new()
{
T myObject = new T();
myObject.NullableProperty = "Definitely not null";
return myObject;
}
...
public interface IMyInterface
{
string? NullableProperty { get; set; }
} With the current implementation of static analysis I have to either check or forgive the nullability of the return value: var result = DoSomething<MyImplementation>();
var length = result.NullableProperty!.Length; or use an public static T DoSomething<T>(out string notNullResult)
where T : IMyInterface, new()
{
T myObject = new T();
myObject.NullableProperty = "Definitely not null";
notNullResult = myObject.NullableProperty;
return myObject;
}
...
var result = DoSomething<MyImplementation>(out string notNullResult);
var length = notNullResult.Length; Having an attribute to indicate that the return value's property is definitely not null would make this a bit more elegant. However, I'm not sure how it would work with a generic method; maybe something similar to the [return: NotNullIfNotNull(nameof(T.NullableProperty))]
public static T DoSomething<T>() where T : IMyInterface, new()
{
T myObject = new T();
myObject.NullableProperty = "Definitely not null";
return myObject;
} I had asked a question about this on stackoverflow here. |
Beta Was this translation helpful? Give feedback.
-
This would be very useful to me as well, I have many Entity Framework data loading methods that are guaranteed to fill in nullable properties, but there's no way to express that some properties are non-null. I'm constantly having to use ! on properties that I know are non-null, which seems to defeat a much of the point of nullability analysis. Example: class Person
{
public int Id { get; private set; }
public List<Address>? Addresses { get; private set; }
}
public static async Task<Person> LoadPersonFull(int personId)
{
....
} I'd like to be able to express that, in the Person returned by this static method, Person.Addresses is non-null. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to call a Validate(complexObject) function and after it returns the compiler would know that complexObject.Foo and complexObject.Bar are guaranteed not null (since I already checked in the Validate function) |
Beta Was this translation helpful? Give feedback.
-
Another question on SO from me. This would be a very handy feature and it would make static analysis more robust. |
Beta Was this translation helpful? Give feedback.
-
I also would benefit from this. Most of my use cases echo those previously mentioned. Creation Methods[return: MemberNotNull(nameof(MethodDefinition.CilMethodBody))]
public static MethodDefinition CreateManaged()
{
MethodDefinition managedMethod = new();
managedMethod.CilMethodBody = new(managedMethod);
return managedMethod;
} Extension Methods for ValidationIt is extremely common for me to write extension methods that validate members of the public static bool IsManaged([MemberNotNullWhen(true, nameof(MethodDefinition.CilMethodBody))] this MethodDefinition method)
{
return method.CilMethodBody is not null;
} However, if extensions come in C#13, that would most likely offer a workaround. |
Beta Was this translation helpful? Give feedback.
-
I was looking for this just now - I am returning an object in a test setup method and it will always have a nullable property set - be nice to tell the caller that! |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/1b4f6ac09fd0f29593cc8bf96244e2b16e2509cd/meetings/2020/LDM-2020-02-05.md#nullability
Currently, MemberNotNull/MemberNotNullWhen can be applied and checked only on member methods:
However there are scenarios, especially when you don't own the type that you want to validate, where it would be useful to have the attribute on parameters or returns:
The LDM notes from February 2020 state:
In this discussion I'd like to gather any evidence that users are seeing significant limitations from this, and would benefit from the parameter and return type support.
Beta Was this translation helpful? Give feedback.
All reactions