roslyn: Collection expressions are suggested for types where it doesn't make sense, and their error messages show inconsistent types

Version Used:

  • VS 17.9.6 with SDK 8.0.204
  • VS 17.11 Preview 1 with SDK 8.0.300
  • VS 17.11 Preview 1 with SDK 9.0.100-preview.4.24267.66
  • sharplab.io “Default”

Steps to Reproduce: See comments in the sample:

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp1;

internal class Program
{
    private readonly InlineValidator<DateTime> _validator;

    public Program()
    {
        // This shows "IDE0028 Collection initialization can be simplified", but only if the Add method below exists.
        // Should it really show this message? I think using a collection expression here is very weird.
        _validator = new InlineValidator<DateTime>();

        // Applying the code fix results in the following working, but weird, code.
        // This still compiles if the Add method below does not exist.
        _validator = [];

        // This calls the Add method below, but should it?
        // The type of the parameter is something completely different than the type of the IEnumerable element.
        // If the Add method below does not exist, then this shows error "CS1061 'InlineValidator<DateTime>' does not
        // contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type
        // 'InlineValidator<DateTime>' could be found".
        // This makes even less sense. Why does it try to find an Add method with parameter type
        // InlineValidator<DateTime>? Shouldn't it only be parameter type DateTime or IValidationRule (depending on
        // whether it should be the generic type of the class itself, or the IEnumerable type)?
        _validator = [null!];

        // This shows error "CS0029 Cannot implicitly convert type 'int' to 'ConsoleApp1.IValidationRule'.
        // This is different from the line above (when the Add method exists). There the compiler passed null as the
        // Add method parameter type (Func). But now it tries to convert it to the IEnumerable element type? Why?
        //_validator = [1];
    }
}

public class InlineValidator<T> : IEnumerable<IValidationRule>
{
    public void Add(Func<int, bool> someFunc)
    {
        someFunc(1);
    }

    public IEnumerator<IValidationRule> GetEnumerator()
    {
        throw new NotImplementedException();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

public interface IValidationRule;

Sample in SharpLab

My actual project uses https://github.com/FluentValidation/FluentValidation. The sample contains simplified versions of the FluentValidation classes.

Diagnostic Id: Initially "IDE0028: Collection initialization can be simplified". But the following error messages are shown if you modify the sample as explained in the comments:

  • "CS1061: 'InlineValidator<DateTime>' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'InlineValidator<DateTime>' could be found"
  • "CS0029: Cannot implicitly convert type 'int' to 'ConsoleApp1.IValidationRule"

Expected Behavior: I don’t expect to see "IDE0028: Collection initialization can be simplified" for new InlineValidator<DateTime>(). I think this should only be shown if the parameter type of the Add method matches the collection type. I expect consistant parameter/element types shown in the error messages if you modify the sample as explained in the comments.

Actual Behavior: See comments in the sample code.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 34 (17 by maintainers)

Most upvoted comments

I guess #79157 doesn’t fix anything of my issue here, right?

Correct. @elgonzo was reporting a different issue.

As I mentioned above, this issue is being kept open for this concern:

But what about the wrong types in the error messages?

This is up to the compiler team to decide if they want to fix the error messages.

Thanks. Created fix for your issue.

PLease file specific issues with a self contained repro, and we can assess appropriately. Thanks 😃

Done. I link it here, so it hopefully makes it easier to flag as duplicate if necessary: https://github.com/dotnet/roslyn/issues/79156

Can I request that somewhere?

Yes. This issue counts as such a request 😃

Currently, you can write a DiagnosticSuppressor for this as well.

I’ll reopen and let the compiler team decide on that.