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

Code before:

        // We can also ignore dunder names.
        // Ex) `from __future__ import annotations`
        // Ex) `from foo import __version__`
        if root_module.starts_with("__") || import_info.member_name.starts_with("__") {
            continue;
        }

        // Ignore private imports from the same module.
        // Ex) `from foo import _bar` within `foo/baz.py`
        if checker
            .package()
            .map(PackageRoot::path)
            .is_some_and(|path| path.ends_with(root_module))
        {
            continue;
        }

        // Ignore public imports; require at least one private name.
        // Ex) `from foo import bar`
        let Some((index, private_name)) = import_info
            .qualified_name
            .segments()
            .iter()
            .find_position(|name| {
                name.starts_with('_') && !(name.starts_with("__") && name.ends_with("__"))
            })
        else {
            continue;
        };

        // Ignore private imports used exclusively for typing.
        if !binding.references.is_empty()
            && binding
                .references()
                .map(|reference_id| checker.semantic().reference(reference_id))
                .all(is_typing)
        {
            continue;
        }

        let name = (*private_name).to_string();
        let module = if index > 0 {
            Some(import_info.qualified_name.segments()[..index].join("."))
        } else {
            None
        };
        let range = match binding.source.map(|id| checker.semantic().statement(id)) {
            Some(ruff_python_ast::Stmt::ImportFrom(import_from)) => {
                if index < import_info.module_name.len() {
                    import_from

Review comment (refactor):
Not much code either way, but I think we should use the existing [`is_dunder`](https://github.com/astral-sh/ruff/blob/b912dfc8788f8f8c351767899df6b72ce3366352/crates/ruff_python_ast/src/helpers.rs#L618) helper here.

Code after:
        }

        // We can also ignore dunder names.
        // Ex) `from __future__ import annotations`
        // Ex) `from foo import __version__`
        if is_dunder(root_module) || is_dunder(&import_info.member_name) {
            continue;
        }

        // Ignore private imports from the same module.
        // Ex) `from foo import _bar` within `foo/baz.py`
        if checker
            .package()
            .map(PackageRoot::path)
            .is_some_and(|path| path.ends_with(root_module))
        {
            continue;
        }

        // Ignore public imports; require at least one private name.
        // Ex) `from foo import bar`
        let Some((index, private_name)) = import_info
            .qualified_name
            .segments()
            .iter()
            .find_position(|name| name.starts_with('_') && !is_dunder(name))
        else {
            continue;
        };

        // Ignore private imports used exclusively for typing.
        if !binding.references.is_empty()
            && binding
                .references()
                .map(|reference_id| checker.semantic().reference(reference_id))
                .all(is_typing)
        {
            continue;
        }

        let name = (*private_name).to_string();
        let module = if index > 0 {
            Some(import_info.qualified_name.segments()[..index].join("."))
        } else {
            None
        };
        let range = match binding.source.map(|id| checker.semantic().statement(id)) {
            Some(ast::Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. })) => {
                if index < import_info.module_name.len() {
                    module.as_ref().map_or(binding.range(), |module| {
                        SimpleTokenizer::starts_at(module.start(), checker.locator().contents())