Is that possible to annotate a method NotNull in Rider with a non-invasive way

38 Views Asked by At

When I using pessimistic value analysis mode, I got hundreds of null related warning. I want to remove them in a non-invasive way.

For example, there is a method in my source code, and I want to annotate that its return value is not null. But, for some reason, I can't using [NotNull] annotation to do that. Actually, I can't introduce any ide related things in code, like annotation or comment (to disable warning once).

    public string Method()
    {
        return "";
    }

External annotations can do this for compiled assembly, but it does not work for source code.

Can I do the same thing for source code by configuring rider, or invoke some API via a plugin.

1

There are 1 best solutions below

0
On

I can't using [NotNull] annotation to do that

The whole point of Rider's own nullability analysis and it's pessimistic mode is to use nullability annotations to guide the analysis when checking the code between method boundaries. Pessimistic mode is designed to be used with heavily-annotated code, otherwise you'll get tons of false-positive warnings, there is not other way to use the Rider's nullability analysis.

To keep the code clean I suggest you to use the compiler's own nullability analysis - Nullable Reference Types (NRT) feature from C# 8.0. Enabling NRT for project will mark all the APIs as non-nullable by default (this is much more common) and you only need to use the ? symbol to annotate nullable parameters.