diff options
author | Sven Boemer <sbomer@gmail.com> | 2022-03-11 22:37:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-11 22:37:27 +0300 |
commit | 93eac59827cae579c0889e293b715cec93b48c62 (patch) | |
tree | 5579b73ebe91c36814e639950d8f7377eeafecbc | |
parent | 5baeb5e08b4a5fd6995e6247c7cb41b673c5ecdc (diff) |
Use attributes on lambdas in tests (#2682)
* Use attributes on lambdas in tests
* Fix formatting
* Unindent attributes
5 files changed, 102 insertions, 72 deletions
diff --git a/eng/Versions.props b/eng/Versions.props index 0effb65f4..aee5c1e42 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -21,7 +21,7 @@ <MicrosoftDotNetApiCompatVersion>7.0.0-beta.22154.3</MicrosoftDotNetApiCompatVersion> <MicrosoftDotNetCodeAnalysisVersion>6.0.0-beta.21271.1</MicrosoftDotNetCodeAnalysisVersion> <MicrosoftCodeAnalysisCSharpCodeStyleVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion> - <MicrosoftCodeAnalysisVersion>3.10.0-2.final</MicrosoftCodeAnalysisVersion> + <MicrosoftCodeAnalysisVersion>4.0.1</MicrosoftCodeAnalysisVersion> <MicrosoftCodeAnalysisCSharpAnalyzerTestingXunitVersion>1.0.1-beta1.*</MicrosoftCodeAnalysisCSharpAnalyzerTestingXunitVersion> <MicrosoftCodeAnalysisBannedApiAnalyzersVersion>3.3.2</MicrosoftCodeAnalysisBannedApiAnalyzersVersion> <!-- This controls the version of the cecil package, or the version of cecil in the project graph diff --git a/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs b/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs index 515c24372..7be81d99e 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs @@ -116,6 +116,18 @@ namespace ILLink.RoslynAnalyzer.Tests ValidateDiagnostics (node, node.AttributeLists); } + public override void VisitSimpleLambdaExpression (SimpleLambdaExpressionSyntax node) + { + base.VisitSimpleLambdaExpression (node); + ValidateDiagnostics (node, node.AttributeLists); + } + + public override void VisitParenthesizedLambdaExpression (ParenthesizedLambdaExpressionSyntax node) + { + base.VisitParenthesizedLambdaExpression (node); + ValidateDiagnostics (node, node.AttributeLists); + } + public override void VisitAccessorDeclaration (AccessorDeclarationSyntax node) { base.VisitAccessorDeclaration (node); diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInCompilerGeneratedCode.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInCompilerGeneratedCode.cs index e7192de58..cadebd061 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInCompilerGeneratedCode.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInCompilerGeneratedCode.cs @@ -564,23 +564,23 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability class WarnInLocalFunction { - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestCall () { LocalFunction (); + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] void LocalFunction () => MethodWithRequires (); } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestCallWithClosure (int p = 0) { LocalFunction (); + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] void LocalFunction () { p++; @@ -588,23 +588,23 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability } } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] static void TestReflectionAccess () { LocalFunction (); + [ExpectedWarning ("IL2026", "--MethodWithRequires--", ProducedBy = ProducedBy.Trimmer)] void LocalFunction () => typeof (RequiresInCompilerGeneratedCode) .GetMethod ("MethodWithRequires", System.Reflection.BindingFlags.NonPublic) .Invoke (null, new object[] { }); } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestLdftn () { LocalFunction (); + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] void LocalFunction () { var action = new Action (MethodWithRequires); @@ -626,11 +626,11 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability } } - [ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] static void TestDynamicallyAccessedMethod () { LocalFunction (); + [ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", ProducedBy = ProducedBy.Trimmer)] void LocalFunction () => typeof (TypeWithMethodWithRequires).RequiresNonPublicMethods (); } @@ -910,41 +910,45 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability class WarnInLambda { - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestCall () { - Action _ = () => MethodWithRequires (); + Action _ = + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + () => MethodWithRequires (); } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestCallWithClosure (int p = 0) { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + () => { p++; MethodWithRequires (); }; } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] static void TestReflectionAccess () { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026", "--MethodWithRequires--", ProducedBy = ProducedBy.Trimmer)] + () => { typeof (RequiresInCompilerGeneratedCode) .GetMethod ("MethodWithRequires", System.Reflection.BindingFlags.NonPublic) .Invoke (null, new object[] { }); }; } - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static void TestLdftn () { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + () => { var action = new Action (MethodWithRequires); }; } @@ -961,10 +965,11 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability }; } - [ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] static void TestDynamicallyAccessedMethod () { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026", "--TypeWithMethodWithRequires.MethodWithRequires--", ProducedBy = ProducedBy.Trimmer)] + () => { typeof (TypeWithMethodWithRequires).RequiresNonPublicMethods (); }; } @@ -987,66 +992,70 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability // C# 10 allows attributes on lambdas // - This would be useful as a workaround for the limitation as Requires could be applied to the lambda directly - // - Would be useful for testing - have to use the CompilerGeneratedCode = true trick instead - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestCall () { - Action _ = () => MethodWithRequires (); + Action _ = + [ExpectedWarning ("IL2026")] + [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] + () => MethodWithRequires (); } - [ExpectedWarning ("IL2067", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestCallWithReflectionAnalysisWarning () { // This should not produce warning because the Requires - Action<Type> _ = (t) => t.RequiresPublicMethods (); + Action<Type> _ = + [ExpectedWarning ("IL2067", ProducedBy = ProducedBy.Trimmer)] + (t) => t.RequiresPublicMethods (); } - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestCallWithClosure (int p = 0) { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026")] + [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] + () => { p++; MethodWithRequires (); }; } - // Analyzer doesn't recognize reflection access - so doesn't warn in this case - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestReflectionAccess () { - Action _ = () => { + Action _ = + // Analyzer doesn't recognize reflection access - so doesn't warn in this case + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => { typeof (RequiresInCompilerGeneratedCode) .GetMethod ("MethodWithRequires", System.Reflection.BindingFlags.NonPublic) .Invoke (null, new object[] { }); }; } - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestLdftn () { - Action _ = () => { + Action _ = + [ExpectedWarning ("IL2026")] + [ExpectedWarning ("IL3002", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", ProducedBy = ProducedBy.Analyzer)] + () => { var action = new Action (MethodWithRequires); }; } @@ -1063,27 +1072,29 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability }; } - // Analyzer doesn't apply DAM - so won't see this warnings - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static void TestDynamicallyAccessedMethod () { - Action _ = () => { + Action _ = + // Analyzer doesn't apply DAM - so won't see this warnings + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => { typeof (TypeWithMethodWithRequires).RequiresNonPublicMethods (); }; } - // TODO: Fix the discrepancy between linker and analyzer - // https://github.com/dotnet/linker/issues/2350 - [ExpectedWarning ("IL2077", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [RequiresUnreferencedCode ("Suppress in body")] [RequiresAssemblyFiles ("Suppress in body")] [RequiresDynamicCode ("Suppress in body")] static async void TestMethodParameterWithRequirements (Type unknownType = null) { - Action _ = () => unknownType.RequiresNonPublicMethods (); + Action _ = + // TODO: Fix the discrepancy between linker and analyzer + // https://github.com/dotnet/linker/issues/2350 + [ExpectedWarning ("IL2077", ProducedBy = ProducedBy.Trimmer)] + () => unknownType.RequiresNonPublicMethods (); } // The warning is currently not detected by roslyn analyzer since it doesn't analyze DAM yet @@ -1130,15 +1141,15 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability class WarnInComplex { - [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] - [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] - [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] static async void TestIteratorLocalFunctionInAsync () { await MethodAsync (); LocalFunction (); await MethodAsync (); + [ExpectedWarning ("IL2026", "--MethodWithRequires--", CompilerGeneratedCode = true)] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] IEnumerable<int> LocalFunction () { yield return 0; diff --git a/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsInCompilerGeneratedCode.cs b/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsInCompilerGeneratedCode.cs index 1655fc9d0..c57dc3afd 100644 --- a/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsInCompilerGeneratedCode.cs +++ b/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsInCompilerGeneratedCode.cs @@ -342,57 +342,64 @@ namespace Mono.Linker.Tests.Cases.Warnings.WarningSuppression class SuppressInLambda { - // Suppression currently doesn't propagate to local functions + // Suppression currently doesn't propagate to lambdas - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2026")] static void TestCallRUCMethod () { - Action _ = () => RequiresUnreferencedCodeMethod (); + Action _ = + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => RequiresUnreferencedCodeMethod (); } - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2026")] static void TestReflectionAccessRUCMethod () { - Action _ = () => typeof (SuppressWarningsInCompilerGeneratedCode) + Action _ = + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => typeof (SuppressWarningsInCompilerGeneratedCode) .GetMethod ("RequiresUnreferencedCodeMethod", System.Reflection.BindingFlags.NonPublic) .Invoke (null, new object[] { }); } - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2026")] static void TestLdftnOnRUCMethod () { - Action _ = () => { var _ = new Action (RequiresUnreferencedCodeMethod); }; + Action _ = + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => { var _ = new Action (RequiresUnreferencedCodeMethod); }; } - [ExpectedWarning ("IL2026", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2026")] static void TestDynamicallyAccessedMethod () { - Action _ = () => typeof (TypeWithRUCMethod).RequiresNonPublicMethods (); + Action _ = + [ExpectedWarning ("IL2026", ProducedBy = ProducedBy.Trimmer)] + () => typeof (TypeWithRUCMethod).RequiresNonPublicMethods (); } - [ExpectedWarning ("IL2077", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2077")] static void TestMethodParameterWithRequirements (Type unknownType = null) { - Action _ = () => unknownType.RequiresNonPublicMethods (); + Action _ = + [ExpectedWarning ("IL2077", ProducedBy = ProducedBy.Trimmer)] + () => unknownType.RequiresNonPublicMethods (); } - [ExpectedWarning ("IL2091", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2091")] static void TestGenericMethodParameterRequirement<TUnknown> () { - Action _ = () => MethodWithGenericWhichRequiresMethods<TUnknown> (); + Action _ = + [ExpectedWarning ("IL2091", ProducedBy = ProducedBy.Trimmer)] + () => MethodWithGenericWhichRequiresMethods<TUnknown> (); } - [ExpectedWarning ("IL2091", CompilerGeneratedCode = true, ProducedBy = ProducedBy.Trimmer)] [UnconditionalSuppressMessage ("Test", "IL2091")] static void TestGenericTypeParameterRequirement<TUnknown> () { - Action _ = () => new TypeWithGenericWhichRequiresNonPublicFields<TUnknown> (); + Action _ = + [ExpectedWarning ("IL2091", ProducedBy = ProducedBy.Trimmer)] + () => new TypeWithGenericWhichRequiresNonPublicFields<TUnknown> (); } public static void Test () diff --git a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj index 9ba73d087..4a9ea7e7f 100644 --- a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj +++ b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj @@ -31,7 +31,7 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" /> + <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" /> <PackageReference Include="nunit" Version="3.12.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.1.0" /> <!-- This reference is purely so that the linker can resolve this |