Skip to content

Commit 4e52050

Browse files
committed
Exclude anonymous types from MA0150
1 parent b06406f commit 4e52050

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/Meziantou.Analyzer/Rules/DoNotUseToStringIfObjectAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ private void AnalyzeExpression(DiagnosticReporter reporter, IOperation operation
110110
if (actualType is null)
111111
return;
112112

113+
if (actualType.IsAnonymousType)
114+
return;
115+
113116
if (actualType.IsSealed) // Method cannot be overridden
114117
{
115118
if (!OverrideToString(actualType))

tests/Meziantou.Analyzer.Test/Rules/DoNotUseToStringIfObjectAnalyzerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ public async Task Interpolation_Enum()
277277
var sourceCode = """
278278
var statusCode = System.Net.HttpStatusCode.OK;
279279
_ = $"{statusCode}";
280+
""";
281+
await CreateProjectBuilder()
282+
.WithSourceCode(sourceCode)
283+
.ValidateAsync();
284+
}
285+
286+
[Fact]
287+
public async Task Interpolation_AnonymousType()
288+
{
289+
var sourceCode = """
290+
var obj = new { FirstName = "" };
291+
_ = $"{obj}";
280292
""";
281293
await CreateProjectBuilder()
282294
.WithSourceCode(sourceCode)

0 commit comments

Comments
 (0)