Repository navigation
Warn on RUC annotated ctors invoked through new() constraint in analyzer - #2254
Conversation
| if (operationContext.Operation is IObjectCreationOperation objectCreationOperation && | ||
| objectCreationOperation.Type is INamedTypeSymbol objectType && objectType.IsGenericType) { | ||
| typeParams = objectType.TypeParameters; | ||
| typeArgs = objectType.TypeArguments; |
There was a problem hiding this comment.
This only reacts to calling a constructor on a generic type.
Code like this should still warn though:
static class MyTest<T> where T : new ()
{
static void DoNothing() {}
}
void Test()
{
// IL2026
MyTest.DoNothing<ClassWithRUCOnCtor>();
}I must admit that I don't know enough about analyzers to tell what type of solution would be the best here. Linker will warn anywhere the generic is instantiated. Even cases like typeof(MyType<ClassWithRUCOnCtor>) will generate the warning.
This is obviously not that common, so it's OK to solve this later...
There was a problem hiding this comment.
I moved the analysis context used here to a SyntaxNodeAnalysisContext; this way we look for syntax nodes that represent generic names and check, for their type parameters, if these have any constructor constraints. I think this would cover up all cases. I've added the scenario you outline above (and the analyzer warns now).
| [SuppressMessage ("MicrosoftCodeAnalysisPerformance", "RS1008", | ||
| Justification = "This action is registered through a compilation start action, so that the instances which " + | ||
| "can register this operation action will not outlive a compilation's lifetime, avoiding the possibility of " + | ||
| "this data causing stale compilations to remain in memory.")] |
There was a problem hiding this comment.
Can you please explain why this action causes the warning and the one above doesn't? I don't see how they're really different...
…lyzer (dotnet#2254) * Add support in RUC analyzer for new() constraint on generics * Support `new()` constraint on types * Use SyntaxNodeAnalysisContext for constructor constraints * Lint * Update justification
…lyzer (dotnet/linker#2254) * Add support in RUC analyzer for new() constraint on generics * Support `new()` constraint on types * Use SyntaxNodeAnalysisContext for constructor constraints * Lint * Update justification Commit migrated from dotnet/linker@4f28a65
Fixes #2165.