dotnet / roslyn-analyzers

MIT License
1.56k stars 462 forks source link

IDE0028 rule breaking library EBinding (Collection initialization can be simplified) #7249

Open jhm-ciberman opened 4 months ago

jhm-ciberman commented 4 months ago

Analyzer

Diagnostic ID: IDE0028: Use collection initializers or expressions

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0 (Also reproduced with the built in analyzers in the Net 8 SDK)

Describe the bug

When I use the library EBind that uses collection initializers as part of it's syntax, the roslyn analizers recomend me to simplify the collection initialization:

this._bindings = new EBinding()
{
    () => this.MyProperty == viewModel.MyProperty,
    () => this.MyOtherProperty == GetMyOtherProperty(viewModel.MyOtherProperty),
};

// EBinding is declared in the library as:
public class EBinding : IEBinding, IDisposable, IReadOnlyCollection<IEBinding>, IEnumerable<IEBinding>, IEnumerable { ... }

When the fixer is applied, the code looks like this:

this._bindings =
[
    () => this.MyProperty == viewModel.MyProperty,
    () => this.MyOtherProperty == GetMyOtherProperty(viewModel.MyOtherProperty),
];

And both lines inside the collection give me this error:

Cannot convert lambda expression to type 'IEBinding' because it is not a delegate typeCS1660

Steps To Reproduce

I am not 100% sure why this is a breaking change as I am not still super familliar with the new collection initialization.

But I think applying the rule breaks the build because the EBinding class uses multiple Adds with different signatures:

public void Add(BindFlag nextFlag)
public void Add(IEBinding binding)
public void Add(Expression<Action> specification, [CallerLineNumber] int sourceLineNumber = 0)
public void Add<T>((T target, string eventName, Action action) eventBindElements, [CallerLineNumber] int sourceLineNumber = 0)
public void Add<T>((T target, string eventName, ICommand command) eventBindElements, [CallerLineNumber] int sourceLineNumber = 0)

But I am not sure.

Expected behavior

Either:

Actual behavior

Cannot convert lambda expression to type 'IEBinding' because it is not a delegate typeCS1660

Additional context