How to stop Resharper from removing [CanBeNull] when typing “?” at the end of a reference type name?

Completed

I am going through the process of converting my .Net 4.8 (not Core!) projects to use the new C# 8 "nullable reference" feature.

To do so, I am changing each source code file in turn by adding #nullable enable and decorating all nullable references with a ? suffix. This allows me to convert all my code in a piecemeal fashion to use the new feature.

Now consider the following method in a source file with #nullable enable specified:

#nullable enable

[CanBeNull] public static string Test([CanBeNull] string arg)
{
    return arg;
}

If I type a '?' after either of the string declarations (in order to use the new C# 8 "nullable" feature), Resharper removes the [CanBeNull] attribute:

public static string? Test(string? arg)
{
    return arg;
}

However, if that attribute is removed then Resharper does not warn me about possible null-reference uses in other source files. (This would not be an issue if all the source files had #nullable enable specified, but because I am converting the files in a piecemeal fashion, there are many files that do not yet have #nullable enable specified, so the compiler will not warn me about possible null-reference uses - and neither will Resharper, because the [CanBeNull] was removed.)

So my question is this:

Is there a way of stopping Resharper from removing the [CanBeNull] attribute when I type a ? at the end of a reference type name?

I've searched through all the Resharper options but I haven't managed to find one for this.

0
2 comments

Hello Matthew,

 

Please try disabling typing assist for this case by unchecking ReSharper | Options | Editor | Behavior | C# | Annotate nullability on '!'/'?' after type name:

Thank you.

0

Thanks for the reply!

I also had to disable "Annotate nullability on '!'/'?' at other positions", but it's now working how I want. :)

0

Please sign in to leave a comment.