Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Instance fields requires warnings should be suppressed if the constructor is annotated #2970

Closed

Description

@tlakollo

Given the following scenario:

[Requires]
public class SomeDangerousClass{}

public class AnotherClass {
    // Warning is being produced here
    private SomeDangerousClass _instanceField = new SomeDangerousClass();
    [Requires]
    public AnotherClass () {}
}

The instance field will generate a warning since SomeDangerousClass is annotated with Requires. Currently, the workaround is to move the initialization explicitly to the constructor

[Requires]
public class SomeDangerousClass{}

public class AnotherClass {
    // Warning is being produced here
    private SomeDangerousClass _instanceField;
    [Requires]
    public AnotherClass () {
        _instanceField = new SomeDangerousClass()
    }
}

Then the whole suppression system will look for the parent and suppress the warning. The proposal is that the suppression system should be modified in instance fields to take into account the constructor of the class and their annotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions