diff options
author | Tlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com> | 2022-08-15 23:20:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-15 23:20:00 +0300 |
commit | 6b5bc03dbc6ca0553fac7dd6f36feefe09924bb8 (patch) | |
tree | f6aac84691252293bad127698e108b4b1ddbcb0a | |
parent | c710e8af4224a3d0d2975ba5c2b09f0398ee2383 (diff) |
Suppress warning in instance fields if the instance constructors are annotated with Requires (#2973)
-rw-r--r-- | src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs | 9 | ||||
-rw-r--r-- | test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs | 17 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs index f47097cc4..c4504332e 100644 --- a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs +++ b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs @@ -72,6 +72,15 @@ namespace ILLink.RoslynAnalyzer if (checkAssociatedSymbol && member is IMethodSymbol { AssociatedSymbol: { } associated } && associated.HasAttribute (requiresAttribute)) return true; + // When using instance fields suppress the warning if the constructor has already the Requires annotation + if (member is IFieldSymbol field && !field.IsStatic) { + foreach (var constructor in field.ContainingType.InstanceConstructors) { + if (!constructor.HasAttribute (requiresAttribute)) + return false; + } + return true; + } + return false; } } diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs index a0ecc2e51..44de82e7f 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs @@ -25,6 +25,7 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability TestRequiresOnBaseButNotOnDerived (); TestRequiresOnDerivedButNotOnBase (); TestRequiresOnBaseAndDerived (); + TestInstanceFieldSuppression (); TestSuppressionsOnClass (); TestStaticMethodOnRequiresTypeSuppressedByRequiresOnMethod (); TestStaticConstructorCalls (); @@ -246,6 +247,22 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability var _ = instance.field; } + public class ClassWithInstanceFieldWhichInitsDangerousClass + { + private ClassWithRequires _instanceField = new ClassWithRequires (); + + [RequiresUnreferencedCode ("Calling the constructor is dangerous")] + [RequiresDynamicCode ("Calling the constructor is dangerous")] + public ClassWithInstanceFieldWhichInitsDangerousClass () { } + } + + [ExpectedWarning ("IL2026", "Calling the constructor is dangerous")] + [ExpectedWarning ("IL3050", "Calling the constructor is dangerous", ProducedBy = ProducedBy.Analyzer)] + static void TestInstanceFieldSuppression () + { + _ = new ClassWithInstanceFieldWhichInitsDangerousClass (); + } + [RequiresUnreferencedCode ("Message for --StaticCtorTriggeredByMethodCall2--")] [RequiresDynamicCode ("Message for --StaticCtorTriggeredByMethodCall2--")] class StaticCtorTriggeredByMethodCall2 |