Skip to content

Commit

Permalink
fix: CA1859 false positive for anonymous types
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Shephard committed Dec 14, 2024
1 parent 7f449a5 commit 90967bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ void Evaluate(ISymbol affectedSymbol, ITypeSymbol fromType, PooledConcurrentSet<
}
}

if (toType.IsAnonymousType)
{
// don't recommend upgrading to an anonymous (i.e. invalid) type
return;
}

if (toType.TypeKind is not TypeKind.Class and not TypeKind.Array and not TypeKind.Struct)
{
// we only deal with classes, arrays, or structs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,22 @@ private void M(ValueTuple<Action> vt)
await TestCSAsync(Source);
}

[Fact, WorkItem(7504, "https://github.com/dotnet/roslyn-analyzers/issues/7504")]
public static async Task ShouldNotSuggestChangingToAnonymousType()
{
const string Source = @"
class Foo
{
object Bar()
{
var outputData = new { A = 1, B = 2 };
return outputData;
}
}";

await TestCSAsync(Source);
}

private static async Task TestCSAsync(string source, params DiagnosticResult[] diagnosticResults)
{
var test = new VerifyCS.Test
Expand Down

0 comments on commit 90967bd

Please sign in to comment.