Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs (Rust)

Code before:
        .filter(|alias| {
            !is_import_required_by_isort(
                &checker.settings().isort.required_imports,
                stmt.into(),
                alias,
            )
        })
        .filter(|alias| alias.asname.is_none())
        // Check that the import isn't shadowing a non-builtin value.
        .filter(|alias| {
            // Always flag `*` imports.
            if &alias.name == "*" {
                return true;
            }
            let Some(binding_id) = semantic.lookup_symbol(alias.name.as_str()) else {
                return false;
            };
            let binding = semantic.binding(binding_id);
            let scope = &semantic.scopes[binding.scope];
            // If the import isn't shadowing anything, it's definitely unnecessary.
            let Some(shadowed_binding_id) = scope.shadowed_binding(binding_id) else {
                return true;
            };
            let shadowed_binding = semantic.binding(shadowed_binding_id);
            shadowed_binding.kind.is_builtin()
        })
        .filter(|alias| {
            matches!(
                (module, alias.name.as_str()),
                (
                    "builtins" | "six.moves.builtins",
                    "*" | "ascii"
                        | "bytes"
                        | "chr"
                        | "dict"
                        | "filter"
                        | "hex"
                        | "input"
                        | "int"
                        | "isinstance"
                        | "list"
                        | "map"
                        | "max"
                        | "min"
                        | "next"
                        | "object"
                        | "oct"
                        | "open"
                        | "pow"
                        | "range"
                        | "round"

Review comment (refactor):
Can we move this after filtering by name? I suspect filtering by namie is faster than all the symbol lookup that we do here. It IMO, also makes it less confusing whyu we always return `true `for `*`.

Code after:
        .filter(|alias| {
            !is_import_required_by_isort(
                &checker.settings().isort.required_imports,
                stmt.into(),
                alias,
            )
        })
        .filter(|alias| alias.asname.is_none())
        .filter(|alias| {
            matches!(
                (module, alias.name.as_str()),
                (
                    "builtins" | "six.moves.builtins",
                    "*" | "ascii"
                        | "bytes"
                        | "chr"
                        | "dict"
                        | "filter"
                        | "hex"
                        | "input"
                        | "int"
                        | "isinstance"
                        | "list"
                        | "map"
                        | "max"
                        | "min"
                        | "next"
                        | "object"
                        | "oct"
                        | "open"
                        | "pow"
                        | "range"
                        | "round"
                        | "str"
                        | "super"
                        | "zip"
                ) | ("io", "open")
                    | ("six", "callable" | "next")
                    | ("six.moves", "filter" | "input" | "map" | "range" | "zip")
            )
        })
        // Check that the import isn't shadowing a non-builtin value.
        .filter(|alias| {
            // Always flag `*` imports.
            if &alias.name == "*" {
                return true;
            }
            let Some(binding_id) = semantic.lookup_symbol(alias.name.as_str()) else {
                return false;
            };
            let binding = semantic.binding(binding_id);