Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ IEnumerator Coroutine2()
await VerifyCSharpFixAsync(test, fixedTest);
}

[Fact]
public async Task NonUnityTypeWithMultipleLiteralArguments()
{
// See https://github.com/microsoft/Microsoft.Unity.Analyzers/issues/471
// The analyzer used to throw AD0001 (InvalidOperationException: Sequence contains more than one matching element)
// when a yield return expression created an object with more than one literal argument.
const string test = @"
using System.Collections.Generic;

public struct A
{
public A(int a, int b)
{
}

public static IEnumerable<A> GetObjs()
{
yield return new A(23, 23);
}
}
";

await VerifyCSharpDiagnosticAsync(test);
}

[Fact]
public async Task WaitForSecondsAndRealtime()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ private static void AnalyzeYieldReturn(SyntaxNodeAnalysisContext context)
if (yieldStatement.Expression is not ObjectCreationExpressionSyntax objectCreation)
return;

if (objectCreation.ArgumentList?.Arguments.SingleOrDefault(arg => arg.Expression is LiteralExpressionSyntax) == null)
// The code fix only supports caching when there's exactly one literal argument (see issue #471).
if (objectCreation.ArgumentList?.Arguments is not { Count: 1 } arguments)
return;

if (arguments[0].Expression is not LiteralExpressionSyntax)
return;

var typeSymbol = context.SemanticModel.GetTypeInfo(objectCreation).Type;
Expand Down
Loading