Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/mro.rs (Rust)

Code before:
            not_head.then_some(candidate)
        })?;

        mro.push(mro_entry);

        // Make sure we don't try to add the candidate to the MRO twice:
        for sequence in &mut sequences {
            if sequence[0] == mro_entry {
                sequence.pop_front();
            }
        }
    }
}

fn check_generic_reorder_fixes_mro<'db>(
    db: &'db dyn Db,
    resolved_bases: &[ClassBase<'db>],
    original_bases: &[Type<'db>],
) -> Option<usize> {
    let indices: Vec<usize> = original_bases
        .iter()
        .enumerate()
        .filter_map(|(i, base)| {
            matches!(
                base,
                Type::KnownInstance(KnownInstanceType::SubscriptedGeneric(_))
            )
            .then_some(i)
        })
        .collect();
    if indices.len() != 1 {
        return None;
    }
    let resolved_index = resolved_bases
        .iter()
        .position(|base| matches!(base, ClassBase::Generic))?;
    if resolved_index == resolved_bases.len() - 1 {
        return None;
    }
    let mut reordered = resolved_bases.to_vec();
    let generic = reordered.remove(resolved_index);
    reordered.push(generic);
    let mut seqs: Vec<VecDeque<ClassBase<'db>>> = Vec::with_capacity(reordered.len() + 1);
    for base in &reordered {
        if base.has_cyclic_mro(db) {
            return None;
        }
        seqs.push(base.mro(db, None).collect());
    }
    seqs.push(reordered.iter().copied().collect());
    c3_merge(seqs)?;

Review comment (suggestion):
```suggestion
/// Determine if an `inconsistent-mro` error could be resolved by moving
/// a `Generic[]` base to the end of the bases list.
///
/// If so, this function will return `Some(i)`, where `i` is the index of
/// the `Generic[]` base. If not, this function will return `None`.
fn check_generic_reorder_fixes_mro<'db>(
```

Code after:
            let candidate = outer_sequence[0];

            let not_head = sequences
                .iter()
                .all(|sequence| sequence.iter().skip(1).all(|base| base != &candidate));

            not_head.then_some(candidate)
        })?;

        mro.push(mro_entry);

        // Make sure we don't try to add the candidate to the MRO twice:
        for sequence in &mut sequences {
            if sequence[0] == mro_entry {
                sequence.pop_front();
            }
        }
    }
}

/// Determine if an `inconsistent-mro` error could be resolved by moving
/// a `Generic[]` base to the end of the bases list.
///
/// If so, this function will return `Some(i)`, where `i` is the index of
/// the `Generic[]` base. If not, this function will return `None`.
fn check_generic_reorder_fixes_mro<'db>(
    db: &'db dyn Db,
    resolved_bases: &[ClassBase<'db>],
    original_bases: &[Type<'db>],
) -> Option<usize> {
    // Only attempt an autofix if `Generic[]` appears exactly once in the original bases list.
    let single_index = original_bases
        .iter()
        .enumerate()
        .filter_map(|(i, base)| {
            matches!(
                base,
                Type::KnownInstance(KnownInstanceType::SubscriptedGeneric(_))
            )
            .then_some(i)
        })
        .exactly_one()
        .ok()?;

    // This should always be true if the original bases list contains exactly one
    // subscripted `Generic`, but return `None` here just to be safe.
    if resolved_bases.get(single_index) != Some(&ClassBase::Generic) {
        return None;
    }

    let mut reordered: VecDeque<ClassBase<'db>> = resolved_bases.iter().copied().collect();