Skip to content

Commit

Permalink
Merge pull request #7044 from CollinAlpert/issue-7033
Browse files Browse the repository at this point in the history
Don't raise CA1861 in for attribute arguments
  • Loading branch information
mavasani authored Nov 30, 2023
2 parents d0e4558 + 2166899 commit 1a7c304
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public override void Initialize(AnalysisContext context)
var knownTypeProvider = WellKnownTypeProvider.GetOrCreate(context.Compilation);
INamedTypeSymbol? readonlySpanType = knownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReadOnlySpan1);
INamedTypeSymbol? functionType = knownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemFunc2);
INamedTypeSymbol? attributeType = knownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemAttribute);

// Analyzes an argument operation
context.RegisterOperationAction(context =>
Expand All @@ -59,7 +60,10 @@ public override void Initialize(AnalysisContext context)

// If no argument, return
// If argument is passed as a params array but isn't itself an array, return
if (argumentOperation?.Parameter is null || (argumentOperation.Parameter.IsParams && arrayCreationOperation.IsImplicit))
// If array is declared as an attribute argument, return
if (argumentOperation?.Parameter is null
|| (argumentOperation.Parameter.IsParams && arrayCreationOperation.IsImplicit)
|| (argumentOperation.Parent is IObjectCreationOperation objectCreation && objectCreation.Type.Inherits(attributeType)))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,5 +957,70 @@ void A(char separatorArray0)

return VerifyCS.VerifyCodeFixAsync(source, fixedSource);
}

[Fact, WorkItem(7033, "https://github.com/dotnet/roslyn-analyzers/issues/7033")]
public Task ArrayAsAttributeParameter_NoDiagnostic()
{
const string source = """
using System;
class Sample
{
[MyAttribute(new[] {"a", "b", "c"})]
void M()
{
}
}
class MyAttribute : Attribute
{
public MyAttribute(string[] array) {}
}
""";

return VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact, WorkItem(7033, "https://github.com/dotnet/roslyn-analyzers/issues/7033")]
public Task ArrayAsNamedAttributeParameter_NoDiagnostic()
{
const string source = """
using System;
class Sample
{
[MyAttribute(Values = new[] {"a", "b", "c"})]
void M()
{
}
}
class MyAttribute : Attribute
{
public string[] Values { get; set; }
}
""";

return VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact, WorkItem(7033, "https://github.com/dotnet/roslyn-analyzers/issues/7033")]
public Task ArrayAsAttributeParameter_Xunit_NoDiagnostic()
{
const string source = """
public class Test
{
[Xunit.Theory]
[Xunit.InlineData("a", new[] { "b" })]
public void Method(string a, string[] b) { }
}
""";

return new VerifyCS.Test
{
TestCode = source,
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithXUnit
}.RunAsync();
}
}
}

0 comments on commit 1a7c304

Please sign in to comment.