Home

Awesome

Nullable Extended

Build Status NuGet Status

Roslyn Tools and Analyzers to improve the experience when coding with Nullable Reference Types.

Nullable reference types are a great feature introduced with C# 8.0. It really helps in writing better code.

This project helps to improve the experience when coding with nullable reference types.

Managing Null-Forgiving Operators

Sometimes it is necessary to use the null-forgiving operator "!" to suppress a false warning. However using too many of them will render the value of nullable checks almost useless.

It's hard to find the null forgiving operators in code, because the ! symbol is so unobtrusive, and has multiple meanings in code.

Another obstacle is that after some time you or your colleagues won't remember why you have added a null-forgiving operator, or if its usage has been validated, so you will have to look at the code again and again.

This toolset will assist you in managing these problems. It will detect and show occurrences of the null-forgiving operator and encourage you to annotate them with a justification comment, so you and your colleagues will instantly know why this usage of the null-forgiving operator is required.

The Visual Studio Extension will show you all occurrences in a list, while the analyzer will generate a warning for every occurrence that is not annotated with a justification comment, and provides a code fix to add the scaffold for the justification comment.

Justification comments are one or more single line comments above the statement or declaration that start with \\ ! , followed by some justification text:

// ! This is only needed to demo the feature
var text = value1!.ToString();

There are two tools available:

Visual Studio Extension

The Visual Studio Extension lists all occurrences, categorizes them, and even detects those that are still present in code but no longer needed.

Occurrences are grouped into three categories, to reflect their different contexts:

e.g. general usages can be mostly avoided by cleaning up the code, while inside lambda expressions they are often unavoidable

Installation

Install the extension from the Visual Studio Marketplace or from the Releases page. The latest CI build is available from the Open VSIX Gallery

Usage

Start the tool window from the tools menu:

menu

In the tool window you can see all occurrences of the null-forgiving operator in your solution and their justification comments. It also shows if the null-forgiving operator is required to suppress nullable warnings. If the "Required" column is not checked, it is a hint that the null-forgiving operator might be obsolete and can probably be removed.

window


Roslyn Analyzer

Installation

Simply install the NuGet Package in your projects.

Show unjustified occurrences of the null-forgiving operator as warnings

The analyzer will generate a warning for every unjustified occurrence of the null-forgiving operator. Depending on the context the code will be NX0001 to NX0003.

A warning will be shown for unjustified occurrences:

warning

The warning will disappear after a justification comment has been added:

no_warning

You can easily configure the warning levels via you editor.config file.