Skip to content

Delegates with mutable parameters are treated as mutable #37

@mwelsh1118

Description

@mwelsh1118

We now have a way to mark arbitrary types immutable via configuration. This allows us to configure System.Action`1, for example, as immutable without impacting other consumers of the analyzers. However, we're still experiencing a fair amount of pain due to the generic type argument checking.

For example this is a violation:

[Immutable]
class Test
{
    public Action<NotImmutable> Action { get; }
}

Even though Action`1 is treated as immutable, its generic type argument (NotImmutable) is not, and so it produces an IMM004.

I know that delegates are treated as mutable as a conscious design choice, but might this be reconsidered? Another corner case (like state-capturing lambdas) that is not covered would be:

[Immutable]
class Test
{
    private readonly object _random;

    public Test(object random)
    {
        _random = random;
    }
    public int Value => ((System.Random)_random).Next(0, 10);
}

I'm not arguing that this should be caught -- simply that there will always be ways to subvert the rules if someone is truly determined to do so.

Alternately, could we add configuration to control the treatment of delegates? The Microsoft analyzers now support options defined in an .editorconfig. Unfortunately their implementation is internal, so cannot be called directly. But the code is open-source and could be incorporated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions