Property rules - SergeyTeplyakov/ErrorProne.NET GitHub Wiki

ErrorProne.NET has several rules for properties.

Readonly property was never assigned

C# compiler emits a warning if readonly field was never assigned:

class Foo
{
  private readonly string _s;
  //                      ~~
  // Field '_s' is never assigned to, and will always have its default value 0
}

This warning is very useful, but first, you wan't see it immediately in the IDE (WHY?!?) and second, there is no warning on the similar construct: on unassigned readonly automatic property that appeared in C# 6. So ErrorProne.NET for the resque!

class Foo
{
    public int S { get; }
    //         ~
    // Readonly property 'S' is never assigned to, and will always have its default value 0
}

The same rule is also applied for properties with private setters:

class Foo
{
  public int S {get; private set;}
  //                 ~~~~~~~~~~~
  // Property 'S' with private setter is never assigned to, and will always have its default value 0
}

One caveat here. Unfortunately, in some cases it is impossible to tell whether some property will be unchanged or not. If the property is abstract or virtual, ErrorProne.NET will give up and will never emit this warning:

class Foo
{
  public virtual int S { get; }
  // Giving up! Derived class can override a getter and provide some value!
}

Note that R# is not covering this case, and will still warn you;)