Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs (C#)

Code before:
                            result.Add(new PSTypeName(method.ReturnType));
                        }

                        continue;
                    }

                    ParameterInfo[] methodParams = method.GetParameters();
                    if (methodParams.Length != 2)
                    {
                        continue;
                    }

                    bool addType = false;
                    foreach (PSTypeName rType in rhsTypes)
                    {
                        if (rType.Type is not null && rType.Type.IsAssignableTo(methodParams[1].ParameterType))
                        {
                            addType = true;
                            break;
                        }
                    }

                    if (addType && addedReturnTypes.Add(method.ReturnType.FullName))
                    {
                        result.Add(new PSTypeName(method.ReturnType));
                    }
                }
            }

            if (result.Count == 0)
            {
                result.AddRange(lhsTypes);
            }

            return result;
        }

        object ICustomAstVisitor.VisitUnaryExpression(UnaryExpressionAst unaryExpressionAst)
        {
            var tokenKind = unaryExpressionAst.TokenKind;
            return (tokenKind == TokenKind.Not || tokenKind == TokenKind.Exclaim)
                   ? BinaryExpressionAst.BoolTypeNameArray
                   : unaryExpressionAst.Child.Accept(this);
        }

        object ICustomAstVisitor.VisitConvertExpression(ConvertExpressionAst convertExpressionAst)
        {
            // The reflection type of PSCustomObject is PSObject, so this covers both the
            // [PSObject] @{ Key = "Value" } and the [PSCustomObject] @{ Key = "Value" } case.
            var type = convertExpressionAst.Type.TypeName.GetReflectionType();


Review comment (refactor):
We can move this in previous loop and remove `addType`.

Code after:
                            result.Add(new PSTypeName(method.ReturnType));
                        }

                        continue;
                    }

                    ParameterInfo[] methodParams = method.GetParameters();
                    if (methodParams.Length != 2)
                    {
                        continue;
                    }

                    foreach (PSTypeName rType in rhsTypes)
                    {
                        if (rType.Type is not null && rType.Type.IsAssignableTo(methodParams[1].ParameterType))
                        {
                            if (addedReturnTypes.Add(method.ReturnType.FullName))
                            {
                                result.Add(new PSTypeName(method.ReturnType));
                            }

                            break;
                        }
                    }
                }
            }

            if (result.Count == 0)
            {
                result.AddRange(lhsTypes);
            }

            return result;
        }

        object ICustomAstVisitor.VisitUnaryExpression(UnaryExpressionAst unaryExpressionAst)
        {
            var tokenKind = unaryExpressionAst.TokenKind;
            return (tokenKind == TokenKind.Not || tokenKind == TokenKind.Exclaim)
                   ? BinaryExpressionAst.BoolTypeNameArray
                   : unaryExpressionAst.Child.Accept(this);
        }

        object ICustomAstVisitor.VisitConvertExpression(ConvertExpressionAst convertExpressionAst)
        {
            // The reflection type of PSCustomObject is PSObject, so this covers both the
            // [PSObject] @{ Key = "Value" } and the [PSCustomObject] @{ Key = "Value" } case.
            var type = convertExpressionAst.Type.TypeName.GetReflectionType();

            if (type is null && convertExpressionAst.Type.TypeName is TypeName unavailableType && unavailableType._typeDefinitionAst is not null)
            {