Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CA1859 false positive for anonymous types #7505

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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