if (operationContext.Operation.Parent is IOperation operation && operation.Kind == OperationKind.NameOf)
return;
+ // Do not emit any diagnostics for constant fields - they can only have Requires attributes applied to them
+ // via the type, and in that case the attribute is guarding the access to the static ctor.
+ // But constant fields are never accessed at runtime, and thus they don't cause static ctor to run.
+ // Constant fields are always inlined by the compiler (required by the ECMA spec).
+ if (member is IFieldSymbol field && field.HasConstantValue)
+ return;
+
ISymbol containingSymbol = FindContainingSymbol (operationContext, AnalyzerDiagnosticTargets);
// Do not emit any diagnostic if caller is annotated with the attribute too.
public static void Method () { }
}
- // https://github.com/dotnet/runtime/issues/84433
- [ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
- [ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
- [ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
- [ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method))]
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method), ProducedBy = Tool.Analyzer | Tool.NativeAot)]
static void TestClassWithRequires ()
ConstClassWithRequires.Method ();
}
- // https://github.com/dotnet/runtime/issues/84433
- [ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
- [ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[RequiresUnreferencedCode (ConstClassWithRequiresUsingField.Message)]
- [ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
- [ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[RequiresDynamicCode (ConstClassWithRequiresUsingField.Message)]
class ConstClassWithRequiresUsingField
{