Skip to content

Commit 2d59990

Browse files
committed
fix(analyzer): don't raise COA1001 for ItemResult pass to object parameter
1 parent d8c5993 commit 2d59990

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/IntelliTect.Coalesce.Analyzer.Tests/Coalesce1001_SimplifyItemResultTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,23 @@ public Task GetResult()
665665
""");
666666
}
667667

668+
[Fact]
669+
public async Task ArgumentToObjectParameter_NoWarning()
670+
{
671+
await VerifyAnalyzerAsync("""
672+
public class TestClass
673+
{
674+
public object GetResult()
675+
{
676+
string error = "error";
677+
return BadRequest(new ItemResult(error));
678+
}
679+
680+
private object BadRequest(object obj) => obj;
681+
}
682+
""");
683+
}
684+
668685
[Fact]
669686
public async Task ItemResultGeneric_PropertyInitializer_ErrorMessageWithObjectNull_ReportsInfo()
670687
{

src/IntelliTect.Coalesce.Analyzer/Analyzers/Coalesce1001_SimplifyItemResult.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,29 @@ initializer.Parent is IVariableDeclaratorOperation varOp &&
291291
return false;
292292
}
293293

294-
return parent switch
294+
// Sometimes the object creation is wrapped in a conversion operation
295+
// (e.g., when passing ItemResult to a parameter of type object)
296+
if (parent is IConversionOperation conversion)
295297
{
296-
// Don't suggest when the ItemResult is used as an argument to another method
297-
// where implicit conversion might not work if the target method is generic
298-
IArgumentOperation => false,
298+
parent = conversion.Parent;
299+
}
299300

300-
// For all other cases, suggest simplification
301-
_ => true
302-
};
301+
if (parent is IArgumentOperation argOp)
302+
{
303+
if (argOp.Parameter?.OriginalDefinition.Type.TypeKind == TypeKind.TypeParameter)
304+
{
305+
// Don't suggest simplification for generic type parameters
306+
// E.g., Task.FromResult(new ItemResult(error))
307+
return false;
308+
}
309+
310+
// Only suggest simplification if the parameter is ItemResult
311+
if (argOp.Parameter?.Type is { } type && !IsItemResultType(type))
312+
{
313+
return false;
314+
}
315+
}
316+
317+
return true;
303318
}
304319
}

0 commit comments

Comments
 (0)