File tree Expand file tree Collapse file tree 2 files changed +39
-7
lines changed
IntelliTect.Coalesce.Analyzer.Tests
IntelliTect.Coalesce.Analyzer/Analyzers Expand file tree Collapse file tree 2 files changed +39
-7
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments