Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs (C#)

Code before:
            bool syntaxIsOk = CheckSyntaxForNameofArgument(argument, out string name, boundArgument.HasAnyErrors ? BindingDiagnosticBag.Discarded : diagnostics);
            if (boundArgument is BoundMethodGroup methodGroup)
            {
                if (!boundArgument.HasAnyErrors && syntaxIsOk)
                {
                    if (!methodGroup.TypeArgumentsOpt.IsDefaultOrEmpty)
                    {
                        // method group with type parameters not allowed
                        diagnostics.Add(ErrorCode.ERR_NameofMethodGroupWithTypeParameters, argument.Location);
                    }
                    else
                    {
                        EnsureNameofExpressionSymbols(methodGroup, diagnostics);
                    }
                }

                bool useType = !Compilation.IsFeatureEnabled(MessageID.IDS_FeatureInstanceMemberInNameof);
                boundArgument = methodGroup.Update(
                    methodGroup.TypeArgumentsOpt,
                    methodGroup.Name,
                    methodGroup.Methods,
                    methodGroup.LookupSymbolOpt,
                    methodGroup.LookupError,
                    methodGroup.Flags,
                    methodGroup.FunctionType,
                    receiverOpt: ReplaceTypeOrValueReceiver(methodGroup.ReceiverOpt, useType: useType, BindingDiagnosticBag.Discarded), //only change
                    methodGroup.ResultKind);
            }
            else if (boundArgument is BoundPropertyAccess propertyAccess)
            {
                if (propertyAccess.PropertySymbol.IsExtensionBlockMember())
                {
                    diagnostics.Add(ErrorCode.ERR_NameofExtensionMember, boundArgument.Syntax);
                }
            }
            else if (boundArgument is BoundNamespaceExpression nsExpr)
            {
                diagnostics.AddAssembliesUsedByNamespaceReference(nsExpr.NamespaceSymbol);
            }
            else if (boundArgument is BoundIndexerAccess indexerAccess)
            {
                // nameof(x[y]) where the argument is an indexer is an error. Ref analysis requires
                // the indexer to be recognized as get, set, or both. We don't know which was
                // intended for nameof() but since this is already an error, assume it's get.
                Debug.Assert(!syntaxIsOk);
                Debug.Assert(indexerAccess.AccessorKind == AccessorKind.Unknown);
                if (indexerAccess.AccessorKind == AccessorKind.Unknown)
                {
                    boundArgument = CheckValue(boundArgument, BindValueKind.RValue, diagnostics);
                }
            }

Review comment (refactor):
>BindingDiagnosticBag.Discarded [](http://example.com/codeflow?start=103&length=30)

It looks like always discarding diagnostics here is fragile. We should try switching to ```boundArgument.HasAnyErrors ? BindingDiagnosticBag.Discarded : diagnostics```. Also, please check other call sites of this helper that unconditionally discard diagnostics when we are not in an obvious error recovery mode. We might want to take a close look at those and quite possibly adjust as well. Also, please review other changes in #80978 that unconditionally discard diagnostics.  #Closed

Code after:
            bool syntaxIsOk = CheckSyntaxForNameofArgument(argument, out string name, boundArgument.HasAnyErrors ? BindingDiagnosticBag.Discarded : diagnostics);
            if (boundArgument is BoundMethodGroup methodGroup)
            {
                if (!boundArgument.HasAnyErrors && syntaxIsOk)
                {
                    if (!methodGroup.TypeArgumentsOpt.IsDefaultOrEmpty)
                    {
                        // method group with type parameters not allowed
                        diagnostics.Add(ErrorCode.ERR_NameofMethodGroupWithTypeParameters, argument.Location);
                    }
                    else
                    {
                        EnsureNameofExpressionSymbols(methodGroup, diagnostics);
                    }
                }

                boundArgument = methodGroup.Update(
                    methodGroup.TypeArgumentsOpt,
                    methodGroup.Name,
                    methodGroup.Methods,
                    methodGroup.LookupSymbolOpt,
                    methodGroup.LookupError,
                    methodGroup.Flags,
                    methodGroup.FunctionType,
                    receiverOpt: ReplaceTypeOrValueReceiver(methodGroup.ReceiverOpt, useType: true, boundArgument.HasAnyErrors ? BindingDiagnosticBag.Discarded : diagnostics), //only change
                    methodGroup.ResultKind);
            }
            else if (boundArgument is BoundPropertyAccess propertyAccess)
            {
                if (propertyAccess.PropertySymbol.IsExtensionBlockMember())
                {
                    diagnostics.Add(ErrorCode.ERR_NameofExtensionMember, boundArgument.Syntax);
                }
            }
            else if (boundArgument is BoundNamespaceExpression nsExpr)
            {
                diagnostics.AddAssembliesUsedByNamespaceReference(nsExpr.NamespaceSymbol);
            }
            else if (boundArgument is BoundIndexerAccess indexerAccess)
            {
                // nameof(x[y]) where the argument is an indexer is an error. Ref analysis requires
                // the indexer to be recognized as get, set, or both. We don't know which was
                // intended for nameof() but since this is already an error, assume it's get.
                Debug.Assert(!syntaxIsOk);
                Debug.Assert(indexerAccess.AccessorKind == AccessorKind.Unknown);
                if (indexerAccess.AccessorKind == AccessorKind.Unknown)
                {
                    boundArgument = CheckValue(boundArgument, BindValueKind.RValue, diagnostics);
                }
            }