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

Code before:
                            var count = methods.Count;
                            ((NamedTypeSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                            // If we found any extension methods, then consider this using as used.
                            if (methods.Count != count)
                            {
                                MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                                seenStaticClassWithExtensionMethods = true;
                            }

                            break;
                        }
                }
            }

            if (seenNamespaceWithExtensionMethods && seenStaticClassWithExtensionMethods)
            {
                methods.RemoveDuplicates();
            }
        }

        internal override void GetExtensionDeclarations(ArrayBuilder<NamedTypeSymbol> extensions, Binder originalBinder)
        {
            Debug.Assert(extensions.Count == 0);

            bool callerIsSemanticModel = originalBinder.IsSemanticModelBinder;

            // We need to avoid collecting multiple candidates for an extension declaration imported both through a namespace and a static class
            // We will look for duplicates only if both of the following flags are set to true
            bool seenNamespaceWithExtensions = false;
            bool seenStaticClassWithExtensions = false;

            foreach (var nsOrType in this.GetUsings(basesBeingResolved: null))
            {
                if (nsOrType.NamespaceOrType is NamespaceSymbol ns)
                {
                    var count = extensions.Count;
                    ns.GetExtensionContainers(extensions);
                    // If we found any extension declarations, then consider this using as used.
                    // Tracked by https://github.com/dotnet/roslyn/issues/79440 : using directives, consider refining this logic
                    if (extensions.Count != count)
                    {
                        MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                        seenNamespaceWithExtensions = true;
                    }
                }
                else if (nsOrType.NamespaceOrType is NamedTypeSymbol namedType)
                {
                    var count = extensions.Count;
                    namedType.GetExtensionContainers(extensions);
                    // If we found any extension declarations, then consider this using as used.

Review comment (suggestion):
:memo: Added `TestUnusedExtensionMarksImportsAsUsed_02`

Code after:
                            var count = methods.Count;
                            ((NamedTypeSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                            // If we found any extension methods, then consider this using as used.
                            if (methods.Count != count)
                            {
                                MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                                seenStaticClassWithExtensionMethods = true;
                            }

                            break;
                        }
                }
            }

            if (seenNamespaceWithExtensionMethods && seenStaticClassWithExtensionMethods)
            {
                methods.RemoveDuplicates();
            }
        }

        internal override void GetCandidateExtensionMembersInSingleBinder(ArrayBuilder<Symbol> members, string? name, string? alternativeName, int arity, LookupOptions options, Binder originalBinder)
        {
            Debug.Assert(members.Count == 0);

            bool callerIsSemanticModel = originalBinder.IsSemanticModelBinder;

            // We need to avoid collecting multiple candidates for an extension declaration imported both through a namespace and a static class
            // We will look for duplicates only if both of the following flags are set to true
            bool seenNamespaceWithExtensions = false;
            bool seenStaticClassWithExtensions = false;

            foreach (var nsOrType in this.GetUsings(basesBeingResolved: null))
            {
                if (nsOrType.NamespaceOrType is NamespaceSymbol ns)
                {
                    var count = members.Count;
                    ns.GetExtensionMembers(members, name, alternativeName, arity, options, originalBinder.FieldsBeingBound);
                    // If we found any extension declarations, then consider this using as used.
                    if (members.Count != count)
                    {
                        MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                        seenNamespaceWithExtensions = true;
                    }
                }
                else if (nsOrType.NamespaceOrType is NamedTypeSymbol namedType)
                {
                    var count = members.Count;
                    namedType.GetExtensionMembers(members, name, alternativeName, arity, options, originalBinder.FieldsBeingBound);
                    // If we found any extension declarations, then consider this using as used.
                    if (members.Count != count)