Skip to content

Readonly reference field is never assigned and is used #647

@giggio

Description

@giggio

When a reference readonly field is not assigned inside the constructor or during declaration, raise an error.

This should raise a diagnostic:

class Foo
{
    private readonly object o;
    void Bar()
    {
        o.GetHashCode();
    }
}

This should not:

class Foo
{
    private readonly object o = new object();
    void Bar()
    {
        o.GetHashCode();
    }
}

This also should not:

class Foo
{
    private readonly object o;
    public Foo()
    {
        o = new object();
    }
    void Bar()
    {
        o.GetHashCode();
    }
}

Should apply to any usage, such as member access, being passed as parameter, used in any expression, etc.

There is no code fix.

Category: Design
Diagnostic id: CC0112
Severity: Error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions