lecaillon / Conan.Plugin.NullGuard

Conan plugin adds null guard code for all methods and constructors parameters preceded by a [NonNull] attribute.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conan.Plugin.NullGuard Build status

Conan.Plugin.NullGuard adds null guard code for all methods and constructors parameters preceded by any [NonNull] attribute.

It is based on Conan, a lightweight fork of the .NET Compiler Platform ("Roslyn") by adding a compiler plugin infrastructure. These plugins can be deployed and installed as regular Diagnostic Analyzers.

Notice

This plugin is an alpha version, whose purpose is to see where the very promising Conan compiler plugin infrastructure can bring us.

Getting started

  1. Add the package Conan.Net.Compilers to your project: This will make the Conan compiler as the default CSharp/VB compiler and replace the default Roslyn compiler (This package works for both Full framework and Core framework unlike the Roslyn packages)
  2. Add the package Conan.Plugin.NullGuard only available on AppVeyor for now.

Example

  1. Add a [NonNull] attribute to any constructor or method reference type parameter you want to check.
public class Person
{
  private readonly string _name;
  private readonly int _age;

  public Person([NonNull] string name, int age)
  {
    _name = name;
    _age = age;
  }
}
  1. At build time the Conan Compiler will automatically insert at the beginning of the constructor the statement needed to ensure the parameter name is not null.
public class Person
{
  private readonly string _name;
  private readonly int _age;

  public Person([NonNull] string name, int age)
  {
    if (name == null)
    {
      throw new ArgumentNullException("name");
    }

    _name = name;
    _age = age;
  }
}
  1. You can see rewritten documents on the disk in a sub directory of the obj folder or using ILSpy. Example: \obj\Debug\netcoreapp2.1\GeneratedFiles\Microsoft.CodeAnalysis.CSharp.Analyzers\Conan.Person.cs

Licensing

Same license than Roslyn: Apache-2.0

Credits

Alexandre MUTEL aka xoofx

About

Conan plugin adds null guard code for all methods and constructors parameters preceded by a [NonNull] attribute.

License:BSD 2-Clause "Simplified" License


Languages

Language:C# 100.0%