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

Code before:

    // Determine whether it's assigned to `abc.ABCMeta`.
    if !checker
        .semantic()
        .resolve_qualified_name(&keyword.value)
        .is_some_and(|qualified_name| matches!(qualified_name.segments(), ["abc", "ABCMeta"]))
    {
        return;
    }

    let applicability = if class_def.bases().is_empty() {
        Applicability::Safe
    } else {
        Applicability::Unsafe
    };
    let mut diagnostic = checker.report_diagnostic(MetaClassABCMeta, keyword.range);

    diagnostic.try_set_fix(|| {
        let (import_edit, binding) = checker.importer().get_or_import_symbol(
            &ImportRequest::import("abc", "ABC"),
            keyword.range.start(),
            checker.semantic(),
        )?;

        // Check the `abc.ABC` is in base classes.
        let is_has_abc = class_def.bases().iter().any(|base| {
            matches!(base, Expr::Name(_) | Expr::Attribute(_))
                && checker
                    .semantic()
                    .resolve_qualified_name(base)
                    .is_some_and(
                        |qualified_name| matches!(qualified_name.segments(), ["abc", "ABC"])
                    )
        });

        Ok(if position > 0 {
            // When the `abc.ABCMeta` is not the first keyword, put `abc.ABC` before the first
            // keyword.
            let rest = if is_has_abc {
                vec![]
            } else {
                vec![
                    Edit::insertion(format!("{binding}, "), class_def.keywords()[0].start()),
                    import_edit,
                ]
            };
            Fix::applicable_edits(
                // Delete from the previous argument, to the end of the `metaclass` argument.
                Edit::range_deletion(TextRange::new(
                    class_def.keywords()[position - 1].end(),
                    keyword.end(),

Review comment (refactor):
We have an [`any_qualified_base_class`](https://github.com/astral-sh/ruff/blob/b2b9d91859f2abc017e35d22f01696f141f6f5b1/crates/ruff_python_semantic/src/analyze/class.rs#L16) helper function that I think we could use here.

`is_has_abc` also reads a bit strangely to me, maybe just `has_abc` would be better.

Code after:
    // Identify the `metaclass` keyword.
    let Some((position, keyword)) = class_def.keywords().iter().find_position(|&keyword| {
        keyword
            .arg
            .as_ref()
            .is_some_and(|arg| arg.as_str() == "metaclass")
    }) else {
        return;
    };

    // Determine whether it's assigned to `abc.ABCMeta`.
    if !checker
        .semantic()
        .resolve_qualified_name(&keyword.value)
        .is_some_and(|qualified_name| matches!(qualified_name.segments(), ["abc", "ABCMeta"]))
    {
        return;
    }

    let applicability = if !class_def.bases().is_empty() {
        // The class has base arguments (not just a `metaclass` keyword).
        // Applying the fix may change semantics, so it is considered unsafe.
        Applicability::Unsafe
    } else if checker.comment_ranges().intersects(arguments.range()) {
        // The `metaclass` keyword overlaps with a comment.
        // Applying the fix would remove or alter the comment, so it is unsafe.
        Applicability::Unsafe
    } else {
        // An empty class definition with no overlapping comments.
        // Applying the fix is considered safe.
        Applicability::Safe
    };
    let mut diagnostic = checker.report_diagnostic(MetaClassABCMeta, keyword.range);

    diagnostic.try_set_fix(|| {
        let (import_edit, binding) = checker.importer().get_or_import_symbol(
            &ImportRequest::import("abc", "ABC"),
            keyword.range.start(),
            checker.semantic(),
        )?;

        // Check the `abc.ABC` is in base classes.
        let has_abc = analyze::class::any_qualified_base_class(
            class_def,
            checker.semantic(),
            &|qualified_name| matches!(qualified_name.segments(), ["abc", "ABC"]),
        );

        let delete_metaclass_keyword = remove_argument(
            keyword,
            arguments,