diff --git a/src/Microsoft.Unity.Analyzers.Tests/RequireComponentTests.cs b/src/Microsoft.Unity.Analyzers.Tests/RequireComponentTests.cs index 01598f86..0a6c4cec 100644 --- a/src/Microsoft.Unity.Analyzers.Tests/RequireComponentTests.cs +++ b/src/Microsoft.Unity.Analyzers.Tests/RequireComponentTests.cs @@ -310,4 +310,21 @@ void Start() await VerifyCSharpFixAsync(test, fixedTest); } + + [Fact] + public async Task GenericTypeParameter() + { + const string test = @" +using UnityEngine; + +public class PlayerScript : MonoBehaviour +{ + void Foo() + { + var bar = GetComponent(); + } +}"; + + await VerifyCSharpDiagnosticAsync(test); + } } diff --git a/src/Microsoft.Unity.Analyzers/RequireComponent.cs b/src/Microsoft.Unity.Analyzers/RequireComponent.cs index 9a6e2dcd..b9686a09 100644 --- a/src/Microsoft.Unity.Analyzers/RequireComponent.cs +++ b/src/Microsoft.Unity.Analyzers/RequireComponent.cs @@ -66,6 +66,9 @@ private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context) return; var componentType = method.TypeArguments.First(); // Checked by IsGenericGetComponent + if (componentType.TypeKind == TypeKind.TypeParameter) + return; + if (IsTypeAlreadyRequired(containerType, componentType)) return;