Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com>2022-08-24 02:00:56 +0300
committerGitHub <noreply@github.com>2022-08-24 02:00:56 +0300
commit6c73ce6d6c4828e20a1e911127963e153bd7db00 (patch)
treed5744c7b094480e5325fdba4e2132a92260b8c0f
parent203d9e5589aacd80dd170eeefa42b428d1095cf8 (diff)
CodeFixer enhancements (#2994)
* Move usage of the semantic model outside of the RegisterCodeFixesAsync method for all CodeFixers since it slows lightbulb creation * Add GetBestTypeByMetadataName from dotnet/roslyn and use it instead of GetTypeByMetadataName * Use raw string literals in all codefixers testing files (this makes some tests to require spaces instead of tabs and change line numbers) * More nit fixes to make test more consistent * Rename DAMCodeFixProvider to DynamicallyAccessedMembersCodeFixProvider * Add helper class for codeFixer
-rw-r--r--src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs40
-rw-r--r--src/ILLink.CodeFix/DynamicallyAccessedMembersCodeFixProvider.cs (renamed from src/ILLink.CodeFix/DAMCodeFixProvider.cs)31
-rw-r--r--src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs10
-rw-r--r--src/ILLink.CodeFix/RequiresDynamicCodeCodeFixProvider.cs10
-rw-r--r--src/ILLink.CodeFix/RequiresHelpers.cs22
-rw-r--r--src/ILLink.CodeFix/RequiresUnreferencedCodeCodeFixProvider.cs10
-rw-r--r--src/ILLink.RoslynAnalyzer/CompilationExtensions.cs164
-rw-r--r--test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs2
-rw-r--r--test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs1126
-rw-r--r--test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs930
-rw-r--r--test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs371
-rw-r--r--test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs648
12 files changed, 1822 insertions, 1542 deletions
diff --git a/src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs b/src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs
index 909dd3a65..481c8ba10 100644
--- a/src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs
+++ b/src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs
@@ -5,6 +5,7 @@ using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
+using ILLink.RoslynAnalyzer;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
@@ -32,46 +33,49 @@ namespace ILLink.CodeFix
protected async Task BaseRegisterCodeFixesAsync (CodeFixContext context)
{
var document = context.Document;
+ var diagnostic = context.Diagnostics.First ();
+ var codeFixTitle = CodeFixTitle.ToString ();
+
if (await document.GetSyntaxRootAsync (context.CancellationToken).ConfigureAwait (false) is not { } root)
return;
- var diagnostic = context.Diagnostics.First ();
+
SyntaxNode targetNode = root.FindNode (diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
if (FindAttributableParent (targetNode, AttributableParentTargets) is not SyntaxNode attributableNode)
return;
- if (await document.GetSemanticModelAsync (context.CancellationToken).ConfigureAwait (false) is not { } model)
- return;
- if (model.GetSymbolInfo (targetNode).Symbol is not { } targetSymbol)
- return;
- if (model.Compilation.GetTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol)
- return;
- // N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables
- var attributableSymbol = model.GetDeclaredSymbol (attributableNode);
-
- var attributeArguments = GetAttributeArguments (attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator (document), diagnostic);
- var codeFixTitle = CodeFixTitle.ToString ();
-
context.RegisterCodeFix (CodeAction.Create (
title: codeFixTitle,
createChangedDocument: ct => AddAttributeAsync (
- document, attributableNode, attributeArguments, attributeSymbol, ct),
+ document, diagnostic, targetNode, attributableNode, ct),
equivalenceKey: codeFixTitle), diagnostic);
}
- private static async Task<Document> AddAttributeAsync (
+ private async Task<Document> AddAttributeAsync (
Document document,
+ Diagnostic diagnostic,
SyntaxNode targetNode,
- SyntaxNode[] attributeArguments,
- ITypeSymbol attributeSymbol,
+ SyntaxNode attributableNode,
CancellationToken cancellationToken)
{
+ if (await document.GetSemanticModelAsync (cancellationToken).ConfigureAwait (false) is not { } model)
+ return document;
+ if (model.GetSymbolInfo (targetNode, cancellationToken).Symbol is not { } targetSymbol)
+ return document;
+ if (model.Compilation.GetBestTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol)
+ return document;
+
+ // N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables
+ var attributableSymbol = model.GetDeclaredSymbol (attributableNode, cancellationToken);
+
+ var attributeArguments = GetAttributeArguments (attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator (document), diagnostic);
+
var editor = await DocumentEditor.CreateAsync (document, cancellationToken).ConfigureAwait (false);
var generator = editor.Generator;
var attribute = generator.Attribute (
generator.TypeExpression (attributeSymbol), attributeArguments)
.WithAdditionalAnnotations (Simplifier.Annotation, Simplifier.AddImportsAnnotation);
- editor.AddAttribute (targetNode, attribute);
+ editor.AddAttribute (attributableNode, attribute);
return editor.GetChangedDocument ();
}
diff --git a/src/ILLink.CodeFix/DAMCodeFixProvider.cs b/src/ILLink.CodeFix/DynamicallyAccessedMembersCodeFixProvider.cs
index e36222918..249dd1c23 100644
--- a/src/ILLink.CodeFix/DAMCodeFixProvider.cs
+++ b/src/ILLink.CodeFix/DynamicallyAccessedMembersCodeFixProvider.cs
@@ -19,8 +19,8 @@ using Microsoft.CodeAnalysis.Simplification;
namespace ILLink.CodeFix
{
- [ExportCodeFixProvider (LanguageNames.CSharp, Name = nameof (DAMCodeFixProvider)), Shared]
- public sealed class DAMCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider
+ [ExportCodeFixProvider (LanguageNames.CSharp, Name = nameof (DynamicallyAccessedMembersCodeFixProvider)), Shared]
+ public sealed class DynamicallyAccessedMembersCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider
{
private static ImmutableArray<DiagnosticDescriptor> GetSupportedDiagnostics ()
{
@@ -93,31 +93,24 @@ namespace ILLink.CodeFix
public override async Task RegisterCodeFixesAsync (CodeFixContext context)
{
var document = context.Document;
+ var diagnostic = context.Diagnostics[0];
+ var codeFixTitle = CodeFixTitle.ToString ();
+
if (await document.GetSyntaxRootAsync (context.CancellationToken).ConfigureAwait (false) is not { } root)
return;
- var diagnostic = context.Diagnostics[0];
if (diagnostic.AdditionalLocations.Count == 0)
return;
- if (root.FindNode (diagnostic.AdditionalLocations[0].SourceSpan, getInnermostNodeForTie: true) is not SyntaxNode attributableNode)
+ if (root.FindNode (diagnostic.AdditionalLocations[0].SourceSpan, getInnermostNodeForTie: true) is not SyntaxNode targetNode)
return;
- // currently not supporting multiple DAM argument, hence the check for commas in the string arguments
- if (diagnostic.Properties[DynamicallyAccessedMembersAnalyzer.attributeArgument] is not string stringArgs || stringArgs.Contains (","))
+ if (diagnostic.Properties["attributeArgument"] is not string stringArgs || stringArgs.Contains (","))
return;
- var syntaxGenerator = SyntaxGenerator.GetGenerator (document);
- if (await document.GetSemanticModelAsync (context.CancellationToken).ConfigureAwait (false) is not { } model)
- return;
- if (model.Compilation.GetTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol)
- return;
- var codeFixTitle = CodeFixTitle.ToString ();
-
context.RegisterCodeFix (CodeAction.Create (
- title: codeFixTitle,
+ title: CodeFixTitle.ToString (),
createChangedDocument: ct => AddAttributeAsync (
document,
- attributableNode,
+ targetNode,
stringArgs,
- attributeSymbol,
addAsReturnAttribute: AttributeOnReturn.Contains (diagnostic.Id),
addGenericParameterAttribute: AttributeOnGeneric.Contains (diagnostic.Id),
ct),
@@ -128,11 +121,15 @@ namespace ILLink.CodeFix
Document document,
SyntaxNode targetNode,
string stringArguments,
- ITypeSymbol attributeSymbol,
bool addAsReturnAttribute,
bool addGenericParameterAttribute,
CancellationToken cancellationToken)
{
+ if (await document.GetSemanticModelAsync (cancellationToken).ConfigureAwait (false) is not { } model)
+ return document;
+ if (model.Compilation.GetBestTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol)
+ return document;
+
var editor = await DocumentEditor.CreateAsync (document, cancellationToken).ConfigureAwait (false);
var generator = editor.Generator;
var attributeArguments = new[] { generator.AttributeArgument (generator.MemberAccessExpression (generator.DottedName ("System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes"), stringArguments)) };
diff --git a/src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs b/src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs
index 1bf420c05..7ed5a8414 100644
--- a/src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs
+++ b/src/ILLink.CodeFix/RequiresAssemblyFilesCodeFixProvider.cs
@@ -33,13 +33,7 @@ namespace ILLink.CodeFix
public sealed override Task RegisterCodeFixesAsync (CodeFixContext context) => BaseRegisterCodeFixesAsync (context);
- protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic)
- {
- var symbolDisplayName = targetSymbol.GetDisplayName ();
- if (string.IsNullOrEmpty (symbolDisplayName) || HasPublicAccessibility (attributableSymbol))
- return Array.Empty<SyntaxNode> ();
-
- return new[] { syntaxGenerator.AttributeArgument (syntaxGenerator.LiteralExpression ($"Calls {symbolDisplayName}")) };
- }
+ protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic) =>
+ RequiresHelpers.GetAttributeArgumentsForRequires (targetSymbol, syntaxGenerator, HasPublicAccessibility(attributableSymbol));
}
}
diff --git a/src/ILLink.CodeFix/RequiresDynamicCodeCodeFixProvider.cs b/src/ILLink.CodeFix/RequiresDynamicCodeCodeFixProvider.cs
index b0e5ef9ec..807d14b7d 100644
--- a/src/ILLink.CodeFix/RequiresDynamicCodeCodeFixProvider.cs
+++ b/src/ILLink.CodeFix/RequiresDynamicCodeCodeFixProvider.cs
@@ -30,13 +30,7 @@ namespace ILLink.CodeFix
public sealed override Task RegisterCodeFixesAsync (CodeFixContext context) => BaseRegisterCodeFixesAsync (context);
- protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic)
- {
- var symbolDisplayName = targetSymbol.GetDisplayName ();
- if (string.IsNullOrEmpty (symbolDisplayName) || HasPublicAccessibility (attributableSymbol))
- return Array.Empty<SyntaxNode> ();
-
- return new[] { syntaxGenerator.AttributeArgument (syntaxGenerator.LiteralExpression ($"Calls {symbolDisplayName}")) };
- }
+ protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic) =>
+ RequiresHelpers.GetAttributeArgumentsForRequires (targetSymbol, syntaxGenerator, HasPublicAccessibility (attributableSymbol));
}
}
diff --git a/src/ILLink.CodeFix/RequiresHelpers.cs b/src/ILLink.CodeFix/RequiresHelpers.cs
new file mode 100644
index 000000000..91bb99591
--- /dev/null
+++ b/src/ILLink.CodeFix/RequiresHelpers.cs
@@ -0,0 +1,22 @@
+// Copyright (c) .NET Foundation and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using System;
+using Microsoft.CodeAnalysis.Editing;
+using Microsoft.CodeAnalysis;
+using ILLink.RoslynAnalyzer;
+
+namespace ILLink.CodeFixProvider
+{
+ sealed class RequiresHelpers
+ {
+ internal static SyntaxNode[] GetAttributeArgumentsForRequires (ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, bool hasPublicAccessibility)
+ {
+ var symbolDisplayName = targetSymbol.GetDisplayName ();
+ if (string.IsNullOrEmpty (symbolDisplayName) || hasPublicAccessibility)
+ return Array.Empty<SyntaxNode> ();
+
+ return new[] { syntaxGenerator.AttributeArgument (syntaxGenerator.LiteralExpression ($"Calls {symbolDisplayName}")) };
+ }
+ }
+}
diff --git a/src/ILLink.CodeFix/RequiresUnreferencedCodeCodeFixProvider.cs b/src/ILLink.CodeFix/RequiresUnreferencedCodeCodeFixProvider.cs
index 578fdff03..5535e1764 100644
--- a/src/ILLink.CodeFix/RequiresUnreferencedCodeCodeFixProvider.cs
+++ b/src/ILLink.CodeFix/RequiresUnreferencedCodeCodeFixProvider.cs
@@ -30,13 +30,7 @@ namespace ILLink.CodeFix
public sealed override Task RegisterCodeFixesAsync (CodeFixContext context) => BaseRegisterCodeFixesAsync (context);
- protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic)
- {
- var symbolDisplayName = targetSymbol.GetDisplayName ();
- if (string.IsNullOrEmpty (symbolDisplayName) || HasPublicAccessibility (attributableSymbol))
- return Array.Empty<SyntaxNode> ();
-
- return new[] { syntaxGenerator.AttributeArgument (syntaxGenerator.LiteralExpression ($"Calls {symbolDisplayName}")) };
- }
+ protected override SyntaxNode[] GetAttributeArguments (ISymbol? attributableSymbol, ISymbol targetSymbol, SyntaxGenerator syntaxGenerator, Diagnostic diagnostic) =>
+ RequiresHelpers.GetAttributeArgumentsForRequires (targetSymbol, syntaxGenerator, HasPublicAccessibility (attributableSymbol));
}
}
diff --git a/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs b/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs
new file mode 100644
index 000000000..0f3d7f8e0
--- /dev/null
+++ b/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs
@@ -0,0 +1,164 @@
+// Copyright (c) .NET Foundation and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using System.Collections.Immutable;
+using System;
+
+namespace ILLink.RoslynAnalyzer
+{
+ // Copied from: https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/CompilationExtensions.cs
+ public static class CompilationExtensions
+ {
+ /// <summary>
+ /// Gets a type by its metadata name to use for code analysis within a <see cref="Compilation"/>. This method
+ /// attempts to find the "best" symbol to use for code analysis, which is the symbol matching the first of the
+ /// following rules.
+ ///
+ /// <list type="number">
+ /// <item><description>
+ /// If only one type with the given name is found within the compilation and its referenced assemblies, that
+ /// type is returned regardless of accessibility.
+ /// </description></item>
+ /// <item><description>
+ /// If the current <paramref name="compilation"/> defines the symbol, that symbol is returned.
+ /// </description></item>
+ /// <item><description>
+ /// If exactly one referenced assembly defines the symbol in a manner that makes it visible to the current
+ /// <paramref name="compilation"/>, that symbol is returned.
+ /// </description></item>
+ /// <item><description>
+ /// Otherwise, this method returns <see langword="null"/>.
+ /// </description></item>
+ /// </list>
+ /// </summary>
+ /// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
+ /// <param name="fullyQualifiedMetadataName">The fully-qualified metadata type name to find.</param>
+ /// <returns>The symbol to use for code analysis; otherwise, <see langword="null"/>.</returns>
+ public static INamedTypeSymbol? GetBestTypeByMetadataName (this Compilation compilation, string fullyQualifiedMetadataName)
+ {
+ // Try to get the unique type with this name, ignoring accessibility
+ var type = compilation.GetTypeByMetadataName (fullyQualifiedMetadataName);
+
+ // Otherwise, try to get the unique type with this name originally defined in 'compilation'
+ type ??= compilation.Assembly.GetTypeByMetadataName (fullyQualifiedMetadataName);
+
+ // Otherwise, try to get the unique accessible type with this name from a reference
+ if (type is null) {
+ foreach (var module in compilation.Assembly.Modules) {
+ foreach (var referencedAssembly in module.ReferencedAssemblySymbols) {
+ var currentType = referencedAssembly.GetTypeByMetadataName (fullyQualifiedMetadataName);
+ if (currentType is null)
+ continue;
+
+ switch (currentType.GetResultantVisibility ()) {
+ case SymbolVisibility.Public:
+ case SymbolVisibility.Internal when referencedAssembly.GivesAccessTo (compilation.Assembly):
+ break;
+
+ default:
+ continue;
+ }
+
+ if (type is object) {
+ // Multiple visible types with the same metadata name are present
+ return null;
+ }
+
+ type = currentType;
+ }
+ }
+ }
+
+ return type;
+ }
+
+ // copied from https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ISymbolExtensions.cs
+ private static SymbolVisibility GetResultantVisibility (this ISymbol symbol)
+ {
+ // Start by assuming it's visible.
+ SymbolVisibility visibility = SymbolVisibility.Public;
+
+ switch (symbol.Kind) {
+ case SymbolKind.Alias:
+ // Aliases are uber private. They're only visible in the same file that they
+ // were declared in.
+ return SymbolVisibility.Private;
+
+ case SymbolKind.Parameter:
+ // Parameters are only as visible as their containing symbol
+ return GetResultantVisibility (symbol.ContainingSymbol);
+
+ case SymbolKind.TypeParameter:
+ // Type Parameters are private.
+ return SymbolVisibility.Private;
+ }
+
+ while (symbol != null && symbol.Kind != SymbolKind.Namespace) {
+ switch (symbol.DeclaredAccessibility) {
+ // If we see anything private, then the symbol is private.
+ case Accessibility.NotApplicable:
+ case Accessibility.Private:
+ return SymbolVisibility.Private;
+
+ // If we see anything internal, then knock it down from public to
+ // internal.
+ case Accessibility.Internal:
+ case Accessibility.ProtectedAndInternal:
+ visibility = SymbolVisibility.Internal;
+ break;
+
+ // For anything else (Public, Protected, ProtectedOrInternal), the
+ // symbol stays at the level we've gotten so far.
+ }
+
+ symbol = symbol.ContainingSymbol;
+ }
+
+ return visibility;
+ }
+
+ // Copied from: https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/SymbolVisibility.cs
+ private enum SymbolVisibility
+ {
+ Public = 0,
+ Internal = 1,
+ Private = 2,
+ Friend = Internal,
+ }
+
+ internal static bool HasAttributeSuffix (this string name, bool isCaseSensitive)
+ {
+ const string AttributeSuffix = "Attribute";
+
+ var comparison = isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
+ return name.Length > AttributeSuffix.Length && name.EndsWith (AttributeSuffix, comparison);
+ }
+
+ public static ImmutableArray<T> ToImmutableArray<T> (this ReadOnlySpan<T> span)
+ {
+ switch (span.Length) {
+ case 0: return ImmutableArray<T>.Empty;
+ case 1: return ImmutableArray.Create (span[0]);
+ case 2: return ImmutableArray.Create (span[0], span[1]);
+ case 3: return ImmutableArray.Create (span[0], span[1], span[2]);
+ case 4: return ImmutableArray.Create (span[0], span[1], span[2], span[3]);
+ default:
+ var builder = ImmutableArray.CreateBuilder<T> (span.Length);
+ foreach (var item in span)
+ builder.Add (item);
+
+ return builder.MoveToImmutable ();
+ }
+ }
+
+ public static SimpleNameSyntax GetUnqualifiedName (this NameSyntax name)
+ => name switch {
+ AliasQualifiedNameSyntax alias => alias.Name,
+ QualifiedNameSyntax qualified => qualified.Right,
+ SimpleNameSyntax simple => simple,
+ _ => throw new InvalidOperationException ("Unreachable"),
+ };
+ }
+}
diff --git a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs
index ff70b7f58..71e6ab82d 100644
--- a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs
+++ b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs
@@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.Testing;
using Xunit;
using VerifyCS = ILLink.RoslynAnalyzer.Tests.CSharpCodeFixVerifier<
ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer,
- ILLink.CodeFix.DAMCodeFixProvider>;
+ ILLink.CodeFix.DynamicallyAccessedMembersCodeFixProvider>;
namespace ILLink.RoslynAnalyzer.Tests
{
diff --git a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs
index 4eed3b026..1197135d8 100644
--- a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs
+++ b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs
@@ -9,7 +9,7 @@ using Microsoft.CodeAnalysis.Text;
using Xunit;
using VerifyCS = ILLink.RoslynAnalyzer.Tests.CSharpCodeFixVerifier<
ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer,
- ILLink.CodeFix.DAMCodeFixProvider>;
+ ILLink.CodeFix.DynamicallyAccessedMembersCodeFixProvider>;
namespace ILLink.RoslynAnalyzer.Tests
{
@@ -71,18 +71,22 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
- // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
- .WithSpan(7, 3, 7, 8)
- .WithSpan(6, 16, 6, 22)
- .WithArguments("t",
- "C.M2(Type)",
- "t",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.All'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
+ // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
+ .WithSpan(7, 3, 7, 8)
+ .WithSpan(6, 16, 6, 22)
+ .WithArguments("t",
+ "C.M2(Type)",
+ "t",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.All'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -121,18 +125,22 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
- // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
- .WithSpan(8, 3, 8, 8)
- .WithSpan(7, 18, 7, 24)
- .WithArguments("t",
- "C.M2(Type)",
- "t",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
+ // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
+ .WithSpan(8, 3, 8, 8)
+ .WithSpan(7, 18, 7, 24)
+ .WithArguments("t",
+ "C.M2(Type)",
+ "t",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -153,7 +161,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
+ // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
// The parameter 't' of method 'C.M(Type)' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
@@ -163,7 +171,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
"C.M2(Type)",
"t",
"C.M(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields'")};
+ "'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -185,8 +194,9 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
static void M2([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type t) {}
}
""";
+
var diag = new[] {
- // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
+ // /0/Test0.cs(7,3): warning IL2067: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'C.M2(Type)'.
// The parameter 't' of method 'C.M(Type)' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter)
@@ -195,7 +205,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
"C.M2(Type)",
"t",
"C.M(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -228,15 +239,19 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,10): warning IL2068: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.All' requirements. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsMethodReturnType)
- .WithSpan (8, 10, 8, 11)
- .WithSpan (7, 9, 7, 15)
- .WithArguments ("C.M(Type)",
- "t",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.All'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,10): warning IL2068: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.All' requirements. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsMethodReturnType)
+ .WithSpan (8, 10, 8, 11)
+ .WithSpan (7, 9, 7, 15)
+ .WithArguments ("C.M(Type)",
+ "t",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.All'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -256,13 +271,14 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(8,10): warning IL2068: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.All' requirements. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ // /0/Test0.cs(8,10): warning IL2068: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.All' requirements. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsMethodReturnType)
.WithSpan (8, 10, 8, 11)
.WithArguments ("C.M(Type)",
"t",
"C.M(Type)",
- "'DynamicallyAccessedMemberTypes.All'")};
+ "'DynamicallyAccessedMemberTypes.All'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -310,17 +326,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
private static Type f = typeof(C);
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(13,3): warning IL2069: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- //The parameter 'type' of method 'C.M(Type)' does not have matching annotations.
- //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsField)
- .WithSpan(13, 3, 13, 11)
- .WithSpan(11, 24, 11, 33)
- .WithArguments ("C.f",
- "type",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(13,3): warning IL2069: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ //The parameter 'type' of method 'C.M(Type)' does not have matching annotations.
+ //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsField)
+ .WithSpan(13, 3, 13, 11)
+ .WithSpan(11, 24, 11, 33)
+ .WithArguments ("C.f",
+ "type",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -359,15 +379,19 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(12,3): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
- .WithSpan(12, 3, 12, 17)
- .WithSpan(10, 16, 10, 22)
- .WithArguments("System.Type.GetMethods()",
- "t",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(12,3): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'. The parameter 't' of method 'C.M(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
+ .WithSpan(12, 3, 12, 17)
+ .WithSpan(10, 16, 10, 22)
+ .WithArguments("System.Type.GetMethods()",
+ "t",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -409,22 +433,28 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(13,3): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'.
- // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
- .WithSpan(13, 3, 13, 39)
- .WithSpan(11, 16, 11, 22)
- .WithArguments("System.Type.GetMethods(BindingFlags)",
- "t",
- "C.M(Type)",
- "'DynamicallyAccessedMemberTypes.NonPublicMethods'")}, new[] {
- // /0/Test0.cs(9,3): warning IL2111: Method 'C.M(Type)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection.
- // Trimmer can't guarantee availability of the requirements of the method.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMethodAccessedViaReflection)
- .WithSpan (9, 3, 9, 15)
- .WithArguments ("C.M(Type)") });
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(13,3): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'.
+ // The parameter 't' of method 'C.M(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
+ .WithSpan(13, 3, 13, 39)
+ .WithSpan(11, 16, 11, 22)
+ .WithArguments("System.Type.GetMethods(BindingFlags)",
+ "t",
+ "C.M(Type)",
+ "'DynamicallyAccessedMemberTypes.NonPublicMethods'")
+ },
+ fixedExpected: new[] {
+ // /0/Test0.cs(9,3): warning IL2111: Method 'C.M(Type)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection.
+ // Trimmer can't guarantee availability of the requirements of the method.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMethodAccessedViaReflection)
+ .WithSpan (9, 3, 9, 15)
+ .WithArguments ("C.M(Type)")
+ });
}
[Fact]
@@ -470,17 +500,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(10,27): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
- // The parameter 't' of method 'System.C.Main(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
- .WithSpan(10, 27, 10, 41)
- .WithSpan(8, 20, 8, 26)
- .WithArguments("System.Type.GetMethods()",
- "t",
- "System.C.Main(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(10,27): warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
+ // The parameter 't' of method 'System.C.Main(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter)
+ .WithSpan(10, 27, 10, 41)
+ .WithSpan(8, 20, 8, 26)
+ .WithArguments("System.Type.GetMethods()",
+ "t",
+ "System.C.Main(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -534,18 +568,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,3): warning IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
- // The return value of method 'C.GetT()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
-
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
+ // The return value of method 'C.GetT()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
.WithSpan(8, 3, 8, 40)
.WithSpan(16, 2, 19, 3)
.WithArguments("type",
"C.NeedsPublicMethodsOnParameter(Type)",
"C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -599,17 +636,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,3): warning IL2072: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
- // The return value of method 'C.GetC(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
- .WithSpan(8, 3, 8, 49)
- .WithSpan(16, 2, 19, 3)
- .WithArguments("t",
- "C.NeedsPublicMethodsOnParameter(Type)",
- "C.GetC(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2072: 't' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
+ // The return value of method 'C.GetC(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
+ .WithSpan(8, 3, 8, 49)
+ .WithSpan(16, 2, 19, 3)
+ .WithArguments("t",
+ "C.NeedsPublicMethodsOnParameter(Type)",
+ "C.GetC(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -643,13 +684,13 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
// /0/Test0.cs(8,3): warning IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
// The return value of method 'C.GetT()' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
-
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
- .WithSpan(8, 3, 8, 40)
- .WithArguments("type",
- "C.NeedsPublicMethodsOnParameter(Type)",
- "C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
+ .WithSpan(8, 3, 8, 40)
+ .WithArguments("type",
+ "C.NeedsPublicMethodsOnParameter(Type)",
+ "C.GetC()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -672,7 +713,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
{
}
- [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
+ [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
private static Type GetT()
{
return typeof(C);
@@ -683,12 +724,13 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
// /0/Test0.cs(8,3): warning IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethodsOnParameter(Type)'.
// The return value of method 'C.GetT()' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
- .WithSpan(8, 3, 8, 40)
- .WithArguments("type",
- "C.NeedsPublicMethodsOnParameter(Type)",
- "C.GetT()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter)
+ .WithSpan(8, 3, 8, 40)
+ .WithArguments("type",
+ "C.NeedsPublicMethodsOnParameter(Type)",
+ "C.GetT()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -726,16 +768,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(11,10): warning IL2073: 'C.M()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The return value of method 'C.Main(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsMethodReturnType)
- .WithSpan(11, 10, 11, 25)
- .WithSpan(5, 2, 7, 3)
- .WithArguments("C.M()",
- "C.Main(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(11,10): warning IL2073: 'C.M()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The return value of method 'C.Main(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsMethodReturnType)
+ .WithSpan(11, 10, 11, 25)
+ .WithSpan(5, 2, 7, 3)
+ .WithArguments("C.M()",
+ "C.Main(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> (), 2);
}
@@ -773,16 +819,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(11,10): warning IL2073: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The return value of method 'C.Main(Type)' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsMethodReturnType)
- .WithSpan(11, 10, 11, 17)
- .WithSpan(5, 2, 7, 3)
- .WithArguments("C.M(Type)",
- "C.Main(Type)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(11,10): warning IL2073: 'C.M(Type)' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The return value of method 'C.Main(Type)' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsMethodReturnType)
+ .WithSpan(11, 10, 11, 17)
+ .WithSpan(5, 2, 7, 3)
+ .WithArguments("C.M(Type)",
+ "C.Main(Type)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -830,16 +880,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
private static Type f;
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,3): warning IL2074: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The return value of method 'C.M()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsField)
- .WithSpan(8, 3, 8, 10)
- .WithSpan(11, 2, 14, 3)
- .WithArguments("C.f",
- "C.M()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2074: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The return value of method 'C.M()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsField)
+ .WithSpan(8, 3, 8, 10)
+ .WithSpan(11, 2, 14, 3)
+ .WithArguments("C.f",
+ "C.M()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> (), 1);
}
@@ -882,16 +936,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- //The return value of method 'C.GetFoo()' does not have matching annotations.
- //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
- .WithSpan(8, 3, 8, 26)
- .WithSpan(11, 2, 14, 3)
- .WithArguments ("System.Type.GetMethod(String)",
- "C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ //The return value of method 'C.GetFoo()' does not have matching annotations.
+ //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
+ .WithSpan(8, 3, 8, 26)
+ .WithSpan(11, 2, 14, 3)
+ .WithArguments ("System.Type.GetMethod(String)",
+ "C.GetC()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -935,17 +993,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest), new[] {
- // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- //The return value of method 'C.GetFoo()' does not have matching annotations.
- //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
- .WithSpan(194, 4, 194, 27)
- .WithSpan(197, 3, 200, 4)
- .WithArguments("System.Type.GetMethod(String)",
- "System.C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ //The return value of method 'C.GetFoo()' does not have matching annotations.
+ //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
+ .WithSpan(194, 4, 194, 27)
+ .WithSpan(197, 3, 200, 4)
+ .WithArguments("System.Type.GetMethod(String)",
+ "System.C.GetC()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -963,7 +1024,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
- private static Type GetC ()
+ private static Type GetC ()
{
return typeof(int);
}
@@ -989,17 +1050,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest), new[] {
- // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- //The return value of method 'C.GetFoo()' does not have matching annotations.
- //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
- .WithSpan(193, 4, 193, 27)
- .WithSpan(196, 3, 200, 4)
- .WithArguments("System.Type.GetMethod(String)",
- "System.C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ //The return value of method 'C.GetFoo()' does not have matching annotations.
+ //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
+ .WithSpan(193, 4, 193, 27)
+ .WithSpan(196, 3, 200, 4)
+ .WithArguments("System.Type.GetMethod(String)",
+ "System.C.GetC()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1045,17 +1109,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest), new[] {
- // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- //The return value of method 'C.GetFoo()' does not have matching annotations.
- //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
- .WithSpan(194, 4, 194, 27)
- .WithSpan(198, 3, 201, 4)
- .WithArguments("System.Type.GetMethod(String)",
- "System.C.GetC()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ //The return value of method 'C.GetFoo()' does not have matching annotations.
+ //The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic (DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter)
+ .WithSpan(194, 4, 194, 27)
+ .WithSpan(198, 3, 201, 4)
+ .WithArguments("System.Type.GetMethod(String)",
+ "System.C.GetC()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1101,17 +1168,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(10,3): warning IL2077: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethods(Type)'.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsParameter)
- .WithSpan(10, 3, 10, 24)
- .WithSpan(6, 22, 6, 35)
- .WithArguments("type",
- "C.NeedsPublicMethods(Type)",
- "C.f",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(10,3): warning IL2077: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethods(Type)'.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsParameter)
+ .WithSpan(10, 3, 10, 24)
+ .WithSpan(6, 22, 6, 35)
+ .WithArguments("type",
+ "C.NeedsPublicMethods(Type)",
+ "C.f",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1140,9 +1211,9 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(11,3): warning IL2077: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethods(Type)'.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ // /0/Test0.cs(11,3): warning IL2077: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.NeedsPublicMethods(Type)'.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsParameter)
.WithSpan(11, 3, 11, 24)
.WithArguments("type",
@@ -1185,16 +1256,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
private static Type f;
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(9,10): warning IL2078: 'C.Main()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsMethodReturnType)
- .WithSpan(9, 10, 9, 11)
- .WithSpan(12, 22, 12, 23)
- .WithArguments("C.Main()",
- "C.f",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(9,10): warning IL2078: 'C.Main()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsMethodReturnType)
+ .WithSpan(9, 10, 9, 11)
+ .WithSpan(12, 22, 12, 23)
+ .WithArguments("C.Main()",
+ "C.f",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1218,9 +1293,9 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(9,10): warning IL2078: 'C.Main()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ // /0/Test0.cs(9,10): warning IL2078: 'C.Main()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsMethodReturnType)
.WithSpan(9, 10, 9, 11)
.WithArguments("C.Main()",
@@ -1266,16 +1341,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(13,3): warning IL2079: value stored in field 'C.f2' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The field 'C.f1' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsField)
- .WithSpan(13, 3, 13, 10)
- .WithSpan(6, 22, 6, 36)
- .WithArguments("C.f2",
- "C.f1",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(13,3): warning IL2079: value stored in field 'C.f2' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The field 'C.f1' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsField)
+ .WithSpan(13, 3, 13, 10)
+ .WithSpan(6, 22, 6, 36)
+ .WithArguments("C.f2",
+ "C.f1",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1301,14 +1380,15 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(14,3): warning IL2079: value stored in field 'C.f2' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The field 'C.f1' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ // /0/Test0.cs(14,3): warning IL2079: value stored in field 'C.f2' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The field 'C.f1' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsField)
.WithSpan(14, 3, 14, 10)
.WithArguments("C.f2",
"C.f1",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -1344,16 +1424,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(10,3): warning IL2080: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsThisParameter)
- .WithSpan(10, 3, 10, 21)
- .WithSpan(6, 22, 6, 35)
- .WithArguments("System.Type.GetMethod(String)",
- "C.f",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(10,3): warning IL2080: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsThisParameter)
+ .WithSpan(10, 3, 10, 21)
+ .WithSpan(6, 22, 6, 35)
+ .WithArguments("System.Type.GetMethod(String)",
+ "C.f",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1389,16 +1473,20 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(10,3): warning IL2080: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
- // The field 'C.f' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsThisParameter)
- .WithSpan(10, 3, 10, 21)
- .WithSpan(6, 21, 6, 34)
- .WithArguments("System.Type.GetMethod(String)",
- "C.f",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(10,3): warning IL2080: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'.
+ // The field 'C.f' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsThisParameter)
+ .WithSpan(10, 3, 10, 21)
+ .WithSpan(6, 21, 6, 34)
+ .WithArguments("System.Type.GetMethod(String)",
+ "C.f",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -1428,7 +1516,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
.WithSpan(11, 3, 11, 21)
.WithArguments("System.Type.GetMethod(String)",
"C.f",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -1479,25 +1568,28 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
- // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
- .WithSpan(198, 4, 198, 12)
- .WithSpan(196, 3, 199, 4)
- .WithArguments("t",
- "System.C.M2(Type)",
- "System.C.M1()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
+ // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
+ .WithSpan(198, 4, 198, 12)
+ .WithSpan(196, 3, 199, 4)
+ .WithArguments("t",
+ "System.C.M2(Type)",
+ "System.C.M1()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")});
+ .WithArguments("System.C.M1()")
+ });
}
[Fact]
@@ -1551,25 +1643,28 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
- // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
- .WithSpan(199, 4, 199, 12)
- .WithSpan(196, 3, 201, 4)
- .WithArguments("t",
- "System.C.M2(Type)",
- "System.C.M1()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
+ // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
+ .WithSpan(199, 4, 199, 12)
+ .WithSpan(196, 3, 201, 4)
+ .WithArguments("t",
+ "System.C.M2(Type)",
+ "System.C.M1()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")});
+ .WithArguments("System.C.M1()")
+ });
}
[Fact]
@@ -1623,25 +1718,28 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
- // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
- .WithSpan(199, 4, 199, 12)
- .WithSpan(196, 3, 201, 4)
- .WithArguments("t",
- "System.C.M2(Type)",
- "System.C.M1(String)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(198,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
+ // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
+ .WithSpan(199, 4, 199, 12)
+ .WithSpan(196, 3, 201, 4)
+ .WithArguments("t",
+ "System.C.M2(Type)",
+ "System.C.M1(String)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 21)
- .WithArguments("System.C.M1(String)")});
+ .WithArguments("System.C.M1(String)")
+ });
}
[Fact]
@@ -1670,20 +1768,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(199,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
- // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ // /0/Test0.cs(199,4): warning IL2082: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2(Type)'.
+ // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter)
.WithSpan(199, 4, 199, 12)
.WithArguments("t",
"System.C.M2(Type)",
"System.C.M1()",
"'DynamicallyAccessedMemberTypes.PublicMethods'"),
- // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
- // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
- VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
- .WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")};
+ // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
+ // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
+ VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
+ .WithSpan(193, 4, 193, 16)
+ .WithArguments("System.C.M1()")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test), diag, diag);
}
@@ -1729,22 +1828,25 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(199,11): warning IL2083: 'System.C.M1()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsMethodReturnType)
- .WithSpan(199, 11, 199, 15)
- .WithSpan(196, 3, 200, 4)
- .WithArguments("System.C.M1()",
- "System.C.M1()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(199,11): warning IL2083: 'System.C.M1()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsMethodReturnType)
+ .WithSpan(199, 11, 199, 15)
+ .WithSpan(196, 3, 200, 4)
+ .WithArguments("System.C.M1()",
+ "System.C.M1()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")});
+ .WithArguments("System.C.M1()")
+ });
}
[Fact]
@@ -1790,22 +1892,25 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(199,11): warning IL2083: 'System.C.M1()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsMethodReturnType)
- .WithSpan(200, 11, 200, 15)
- .WithSpan(196, 3, 201, 4)
- .WithArguments("System.C.M1(String)",
- "System.C.M1(String)",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(199,11): warning IL2083: 'System.C.M1()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsMethodReturnType)
+ .WithSpan(200, 11, 200, 15)
+ .WithSpan(196, 3, 201, 4)
+ .WithArguments("System.C.M1(String)",
+ "System.C.M1(String)",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 21)
- .WithArguments("System.C.M1(String)")});
+ .WithArguments("System.C.M1(String)")
+ });
}
[Fact]
@@ -1837,11 +1942,12 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
.WithArguments("System.C.M1()",
"System.C.M1()",
"'DynamicallyAccessedMemberTypes.PublicMethods'"),
- // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
- // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
- VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
- .WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")};
+ // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
+ // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
+ VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
+ .WithSpan(193, 4, 193, 16)
+ .WithArguments("System.C.M1()")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test), diag, diag);
}
@@ -1892,24 +1998,27 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(198,4): warning IL2084: value stored in field 'System.C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
- // The implicit 'this' argument of method 'System.C.M()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsField)
- .WithSpan(198, 4, 198, 12)
- .WithSpan(196, 3, 199, 4)
- .WithArguments("System.C.f",
- "System.C.M()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'") },
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(198,4): warning IL2084: value stored in field 'System.C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements.
+ // The implicit 'this' argument of method 'System.C.M()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsField)
+ .WithSpan(198, 4, 198, 12)
+ .WithSpan(196, 3, 199, 4)
+ .WithArguments("System.C.f",
+ "System.C.M()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 15)
- .WithArguments("System.C.M()")});
+ .WithArguments("System.C.M()")
+ });
}
[Fact]
@@ -1961,24 +2070,27 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
- string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
- new[] {
- // /0/Test0.cs(198,4): warning IL2085: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2()'.
- // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsThisParameter)
- .WithSpan(198, 4, 198, 13)
- .WithSpan(196, 3, 199, 4)
- .WithArguments("System.C.M2()",
- "System.C.M1()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
+ fixedSource: string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), fixtest),
+ baselineExpected: new[] {
+ // /0/Test0.cs(198,4): warning IL2085: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.C.M2()'.
+ // The implicit 'this' argument of method 'System.C.M1()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsThisParameter)
+ .WithSpan(198, 4, 198, 13)
+ .WithSpan(196, 3, 199, 4)
+ .WithArguments("System.C.M2()",
+ "System.C.M1()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
+ fixedExpected: new[] {
// /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
// and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
.WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")});
+ .WithArguments("System.C.M1()")
+ });
}
[Fact]
@@ -2016,11 +2128,12 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
.WithArguments("System.C.M2()",
"System.C.M1()",
"'DynamicallyAccessedMemberTypes.PublicMethods'"),
- // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
- // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
- VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
- .WithSpan(193, 4, 193, 16)
- .WithArguments("System.C.M1()")};
+ // /0/Test0.cs(193,4): warning IL2065: Value passed to implicit 'this' parameter of method 'System.C.M1()' can not be statically determined
+ // and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
+ VerifyCS.Diagnostic(DiagnosticId.ImplicitThisCannotBeStaticallyDetermined)
+ .WithSpan(193, 4, 193, 16)
+ .WithArguments("System.C.M1()")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test),
string.Concat (DynamicallyAccessedMembersAnalyzerTests.GetSystemTypeBase (), test), diag, diag);
}
@@ -2072,7 +2185,10 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
// /0/Test0.cs(18,3): warning IL2087: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'C.M1(Type)'.
// The generic parameter 'T' of 'C.M2<T>()' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
@@ -2083,7 +2199,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
"C.M1(Type)",
"T",
"C.M2<T>()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2126,7 +2243,10 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
// /0/Test0.cs(14,10): warning IL2088: 'C.M<T>()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' requirements.
// The generic parameter 'T' of 'C.M<T>()' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
@@ -2136,7 +2256,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
.WithArguments("C.M<T>()",
"T",
"C.M<T>()",
- "'DynamicallyAccessedMemberTypes.PublicConstructors'")},
+ "'DynamicallyAccessedMemberTypes.PublicConstructors'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2162,15 +2283,16 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(14,10): warning IL2088: 'C.M<T>()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' requirements.
- // The generic parameter 'T' of 'C.M<T>()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsMethodReturnType)
- .WithSpan(14, 10, 14, 19)
- .WithArguments("C.M<T>()",
- "T",
- "C.M<T>()",
- "'DynamicallyAccessedMemberTypes.PublicConstructors'")};
+ // /0/Test0.cs(14,10): warning IL2088: 'C.M<T>()' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' requirements.
+ // The generic parameter 'T' of 'C.M<T>()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsMethodReturnType)
+ .WithSpan(14, 10, 14, 19)
+ .WithArguments("C.M<T>()",
+ "T",
+ "C.M<T>()",
+ "'DynamicallyAccessedMemberTypes.PublicConstructors'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2207,15 +2329,19 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
private static Type f;
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(8,3): warning IL2089: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The generic parameter 'T' of 'C.Main<T>()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsField)
- .WithSpan(8, 3, 8, 16)
- .WithSpan(6, 26, 6, 27)
- .WithArguments("C.f",
- "T",
- "C.Main<T>()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,3): warning IL2089: value stored in field 'C.f' does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' requirements. The generic parameter 'T' of 'C.Main<T>()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsField)
+ .WithSpan(8, 3, 8, 16)
+ .WithSpan(6, 26, 6, 27)
+ .WithArguments("C.f",
+ "T",
+ "C.Main<T>()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2244,7 +2370,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
.WithArguments("C.f",
"T",
"C.Main<T>()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2276,8 +2403,10 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest,
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
// /0/Test0.cs(8,3): warning IL2090: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
// The generic parameter 'T' of 'C<T>' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
@@ -2306,18 +2435,18 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, test,
- new[] {
- // /0/Test0.cs(8,3): warning IL2090: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
- // The generic parameter 'T' of 'C<T>' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsThisParameter)
- .WithSpan(8, 3, 8, 25)
- .WithArguments("System.Type.GetMethods()",
- "T",
- "C<T>",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
- fixedExpected: Array.Empty<DiagnosticResult> ());
+ var diag = new[] {
+ // /0/Test0.cs(8,3): warning IL2090: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
+ // The generic parameter 'T' of 'C<T>' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsThisParameter)
+ .WithSpan(8, 3, 8, 25)
+ .WithArguments("System.Type.GetMethods()",
+ "T",
+ "C<T>",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
+ await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
[Fact]
@@ -2335,18 +2464,18 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, test,
- new[] {
- // /0/Test0.cs(8,3): warning IL2090: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
- // The generic parameter 'T' of 'C<T>' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsThisParameter)
- .WithSpan(8, 3, 8, 25)
- .WithArguments("System.Type.GetMethods()",
- "T",
- "C<T>",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
- fixedExpected: Array.Empty<DiagnosticResult> ());
+ var diag = new[] {
+ // /0/Test0.cs(8,3): warning IL2090: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'.
+ // The generic parameter 'T' of 'C<T>' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsThisParameter)
+ .WithSpan(8, 3, 8, 25)
+ .WithArguments("System.Type.GetMethods()",
+ "T",
+ "C<T>",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
+ await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
[Fact]
@@ -2392,8 +2521,10 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest,
- new[] {
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
// /0/Test0.cs(16,3): warning IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in 'C.M1<T>()'.
// The generic parameter 'S' of 'C.M2<S>()' does not have matching annotations.
// The source value must declare at least the same requirements as those declared on the target location it is assigned to.
@@ -2404,7 +2535,8 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
"C.M1<T>()",
"S",
"C.M2<S>()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")},
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2432,16 +2564,17 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(16,3): warning IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in 'C.M1<T>()'.
- // The generic parameter 'S' of 'C.M2<S>()' does not have matching annotations.
- // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter)
- .WithSpan(16, 3, 16, 8)
- .WithArguments("T",
- "C.M1<T>()",
- "S",
- "C.M2<S>()",
- "'DynamicallyAccessedMemberTypes.PublicMethods'")};
+ // /0/Test0.cs(16,3): warning IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in 'C.M1<T>()'.
+ // The generic parameter 'S' of 'C.M2<S>()' does not have matching annotations.
+ // The source value must declare at least the same requirements as those declared on the target location it is assigned to.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter)
+ .WithSpan(16, 3, 16, 8)
+ .WithArguments("T",
+ "C.M1<T>()",
+ "S",
+ "C.M2<S>()",
+ "'DynamicallyAccessedMemberTypes.PublicMethods'")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2483,10 +2616,10 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(11,30): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
- // don't match overridden parameter 't' of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
+ // /0/Test0.cs(11,30): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
+ // don't match overridden parameter 't' of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
.WithSpan(11, 30, 11, 31)
.WithSpan(11, 30, 11, 31)
.WithArguments("t",
@@ -2535,17 +2668,21 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
- // don't match overridden parameter 't' of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
- .WithSpan(11, 108, 11, 109)
- .WithSpan(6, 29, 6, 30)
- .WithArguments("t",
- "C.M(Type)",
- "t",
- "Base.M(Type)") },
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
+ // don't match overridden parameter 't' of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
+ .WithSpan(11, 108, 11, 109)
+ .WithSpan(6, 29, 6, 30)
+ .WithArguments("t",
+ "C.M(Type)",
+ "t",
+ "Base.M(Type)")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2571,15 +2708,16 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
- // don't match overridden parameter 't' of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
+ // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
+ // don't match overridden parameter 't' of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
.WithSpan(11, 108, 11, 109)
.WithArguments("t",
"C.M(Type)",
"t",
- "Base.M(Type)") };
+ "Base.M(Type)")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2605,16 +2743,17 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
- // don't match overridden parameter 't' of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
+ // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
+ // don't match overridden parameter 't' of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
.WithSpan(11, 30, 11, 31)
.WithSpan(11, 30, 11, 31)
.WithArguments("t",
"C.M(Type)",
"t",
- "Base.M(Type)") };
+ "Base.M(Type)")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2640,15 +2779,16 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
- // don't match overridden parameter 't' of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
+ // /0/Test0.cs(11,108): warning IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 't' of method 'C.M(Type)'
+ // don't match overridden parameter 't' of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides)
.WithSpan(11, 108, 11, 109)
.WithArguments("t",
"C.M(Type)",
"t",
- "Base.M(Type)") };
+ "Base.M(Type)")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2662,7 +2802,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
public class Base
{
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)]
- public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
+ public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
return t;
}
}
@@ -2684,7 +2824,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
public class Base
{
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)]
- public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
+ public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
return t;
}
}
@@ -2700,15 +2840,19 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(14,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
- // don't match overridden return value of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
- .WithSpan(14, 23, 14, 24)
- .WithSpan(14, 23, 14, 24)
- .WithArguments("C.M(Type)",
- "Base.M(Type)")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(14,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
+ // don't match overridden return value of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
+ .WithSpan(14, 23, 14, 24)
+ .WithSpan(14, 23, 14, 24)
+ .WithArguments("C.M(Type)",
+ "Base.M(Type)")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2721,7 +2865,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
public class Base
{
- public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
+ public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
return t;
}
}
@@ -2760,15 +2904,19 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
}
""";
- await VerifyDynamicallyAccessedMembersCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(14,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
- // don't match overridden return value of method 'Base.M(Type)'.
- // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
- VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
- .WithSpan(14, 23, 14, 24)
- .WithSpan(6, 25, 6, 26)
- .WithArguments("C.M(Type)",
- "Base.M(Type)")},
+ await VerifyDynamicallyAccessedMembersCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(14,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
+ // don't match overridden return value of method 'Base.M(Type)'.
+ // All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
+ VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
+ .WithSpan(14, 23, 14, 24)
+ .WithSpan(6, 22, 6, 23)
+ .WithArguments("C.M(Type)",
+ "Base.M(Type)")
+ },
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -2782,7 +2930,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
public class Base
{
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
- public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
+ public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
return t;
}
}
@@ -2799,13 +2947,14 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(15,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
+ // /0/Test0.cs(15,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
// don't match overridden return value of method 'Base.M(Type)'.
// All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
.WithSpan(15, 23, 15, 24)
.WithArguments("C.M(Type)",
- "Base.M(Type)")};
+ "Base.M(Type)")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
@@ -2819,7 +2968,7 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
public class Base
{
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.None)]
- public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
+ public virtual Type M([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type t) {
return t;
}
}
@@ -2836,13 +2985,14 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
}
""";
var diag = new[] {
- // /0/Test0.cs(15,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
+ // /0/Test0.cs(15,23): warning IL2093: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'C.M(Type)'
// don't match overridden return value of method 'Base.M(Type)'.
// All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
VerifyCS.Diagnostic(DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodReturnValueBetweenOverrides)
.WithSpan(15, 23, 15, 24)
.WithArguments("C.M(Type)",
- "Base.M(Type)")};
+ "Base.M(Type)")
+ };
await VerifyDynamicallyAccessedMembersCodeFix (test, test, diag, diag);
}
}
diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs
index 17e50f0c1..1d80ed7a6 100644
--- a/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs
+++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresAssemblyFilesAnalyzerTests.cs
@@ -59,230 +59,240 @@ build_property.{MSBuildPropertyOptionNames.EnableSingleFileAnalyzer} = true")));
[Fact]
public Task SimpleDiagnosticOnEvent ()
{
- var TestRequiresAssemblyFieldsOnEvent = @"
-#nullable enable
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- [RequiresAssemblyFiles]
- event System.EventHandler? E;
-
- void M()
- {
- var handler = E;
- }
-}";
+ var TestRequiresAssemblyFieldsOnEvent = $$"""
+ #nullable enable
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ [RequiresAssemblyFiles]
+ event System.EventHandler? E;
+
+ void M()
+ {
+ var handler = E;
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestRequiresAssemblyFieldsOnEvent,
- // (12,17): warning IL3002: Using member 'C.E' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (12, 17, 12, 18).WithArguments ("C.E", "", ""));
+ // (11,17): warning IL3002: Using member 'C.E' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (11, 17, 11, 18).WithArguments ("C.E", "", ""));
}
[Fact]
public Task SimpleDiagnosticOnProperty ()
{
- var TestRequiresAssemblyFilesOnProperty = @"
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- [RequiresAssemblyFiles]
- bool P { get; set; }
-
- void M()
- {
- P = false;
- List<bool> b = new List<bool> { P };
- }
-}";
+ var TestRequiresAssemblyFilesOnProperty = $$"""
+ using System.Collections.Generic;
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ [RequiresAssemblyFiles]
+ bool P { get; set; }
+
+ void M()
+ {
+ P = false;
+ List<bool> b = new List<bool> { P };
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestRequiresAssemblyFilesOnProperty,
// (11,3): warning IL3002: Using member 'C.P' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (12, 3, 12, 4).WithArguments ("C.P", "", ""),
- // (13,12): warning IL3002: Using member 'C.P' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (13, 35, 13, 36).WithArguments ("C.P", "", ""));
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (11, 3, 11, 4).WithArguments ("C.P", "", ""),
+ // (12,35): warning IL3002: Using member 'C.P' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (12, 35, 12, 36).WithArguments ("C.P", "", ""));
}
[Fact]
public Task CallDangerousMethodInsideProperty ()
{
- var TestRequiresAssemblyFilesOnMethodInsideProperty = @"
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- bool field;
-
- [RequiresAssemblyFiles]
- bool P {
- get {
- return field;
- }
- set {
- CallDangerousMethod ();
- field = value;
- }
- }
-
- [RequiresAssemblyFiles]
- void CallDangerousMethod () {}
-
- void M ()
- {
- P = false;
- }
-}";
+ var TestRequiresAssemblyFilesOnMethodInsideProperty = $$"""
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ bool field;
+
+ [RequiresAssemblyFiles]
+ bool P {
+ get {
+ return field;
+ }
+ set {
+ CallDangerousMethod ();
+ field = value;
+ }
+ }
+
+ [RequiresAssemblyFiles]
+ void CallDangerousMethod () {}
+
+ void M ()
+ {
+ P = false;
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestRequiresAssemblyFilesOnMethodInsideProperty,
- // (24,3): warning IL3002: Using member 'C.P' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (24, 3, 24, 4).WithArguments ("C.P", "", ""));
+ // (23,3): warning IL3002: Using member 'C.P' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (23, 3, 23, 4).WithArguments ("C.P", "", ""));
}
[Fact]
public Task RequiresAssemblyFilesWithUrlOnly ()
{
- var TestRequiresAssemblyFilesWithMessageAndUrl = @"
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- [RequiresAssemblyFiles (Url = ""https://helpurl"")]
- void M1()
- {
- }
-
- void M2()
- {
- M1();
- }
-}";
+ var TestRequiresAssemblyFilesWithMessageAndUrl = $$"""
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ [RequiresAssemblyFiles (Url = "https://helpurl")]
+ void M1()
+ {
+ }
+
+ void M2()
+ {
+ M1();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestRequiresAssemblyFilesWithMessageAndUrl,
- // (13,3): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. https://helpurl
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (13, 3, 13, 7).WithArguments ("C.M1()", "", " https://helpurl"));
+ // (12,3): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. https://helpurl
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (12, 3, 12, 7).WithArguments ("C.M1()", "", " https://helpurl"));
}
[Fact]
public Task NoDiagnosticIfMethodNotCalled ()
{
- var TestNoDiagnosticIfMethodNotCalled = @"
-using System.Diagnostics.CodeAnalysis;
+ var TestNoDiagnosticIfMethodNotCalled = $$"""
+ using System.Diagnostics.CodeAnalysis;
-class C
-{
- [RequiresAssemblyFiles]
- void M() { }
-}";
+ class C
+ {
+ [RequiresAssemblyFiles]
+ void M() { }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestNoDiagnosticIfMethodNotCalled);
}
[Fact]
public Task NoDiagnosticIsProducedIfCallerIsAnnotated ()
{
- var TestNoDiagnosticIsProducedIfCallerIsAnnotated = @"
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- void M1()
- {
- M2();
- }
-
- [RequiresAssemblyFiles (""Warn from M2"")]
- void M2()
- {
- M3();
- }
-
- [RequiresAssemblyFiles (""Warn from M3"")]
- void M3()
- {
- }
-}";
+ var TestNoDiagnosticIsProducedIfCallerIsAnnotated = $$"""
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ void M1()
+ {
+ M2();
+ }
+
+ [RequiresAssemblyFiles ("Warn from M2")]
+ void M2()
+ {
+ M3();
+ }
+
+ [RequiresAssemblyFiles ("Warn from M3")]
+ void M3()
+ {
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (TestNoDiagnosticIsProducedIfCallerIsAnnotated,
- // (8,3): warning IL3002: Using member 'C.M2()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. Warn from M2.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (8, 3, 8, 7).WithArguments ("C.M2()", " Warn from M2.", ""));
+ // (7,3): warning IL3002: Using member 'C.M2()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. Warn from M2.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (7, 3, 7, 7).WithArguments ("C.M2()", " Warn from M2.", ""));
}
[Fact]
public Task GetExecutingAssemblyLocation ()
{
- const string src = @"
-using System.Reflection;
-class C
-{
- public string M() => Assembly.GetExecutingAssembly().Location;
-}";
+ const string src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public string M() => Assembly.GetExecutingAssembly().Location;
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src,
// (5,26): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (5, 26, 5, 66).WithArguments ("System.Reflection.Assembly.Location"));
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (4, 23, 4, 63).WithArguments ("System.Reflection.Assembly.Location"));
}
[Fact]
public Task GetAssemblyLocationViaAssemblyProperties ()
{
- var src = @"
-using System.Reflection;
-class C
-{
- public void M()
- {
- var a = Assembly.GetExecutingAssembly();
- _ = a.Location;
- // below methods are marked as obsolete in 5.0
- // _ = a.CodeBase;
- // _ = a.EscapedCodeBase;
- }
-}";
+ var src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public void M()
+ {
+ var a = Assembly.GetExecutingAssembly();
+ _ = a.Location;
+ // below methods are marked as obsolete in 5.0
+ // _ = a.CodeBase;
+ // _ = a.EscapedCodeBase;
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src,
- // (8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.Assembly.Location")
+ // (7,7): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (7, 7, 7, 17).WithArguments ("System.Reflection.Assembly.Location")
);
}
[Fact]
public Task CallKnownDangerousAssemblyMethods ()
{
- var src = @"
-using System.Reflection;
-class C
-{
- public void M()
- {
- var a = Assembly.GetExecutingAssembly();
- _ = a.GetFile(""/some/file/path"");
- _ = a.GetFiles();
- }
-}";
+ var src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public void M()
+ {
+ var a = Assembly.GetExecutingAssembly();
+ _ = a.GetFile("/some/file/path");
+ _ = a.GetFiles();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src,
- // (8,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (8, 13, 8, 41).WithArguments ("System.Reflection.Assembly.GetFile(String)"),
- // (9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (9, 13, 9, 25).WithArguments ("System.Reflection.Assembly.GetFiles()")
+ // (7,7): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (7, 7, 7, 35).WithArguments ("System.Reflection.Assembly.GetFile(String)"),
+ // (8,7): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (8, 7, 8, 19).WithArguments ("System.Reflection.Assembly.GetFiles()")
);
}
[Fact]
public Task CallKnownDangerousAssemblyNameAttributes ()
{
- var src = @"
-using System.Reflection;
-class C
-{
- public void M()
- {
- var a = Assembly.GetExecutingAssembly().GetName();
- _ = a.CodeBase;
- _ = a.EscapedCodeBase;
- }
-}";
+ var src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public void M()
+ {
+ var a = Assembly.GetExecutingAssembly().GetName();
+ _ = a.CodeBase;
+ _ = a.EscapedCodeBase;
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src,
- // (8,13): warning IL3002: Using member 'System.Reflection.AssemblyName.CodeBase.get' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. The code will return an empty string for assemblies embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.AssemblyName.CodeBase.get", " The code will return an empty string for assemblies embedded in a single-file app.", ""),
- // (8,13): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.AssemblyName.CodeBase"),
- // (9,13): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (9, 13, 9, 30).WithArguments ("System.Reflection.AssemblyName.EscapedCodeBase")
+ // (7,7): warning IL3002: Using member 'System.Reflection.AssemblyName.CodeBase.get' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. The code will return an empty string for assemblies embedded in a single-file app.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (7, 7, 7, 17).WithArguments ("System.Reflection.AssemblyName.CodeBase.get", " The code will return an empty string for assemblies embedded in a single-file app.", ""),
+ // (7,7): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (7, 7, 7, 17).WithArguments ("System.Reflection.AssemblyName.CodeBase"),
+ // (8,7): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (8, 7, 8, 24).WithArguments ("System.Reflection.AssemblyName.EscapedCodeBase")
);
}
@@ -291,37 +301,39 @@ class C
{
// This is an OK use of Location and GetFile since these assemblies were loaded from
// a file, but the analyzer is conservative
- var src = @"
-using System.Reflection;
-class C
-{
- public void M()
- {
- var a = Assembly.LoadFrom(""/some/path/not/in/bundle"");
- _ = a.Location;
- _ = a.GetFiles();
- }
-}";
+ var src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public void M()
+ {
+ var a = Assembly.LoadFrom("/some/path/not/in/bundle");
+ _ = a.Location;
+ _ = a.GetFiles();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src,
- // (8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (8, 13, 8, 23).WithArguments ("System.Reflection.Assembly.Location"),
- // (9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (9, 13, 9, 25).WithArguments ("System.Reflection.Assembly.GetFiles()")
+ // (7,7): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (7, 7, 7, 17).WithArguments ("System.Reflection.Assembly.Location"),
+ // (8,7): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (8, 7, 8, 19).WithArguments ("System.Reflection.Assembly.GetFiles()")
);
}
[Fact]
public Task PublishSingleFileIsNotSet ()
{
- var src = @"
-using System.Reflection;
-class C
-{
- public void M()
- {
- var a = Assembly.GetExecutingAssembly().Location;
- }
-}";
+ var src = $$"""
+ using System.Reflection;
+ class C
+ {
+ public void M()
+ {
+ var a = Assembly.GetExecutingAssembly().Location;
+ }
+ }
+ """;
// If 'PublishSingleFile' is not set to true, no diagnostics should be produced by the analyzer. This will
// effectively verify that the number of produced diagnostics matches the number of expected ones (zero).
return VerifyCS.VerifyAnalyzerAsync (src);
@@ -330,21 +342,22 @@ class C
[Fact]
public Task SupressWarningsWithRequiresAssemblyFiles ()
{
- const string src = @"
-using System.Reflection;
-using System.Diagnostics.CodeAnalysis;
-class C
-{
- [RequiresAssemblyFiles]
- public void M()
- {
- var a = Assembly.GetExecutingAssembly();
- _ = a.Location;
- var b = Assembly.GetExecutingAssembly();
- _ = b.GetFile(""/some/file/path"");
- _ = b.GetFiles();
- }
-}";
+ const string src = $$"""
+ using System.Reflection;
+ using System.Diagnostics.CodeAnalysis;
+ class C
+ {
+ [RequiresAssemblyFiles]
+ public void M()
+ {
+ var a = Assembly.GetExecutingAssembly();
+ _ = a.Location;
+ var b = Assembly.GetExecutingAssembly();
+ _ = b.GetFile("/some/file/path");
+ _ = b.GetFiles();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesAnalyzer (src);
}
@@ -352,70 +365,70 @@ class C
[Fact]
public Task RequiresAssemblyFilesDiagnosticFix ()
{
- var test = @"
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
- int M2() => M1();
-}
-class D
-{
- public int M3(C c) => c.M1();
- public class E
- {
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- public int M5(C c) => c.M1();
- }
-}
-";
- var fixtest = @"
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
- [RequiresAssemblyFiles(""Calls C.M1()"")]
- int M2() => M1();
-}
-class D
-{
- [RequiresAssemblyFiles(""Calls C.M1()"")]
- public int M3(C c) => c.M1();
- public class E
- {
- [RequiresAssemblyFiles(""Calls C.M1()"")]
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- [RequiresAssemblyFiles()]
- public int M5(C c) => c.M1();
- }
-}
-";
+ var test = $$"""
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+ int M2() => M1();
+ }
+ class D
+ {
+ public int M3(C c) => c.M1();
+ public class E
+ {
+ public int M4(C c) => c.M1();
+ }
+ }
+ public class E
+ {
+ public class F
+ {
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
+ var fixtest = $$"""
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+ [RequiresAssemblyFiles("Calls C.M1()")]
+ int M2() => M1();
+ }
+ class D
+ {
+ [RequiresAssemblyFiles("Calls C.M1()")]
+ public int M3(C c) => c.M1();
+ public class E
+ {
+ [RequiresAssemblyFiles("Calls C.M1()")]
+ public int M4(C c) => c.M1();
+ }
+ }
+ public class E
+ {
+ public class F
+ {
+ [RequiresAssemblyFiles()]
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesCodeFix (
- test,
- fixtest,
- baselineExpected: new[] {
- // /0/Test0.cs(7,17): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (7, 17, 7, 21).WithArguments ("C.M1()", " message.", ""),
- // /0/Test0.cs(11,27): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (11, 27, 11, 33).WithArguments ("C.M1()", " message.", ""),
- // /0/Test0.cs(14,31): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (14, 31, 14, 37).WithArguments ("C.M1()", " message.", ""),
- // /0/Test0.cs(21,31): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (21, 31, 21, 37).WithArguments ("C.M1()", " message.", "")
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected:new[] {
+ // /0/Test0.cs(6,14): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(6, 14, 6, 18).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(10,24): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(10, 24, 10, 30).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(13,25): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(13, 25, 13, 31).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(20,25): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(20, 25, 20, 31).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -423,42 +436,42 @@ public class E
[Fact]
public Task FixInSingleFileSpecialCases ()
{
- var test = @"
-using System.Reflection;
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- public static Assembly assembly = Assembly.LoadFrom(""/some/path/not/in/bundle"");
- public string M1() => assembly.Location;
- public void M2() {
- _ = assembly.GetFiles();
- }
-}
-";
- var fixtest = @"
-using System.Reflection;
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- public static Assembly assembly = Assembly.LoadFrom(""/some/path/not/in/bundle"");
-
- [RequiresAssemblyFiles()]
- public string M1() => assembly.Location;
-
- [RequiresAssemblyFiles()]
- public void M2() {
- _ = assembly.GetFiles();
- }
-}
-";
+ var test = $$"""
+ using System.Reflection;
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ public static Assembly assembly = Assembly.LoadFrom("/some/path/not/in/bundle");
+ public string M1() => assembly.Location;
+ public void M2() {
+ _ = assembly.GetFiles();
+ }
+ }
+ """;
+ var fixtest = $$"""
+ using System.Reflection;
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ public static Assembly assembly = Assembly.LoadFrom("/some/path/not/in/bundle");
+
+ [RequiresAssemblyFiles()]
+ public string M1() => assembly.Location;
+
+ [RequiresAssemblyFiles()]
+ public void M2() {
+ _ = assembly.GetFiles();
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesCodeFix (
- test,
- fixtest,
+ source: test,
+ fixedSource: fixtest,
baselineExpected: new[] {
- // /0/Test0.cs(7,27): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (7, 27, 7, 44).WithArguments ("System.Reflection.Assembly.Location", "", ""),
- // /0/Test0.cs(9,13): warning IL3001: 'System.Reflection.Assembly.GetFiles()' will throw for assemblies embedded in a single-file app
- VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (9, 13, 9, 32).WithArguments("System.Reflection.Assembly.GetFiles()", "", ""),
+ // /0/Test0.cs(6,24): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyLocationInSingleFile).WithSpan (6, 24, 6, 41).WithArguments ("System.Reflection.Assembly.Location", "", ""),
+ // /0/Test0.cs(8,7): warning IL3001: 'System.Reflection.Assembly.GetFiles()' will throw for assemblies embedded in a single-file app
+ VerifyCS.Diagnostic (DiagnosticId.AvoidAssemblyGetFilesInSingleFile).WithSpan (8, 7, 8, 26).WithArguments("System.Reflection.Assembly.GetFiles()", "", ""),
},
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -466,35 +479,37 @@ public class C
[Fact]
public Task FixInPropertyDecl ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- int M2 => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
- [RequiresAssemblyFiles(""Calls C.M1()"")]
- int M2 => M1();
-}";
+ int M2 => M1();
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ [RequiresAssemblyFiles("Calls C.M1()")]
+ int M2 => M1();
+ }
+ """;
return VerifyRequiresAssemblyFilesCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(10,15): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(10, 15, 10, 19).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(9,12): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(9, 12, 9, 16).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -502,89 +517,72 @@ public class C
[Fact]
public Task FixInField ()
{
- const string src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-class C
-{
- public static Lazy<C> _default = new Lazy<C>(InitC);
- public static C Default => _default.Value;
-
- [RequiresAssemblyFiles]
- public static C InitC() {
- C cObject = new C();
- return cObject;
- }
-}";
- var fixtest = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-class C
-{
- public static Lazy<C> _default = new Lazy<C>(InitC);
- public static C Default => _default.Value;
-
- [RequiresAssemblyFiles]
- public static C InitC() {
- C cObject = new C();
- return cObject;
- }
-}";
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+ class C
+ {
+ public static Lazy<C> _default = new Lazy<C>(InitC);
+ public static C Default => _default.Value;
+
+ [RequiresAssemblyFiles]
+ public static C InitC() {
+ C cObject = new C();
+ return cObject;
+ }
+ }
+ """;
- return VerifyRequiresAssemblyFilesCodeFix (
- src,
- fixtest,
- baselineExpected: new[] {
- // /0/Test0.cs(6,50): warning IL3002: Using member 'C.InitC()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (6, 50, 6, 55).WithArguments ("C.InitC()", "", ""),
- },
- fixedExpected: new[] {
- // /0/Test0.cs(6,50): warning IL3002: Using member 'C.InitC()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
- VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (6, 50, 6, 55).WithArguments ("C.InitC()", "", ""),
- });
+ var diag = new[] {
+ // /0/Test0.cs(5,47): warning IL3002: Using member 'C.InitC()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresAssemblyFiles).WithSpan (5, 47, 5, 52).WithArguments ("C.InitC()", "", ""),
+ };
+ return VerifyRequiresAssemblyFilesCodeFix (src, src, diag, diag);
}
[Fact]
public Task FixInLocalFunc ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- void Wrapper () => M1();
- return Wrapper;
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- [RequiresAssemblyFiles(""Calls Wrapper()"")]
- Action M2()
- {
- [global::System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute(""Calls C.M1()"")] void Wrapper () => M1();
- return Wrapper;
- }
-}";
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ Action M2()
+ {
+ void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ [RequiresAssemblyFiles("Calls Wrapper()")]
+ Action M2()
+ {
+ [global::System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("Calls C.M1()")] void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
// Roslyn currently doesn't simplify the attribute name properly, see https://github.com/dotnet/roslyn/issues/52039
return VerifyRequiresAssemblyFilesCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(12,28): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(12, 28, 12, 32).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(11,22): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(11, 22, 11, 26).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> (),
numberOfIterations: 2);
@@ -593,35 +591,37 @@ public class C
[Fact]
public Task FixInCtor ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- public C () => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
- [RequiresAssemblyFiles()]
- public C () => M1();
-}";
+ public C () => M1();
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ [RequiresAssemblyFiles()]
+ public C () => M1();
+ }
+ """;
return VerifyRequiresAssemblyFilesCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(10,15): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(10, 20, 10, 24).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(9,17): warning IL3002: Using member 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when embedded in a single-file app. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(9, 17, 9, 21).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> ());
}
@@ -629,49 +629,51 @@ public class C
[Fact]
public Task FixInEvent ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- public event EventHandler E1
- {
- add
- {
- var a = M1();
- }
- remove { }
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresAssemblyFiles(""message"")]
- public int M1() => 0;
-
- [RequiresAssemblyFiles()]
- public event EventHandler E1
- {
- add
- {
- var a = M1();
- }
- remove { }
- }
-}";
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ public event EventHandler E1
+ {
+ add
+ {
+ var a = M1();
+ }
+ remove { }
+ }
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresAssemblyFiles("message")]
+ public int M1() => 0;
+
+ [RequiresAssemblyFiles()]
+ public event EventHandler E1
+ {
+ add
+ {
+ var a = M1();
+ }
+ remove { }
+ }
+ }
+ """;
return VerifyRequiresAssemblyFilesCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(14,21): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(14, 21, 14, 25).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(13,12): warning IL3002: Using method 'C.M1()' which has 'RequiresAssemblyFilesAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresAssemblyFiles).WithSpan(13, 12, 13, 16).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> ());
}
diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs
index a2977b3b9..cf55055dd 100644
--- a/test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs
+++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresDynamicCodeAnalyzerTests.cs
@@ -62,167 +62,156 @@ build_property.{MSBuildPropertyOptionNames.EnableAotAnalyzer} = true")));
[Fact]
public async Task SimpleDiagnosticFix ()
{
- var test = @"
-using System.Diagnostics.CodeAnalysis;
+ var test = $$"""
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
- int M2() => M1();
-}
-class D
-{
- public int M3(C c) => c.M1();
+ int M2() => M1();
+ }
+ class D
+ {
+ public int M3(C c) => c.M1();
+
+ public class E
+ {
+ public int M4(C c) => c.M1();
+ }
+ }
+ public class E
+ {
+ public class F
+ {
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
- public class E
- {
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- public int M5(C c) => c.M1();
- }
-}
-";
+ var fixtest = $$"""
+ using System.Diagnostics.CodeAnalysis;
- var fixtest = @"
-using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- [RequiresDynamicCode(""Calls C.M1()"")]
- int M2() => M1();
-}
-class D
-{
- [RequiresDynamicCode(""Calls C.M1()"")]
- public int M3(C c) => c.M1();
-
- public class E
- {
- [RequiresDynamicCode(""Calls C.M1()"")]
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- [RequiresDynamicCode()]
- public int M5(C c) => c.M1();
- }
-}
-";
-
- await VerifyRequiresDynamicCodeCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(9,17): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(9, 17, 9, 21).WithArguments("C.M1()", " message.", ""),
- // /0/Test0.cs(13,27): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(13, 27, 13, 33).WithArguments("C.M1()", " message.", ""),
- // /0/Test0.cs(17,31): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(17, 31, 17, 37).WithArguments("C.M1()", " message.", ""),
- // /0/Test0.cs(24,31): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(24, 31, 24, 37).WithArguments("C.M1()", " message.", "")
- }, new[] {
- // /0/Test0.cs(27,10): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)'
- DiagnosticResult.CompilerError("CS7036").WithSpan(27, 10, 27, 31).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)"),
+ [RequiresDynamicCode("Calls C.M1()")]
+ int M2() => M1();
+ }
+ class D
+ {
+ [RequiresDynamicCode("Calls C.M1()")]
+ public int M3(C c) => c.M1();
+
+ public class E
+ {
+ [RequiresDynamicCode("Calls C.M1()")]
+ public int M4(C c) => c.M1();
+ }
}
- );
+ public class E
+ {
+ public class F
+ {
+ [RequiresDynamicCode()]
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
+
+ await VerifyRequiresDynamicCodeCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,14): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(8, 14, 8, 18).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(12,24): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(12, 24, 12, 30).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(16,25): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(16, 25, 16, 31).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(23,25): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(23, 25, 23, 31).WithArguments("C.M1()", " message.", "")
+ },
+ fixedExpected: new[] {
+ // /0/Test0.cs(26,10): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)'
+ DiagnosticResult.CompilerError("CS7036").WithSpan(26, 10, 26, 31).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)"),
+ });
}
[Fact]
public Task FixInLambda ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- return () => M1();
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- return () => M1();
- }
-}";
- // No fix available inside a lambda, requries manual code change since attribute cannot
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
+
+ Action M2()
+ {
+ return () => M1();
+ }
+ }
+ """;
+ var diag = new[] {
+ // /0/Test0.cs(11,16): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(11, 16, 11, 20).WithArguments("C.M1()", " message.", "")
+ };
+ // No fix available inside a lambda, requires manual code change since attribute cannot
// be applied
- return VerifyRequiresDynamicCodeCodeFix (
- src,
- fix,
- baselineExpected: new[] {
- // /0/Test0.cs(12,22): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(12, 22, 12, 26).WithArguments("C.M1()", " message.", "")
- },
- fixedExpected: Array.Empty<DiagnosticResult> ());
+ return VerifyRequiresDynamicCodeCodeFix (src, src, diag, diag);
}
[Fact]
public Task FixInLocalFunc ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- void Wrapper () => M1();
- return Wrapper;
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- [RequiresDynamicCode(""Calls Wrapper()"")]
- Action M2()
- {
- [global::System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute(""Calls C.M1()"")] void Wrapper () => M1();
- return Wrapper;
- }
-}";
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
+
+ Action M2()
+ {
+ void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
+
+ [RequiresDynamicCode("Calls Wrapper()")]
+ Action M2()
+ {
+ [global::System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Calls C.M1()")] void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
// Roslyn currently doesn't simplify the attribute name properly, see https://github.com/dotnet/roslyn/issues/52039
return VerifyRequiresDynamicCodeCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(12,28): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(12, 28, 12, 32).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(11,22): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(11, 22, 11, 26).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> (),
// The default iterations for the codefix is the number of diagnostics (1 in this case)
@@ -234,80 +223,66 @@ public class C
[Fact]
public Task FixInCtor ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public static int M1() => 0;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
- public C() => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public static int M1() => 0;
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public static int M1() => 0;
- [RequiresDynamicCode()]
- public C() => M1();
-}";
+ public C() => M1();
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public static int M1() => 0;
+
+ [RequiresDynamicCode()]
+ public C() => M1();
+ }
+ """;
// Roslyn currently doesn't simplify the attribute name properly, see https://github.com/dotnet/roslyn/issues/52039
return VerifyRequiresDynamicCodeCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(10,19): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(10, 19, 10, 23).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(9,16): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(9, 16, 9, 20).WithArguments("C.M1()", " message.", "")
},
fixedExpected: new[] {
- // /0/Test0.cs(10,6): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)'
- DiagnosticResult.CompilerError("CS7036").WithSpan(10, 6, 10, 27).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)")
+ // /0/Test0.cs(9,6): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)'
+ DiagnosticResult.CompilerError("CS7036").WithSpan(9, 6, 9, 27).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.RequiresDynamicCodeAttribute(string)")
});
}
[Fact]
public Task FixInPropertyDecl ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
-
- int M2 => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresDynamicCodeAttribute(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresDynamicCodeAttribute("message")]
+ public int M1() => 0;
- int M2 => M1();
-}";
+ int M2 => M1();
+ }
+ """;
+ var diag = new[] {
+ // /0/Test0.cs(9,12): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(9, 12, 9, 16).WithArguments("C.M1()", " message.", "")
+ };
// Can't apply RDC on properties at the moment
- return VerifyRequiresDynamicCodeCodeFix (
- src,
- fix,
- baselineExpected: new[] {
- // /0/Test0.cs(10,15): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(10, 15, 10, 19).WithArguments("C.M1()", " message.", "")
- },
- fixedExpected: new[] {
- // /0/Test0.cs(10,15): warning IL3050: Using member 'C.M1()' which has 'RequiresDynamicCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresDynamicCode).WithSpan(10, 15, 10, 19).WithArguments("C.M1()", " message.", "")
- });
+ return VerifyRequiresDynamicCodeCodeFix (src, src, diag, diag);
}
}
}
diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs
index f758fa8e0..2576b1401 100644
--- a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs
+++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs
@@ -57,204 +57,197 @@ build_property.{MSBuildPropertyOptionNames.EnableTrimAnalyzer} = true")));
[Fact]
public async Task WarningInArgument ()
{
- var test = """
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCode("message")]
- public int M1() => 0;
- public void M2(int x)
- {
- }
- public void M3() => M2(M1());
-}
-""";
- var fixtest = """
-using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCode("message")]
- public int M1() => 0;
- public void M2(int x)
- {
- }
-
- [RequiresUnreferencedCode()]
- public void M3() => M2(M1());
-}
-""";
- await VerifyRequiresUnreferencedCodeCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(9,25): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(9, 25, 9, 29).WithArguments("C.M1()", " message.", ""),
- }, new[] {
- // /0/Test0.cs(10,6): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
- DiagnosticResult.CompilerError("CS7036").WithSpan(10, 6, 10, 32).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)"),
- });
+ var test = $$"""
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ [RequiresUnreferencedCode("message")]
+ public int M1() => 0;
+ public void M2(int x)
+ {
+ }
+ public void M3() => M2(M1());
+ }
+ """;
+ var fixtest = $$"""
+ using System.Diagnostics.CodeAnalysis;
+ public class C
+ {
+ [RequiresUnreferencedCode("message")]
+ public int M1() => 0;
+ public void M2(int x)
+ {
+ }
+
+ [RequiresUnreferencedCode()]
+ public void M3() => M2(M1());
+ }
+ """;
+ await VerifyRequiresUnreferencedCodeCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(9,25): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(9, 25, 9, 29).WithArguments("C.M1()", " message.", ""),
+ },
+ fixedExpected: new[] {
+ // /0/Test0.cs(10,3): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
+ DiagnosticResult.CompilerError("CS7036").WithSpan(10, 6, 10, 32).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)"),
+ });
}
[Fact]
public async Task SimpleDiagnosticFix ()
{
- var test = @"
-using System.Diagnostics.CodeAnalysis;
+ var test = $$"""
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- int M2() => M1();
-}
-class D
-{
- public int M3(C c) => c.M1();
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
- public class E
- {
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- public int M5(C c) => c.M1();
- }
-}
-";
+ int M2() => M1();
+ }
+ class D
+ {
+ public int M3(C c) => c.M1();
+
+ public class E
+ {
+ public int M4(C c) => c.M1();
+ }
+ }
+ public class E
+ {
+ public class F
+ {
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
- var fixtest = @"
-using System.Diagnostics.CodeAnalysis;
+ var fixtest = $$"""
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
- [RequiresUnreferencedCode(""Calls C.M1()"")]
- int M2() => M1();
-}
-class D
-{
- [RequiresUnreferencedCode(""Calls C.M1()"")]
- public int M3(C c) => c.M1();
-
- public class E
- {
- [RequiresUnreferencedCode(""Calls C.M1()"")]
- public int M4(C c) => c.M1();
- }
-}
-public class E
-{
- public class F
- {
- [RequiresUnreferencedCode()]
- public int M5(C c) => c.M1();
- }
-}
-";
-
- await VerifyRequiresUnreferencedCodeCodeFix (test, fixtest, new[] {
- // /0/Test0.cs(9,17): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (9, 17, 9, 21).WithArguments ("C.M1()", " message.", ""),
- // /0/Test0.cs(13,27): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(13, 27, 13, 33).WithArguments("C.M1()", " message.", ""),
- // /0/Test0.cs(17,31): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (17, 31, 17, 37).WithArguments ("C.M1()", " message.", ""),
- // /0/Test0.cs(24,31): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (24, 31, 24, 37).WithArguments ("C.M1()", " message.", "")
- }, new[] {
- // /0/Test0.cs(27,10): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
- DiagnosticResult.CompilerError("CS7036").WithSpan(27, 10, 27, 36).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)"),
+ [RequiresUnreferencedCode("Calls C.M1()")]
+ int M2() => M1();
}
- );
+ class D
+ {
+ [RequiresUnreferencedCode("Calls C.M1()")]
+ public int M3(C c) => c.M1();
+
+ public class E
+ {
+ [RequiresUnreferencedCode("Calls C.M1()")]
+ public int M4(C c) => c.M1();
+ }
+ }
+ public class E
+ {
+ public class F
+ {
+ [RequiresUnreferencedCode()]
+ public int M5(C c) => c.M1();
+ }
+ }
+ """;
+
+ await VerifyRequiresUnreferencedCodeCodeFix (
+ source: test,
+ fixedSource: fixtest,
+ baselineExpected: new[] {
+ // /0/Test0.cs(8,14): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (8, 14, 8, 18).WithArguments ("C.M1()", " message.", ""),
+ // /0/Test0.cs(12,24): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan (12, 24, 12, 30).WithArguments("C.M1()", " message.", ""),
+ // /0/Test0.cs(16,25): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (16, 25, 16, 31).WithArguments ("C.M1()", " message.", ""),
+ // /0/Test0.cs(23,25): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic (DiagnosticId.RequiresUnreferencedCode).WithSpan (23, 25, 23, 31).WithArguments ("C.M1()", " message.", "")
+ },
+ fixedExpected: new[] {
+ // /0/Test0.cs(26,10): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
+ DiagnosticResult.CompilerError("CS7036").WithSpan(26, 10, 26, 36).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)"),
+ });
}
[Fact]
public Task FixInLambda ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- return () => M1();
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- return () => M1();
- }
-}";
- // No fix available inside a lambda, requries manual code change since attribute cannot
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
+
+ Action M2()
+ {
+ return () => M1();
+ }
+ }
+ """;
+ var diag = new[] {
+ // /0/Test0.cs(11,16): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(11, 16, 11, 20).WithArguments("C.M1()", " message.", "")
+ };
+ // No fix available inside a lambda, requires manual code change since attribute cannot
// be applied
- return VerifyRequiresUnreferencedCodeCodeFix (
- src,
- fix,
- baselineExpected: new[] {
- // /0/Test0.cs(12,22): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(12, 22, 12, 26).WithArguments("C.M1()", " message.", "")
- },
- fixedExpected: Array.Empty<DiagnosticResult> ());
+ return VerifyRequiresUnreferencedCodeCodeFix (src, src, diag, diag);
}
[Fact]
public Task FixInLocalFunc ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- Action M2()
- {
- void Wrapper () => M1();
- return Wrapper;
- }
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- [RequiresUnreferencedCode(""Calls Wrapper()"")]
- Action M2()
- {
- [global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute(""Calls C.M1()"")] void Wrapper () => M1();
- return Wrapper;
- }
-}";
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
+
+ Action M2()
+ {
+ void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
+
+ [RequiresUnreferencedCode("Calls Wrapper()")]
+ Action M2()
+ {
+ [global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Calls C.M1()")] void Wrapper () => M1();
+ return Wrapper;
+ }
+ }
+ """;
// Roslyn currently doesn't simplify the attribute name properly, see https://github.com/dotnet/roslyn/issues/52039
return VerifyRequiresUnreferencedCodeCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(12,28): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(12, 28, 12, 32).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(11,22): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(11, 22, 11, 26).WithArguments("C.M1()", " message.", "")
},
fixedExpected: Array.Empty<DiagnosticResult> (),
// The default iterations for the codefix is the number of diagnostics (1 in this case)
@@ -266,134 +259,121 @@ public class C
[Fact]
public Task FixInCtor ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public static int M1() => 0;
-
- public C() => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public static int M1() => 0;
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public static int M1() => 0;
- [RequiresUnreferencedCode()]
- public C() => M1();
-}";
+ public C() => M1();
+ }
+ """;
+ var fix = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public static int M1() => 0;
+
+ [RequiresUnreferencedCode()]
+ public C() => M1();
+ }
+ """;
// Roslyn currently doesn't simplify the attribute name properly, see https://github.com/dotnet/roslyn/issues/52039
return VerifyRequiresUnreferencedCodeCodeFix (
- src,
- fix,
+ source: src,
+ fixedSource: fix,
baselineExpected: new[] {
- // /0/Test0.cs(10,19): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(10, 19, 10, 23).WithArguments("C.M1()", " message.", "")
+ // /0/Test0.cs(9,16): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(9, 16, 9, 20).WithArguments("C.M1()", " message.", "")
},
fixedExpected: new[] {
- // /0/Test0.cs(10,6): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
- DiagnosticResult.CompilerError("CS7036").WithSpan(10, 6, 10, 32).WithArguments("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)"),
+ // /0/Test0.cs(9,3): error CS7036: There is no argument given that corresponds to the required formal parameter 'message' of 'RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)'
+ DiagnosticResult.CompilerError ("CS7036").WithSpan (9, 6, 9, 32).WithArguments ("message", "System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute.RequiresUnreferencedCodeAttribute(string)")
});
}
[Fact]
public Task FixInPropertyDecl ()
{
- var src = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
-
- int M2 => M1();
-}";
- var fix = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
+ var src = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
-public class C
-{
- [RequiresUnreferencedCodeAttribute(""message"")]
- public int M1() => 0;
+ public class C
+ {
+ [RequiresUnreferencedCodeAttribute("message")]
+ public int M1() => 0;
- int M2 => M1();
-}";
+ int M2 => M1();
+ }
+ """;
+ var diag = new[] {
+ // /0/Test0.cs(10,15): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
+ VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(9, 12, 9, 16).WithArguments("C.M1()", " message.", "")
+ };
// Can't apply RUC on properties at the moment
- return VerifyRequiresUnreferencedCodeCodeFix (
- src,
- fix,
- baselineExpected: new[] {
- // /0/Test0.cs(10,15): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(10, 15, 10, 19).WithArguments("C.M1()", " message.", "")
- },
- fixedExpected: new[] {
- // /0/Test0.cs(10,15): warning IL2026: Using member 'C.M1()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. message.
- VerifyCS.Diagnostic(DiagnosticId.RequiresUnreferencedCode).WithSpan(10, 15, 10, 19).WithArguments("C.M1()", " message.", "")
- });
+ return VerifyRequiresUnreferencedCodeCodeFix (src, src, diag, diag);
}
[Fact]
public Task InvocationOnDynamicType ()
{
- var source = @"
-using System;
-class C
-{
- static void M0 ()
- {
- dynamic dynamicField = ""Some string"";
- Console.WriteLine (dynamicField);
- }
-
- static void M1 ()
- {
- MethodWithDynamicArgDoNothing (0);
- MethodWithDynamicArgDoNothing (""Some string"");
- MethodWithDynamicArg(-1);
- }
-
- static void MethodWithDynamicArgDoNothing (dynamic arg)
- {
- }
-
- static void MethodWithDynamicArg (dynamic arg)
- {
- arg.MethodWithDynamicArg (arg);
- }
-}";
+ var source = $$"""
+ using System;
+ class C
+ {
+ static void M0 ()
+ {
+ dynamic dynamicField = "Some string";
+ Console.WriteLine (dynamicField);
+ }
+
+ static void M1 ()
+ {
+ MethodWithDynamicArgDoNothing (0);
+ MethodWithDynamicArgDoNothing ("Some string");
+ MethodWithDynamicArg(-1);
+ }
+
+ static void MethodWithDynamicArgDoNothing (dynamic arg)
+ {
+ }
+
+ static void MethodWithDynamicArg (dynamic arg)
+ {
+ arg.MethodWithDynamicArg (arg);
+ }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source,
// (8,3): warning IL2026: Invoking members on dynamic types is not trimming safe. Types or members might have been removed by the trimmer.
- VerifyCS.Diagnostic (dynamicInvocationDiagnosticDescriptor).WithSpan (8, 3, 8, 35),
+ VerifyCS.Diagnostic (dynamicInvocationDiagnosticDescriptor).WithSpan (7, 3, 7, 35),
// (24,3): warning IL2026: Invoking members on dynamic types is not trimming safe. Types or members might have been removed by the trimmer.
- VerifyCS.Diagnostic (dynamicInvocationDiagnosticDescriptor).WithSpan (24, 3, 24, 33));
+ VerifyCS.Diagnostic (dynamicInvocationDiagnosticDescriptor).WithSpan (23, 3, 23, 33));
}
[Fact]
public Task DynamicInRequiresUnreferencedCodeClass ()
{
- var source = @"
-using System.Diagnostics.CodeAnalysis;
-
-[RequiresUnreferencedCode(""message"")]
-class ClassWithRequires
-{
- public static void MethodWithDynamicArg (dynamic arg)
- {
- arg.DynamicInvocation ();
- }
-}
-";
+ var source = $$"""
+ using System.Diagnostics.CodeAnalysis;
+
+ [RequiresUnreferencedCode("message")]
+ class ClassWithRequires
+ {
+ public static void MethodWithDynamicArg (dynamic arg)
+ {
+ arg.DynamicInvocation ();
+ }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
@@ -401,18 +381,19 @@ class ClassWithRequires
[Fact]
public Task InvocationOnDynamicTypeInMethodWithRUCDoesNotWarnTwoTimes ()
{
- var source = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-class C
-{
- [RequiresUnreferencedCode (""We should only see the warning related to this annotation, and none about the dynamic type."")]
- static void M0 ()
- {
- dynamic dynamicField = ""Some string"";
- Console.WriteLine (dynamicField);
- }
-}";
+ var source = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+ class C
+ {
+ [RequiresUnreferencedCode ("We should only see the warning related to this annotation, and none about the dynamic type.")]
+ static void M0 ()
+ {
+ dynamic dynamicField = "Some string";
+ Console.WriteLine (dynamicField);
+ }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
@@ -420,23 +401,24 @@ class C
[Fact]
public Task TestMakeGenericMethodUsage ()
{
- var source = @"
-using System.Diagnostics.CodeAnalysis;
-using System.Reflection;
-
-class C
-{
- static void M1 (MethodInfo methodInfo)
- {
- methodInfo.MakeGenericMethod (typeof (C));
- }
-
- [RequiresUnreferencedCode (""Message from RUC"")]
- static void M2 (MethodInfo methodInfo)
- {
- methodInfo.MakeGenericMethod (typeof (C));
- }
-}";
+ var source = $$"""
+ using System.Diagnostics.CodeAnalysis;
+ using System.Reflection;
+
+ class C
+ {
+ static void M1 (MethodInfo methodInfo)
+ {
+ methodInfo.MakeGenericMethod (typeof (C));
+ }
+
+ [RequiresUnreferencedCode ("Message from RUC")]
+ static void M2 (MethodInfo methodInfo)
+ {
+ methodInfo.MakeGenericMethod (typeof (C));
+ }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
@@ -444,23 +426,24 @@ class C
[Fact]
public Task TestMakeGenericTypeUsage ()
{
- var source = @"
-using System;
-using System.Diagnostics.CodeAnalysis;
-
-class C
-{
- static void M1 (Type t)
- {
- typeof (Nullable<>).MakeGenericType (typeof (C));
- }
-
- [RequiresUnreferencedCode (""Message from RUC"")]
- static void M2 (Type t)
- {
- typeof (Nullable<>).MakeGenericType (typeof (C));
- }
-}";
+ var source = $$"""
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+
+ class C
+ {
+ static void M1 (Type t)
+ {
+ typeof (Nullable<>).MakeGenericType (typeof (C));
+ }
+
+ [RequiresUnreferencedCode ("Message from RUC")]
+ static void M2 (Type t)
+ {
+ typeof (Nullable<>).MakeGenericType (typeof (C));
+ }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
@@ -468,14 +451,15 @@ class C
[Fact]
public Task VerifyThatAnalysisOfFieldsDoesNotNullRef ()
{
- var source = @"
-using System.Diagnostics.CodeAnalysis;
+ var source = $$"""
+ using System.Diagnostics.CodeAnalysis;
-[DynamicallyAccessedMembers (field)]
-class C
-{
- public const DynamicallyAccessedMemberTypes field = DynamicallyAccessedMemberTypes.PublicMethods;
-}";
+ [DynamicallyAccessedMembers (field)]
+ class C
+ {
+ public const DynamicallyAccessedMemberTypes field = DynamicallyAccessedMemberTypes.PublicMethods;
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
@@ -483,15 +467,15 @@ class C
[Fact]
public Task TestPropertyAssignmentInAssemblyAttribute ()
{
- var source = @"
-using System;
-[assembly: MyAttribute (Value = 5)]
+ var source = $$"""
+ using System;
+ [assembly: MyAttribute (Value = 5)]
-class MyAttribute : Attribute
-{
- public int Value { get; set; }
-}
-";
+ class MyAttribute : Attribute
+ {
+ public int Value { get; set; }
+ }
+ """;
return VerifyRequiresUnreferencedCodeAnalyzer (source);
}
}