microsoft / Microsoft.Unity.Analyzers

Roslyn analyzers for Unity game developers
MIT License
672 stars 71 forks source link

UNT0014 generics problem with interfaces #349

Closed bdovaz closed 2 weeks ago

bdovaz commented 3 weeks ago

Bug description

In the documentation of this rule it clearly states that if you pass an interface it will not fail and it is so, but if you do it with generics it will fail.

To Reproduce

Test<IMyInterface>();

void Test<T>() where T : class
{
  gameObject.GetComponents<T>(); // Issues UNT0014
}

Expected behavior

It should not report a problem.

sailro commented 2 weeks ago

cc @jbevain

You are right. But this is the typical case of generics where we cannot use type-constraints provided directly for GetComponent<T> and where we should infer effective type based on the usage: Test<IMyInterface>. This is not what we are doing for now (for simplicity and performance).

So, for consistency, I refactored the diagnostic with the following rule: