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

Code before:
{
    let mut results = Vec::new();
    let mut errors = Vec::new();

    // Use `positive_elements_or_object` to ensure we always have at least one element.
    // An intersection with only negative elements (e.g., `~int & ~str`) is implicitly
    // `object & ~int & ~str`, so we fall back to `object`.
    for element in intersection.positive_elements_or_object(db) {
        match map_fn(element) {
            Ok(result) => results.push(result),
            Err(error) => errors.push(error),
        }
    }

    // If any element succeeded, return the intersection of successful results.
    if !results.is_empty() {
        let mut builder = IntersectionBuilder::new(db);
        for result in results {
            builder = builder.add_positive(result);
        }
        return Ok(builder.build());
    }

    // All elements failed. Check if any element has the method available
    // (even if the call failed). If so, filter out `NotSubscriptable` errors
    // for elements that lack the method.
    let any_has_method = errors.iter().any(SubscriptError::any_method_available);

    if any_has_method {
        let mut builder = IntersectionBuilder::new(db);
        let mut filtered_errors = Vec::new();

        for error in errors {
            if error.any_method_available() {
                builder = builder.add_positive(error.result_type());
                filtered_errors.extend(
                    error
                        .into_errors()
                        .into_iter()
                        .filter(SubscriptErrorKind::method_available),
                );
            }
        }

        return Err(SubscriptError::with_errors(
            builder.build(),
            filtered_errors,
        ));
    }

    // No element has the method. Combine all errors.

Review comment (suggestion):
In my intersection-calls PR, I have a similar situation with 1) "not callable" vs 2) "calling top callable" vs 3) "specific argument error", where I want to show only (3) if there are any, or only (2) if there are any. I use a concept of "error priority" there, where I calculate the max error priority and filter down to only errors with that priority.

But I think that makes more sense when there are >2 priority levels; with just two, I think it makes sense to do it a bit less generically, as you do here.

Code after:
{
    let mut results = Vec::new();
    let mut errors = Vec::new();

    // Use `positive_elements_or_object` to ensure we always have at least one element.
    // An intersection with only negative elements (e.g., `~int & ~str`) is implicitly
    // `object & ~int & ~str`, so we fall back to `object`.
    for element in intersection.positive_elements_or_object(db) {
        match map_fn(element) {
            Ok(result) => results.push(result),
            Err(error) => errors.push(error),
        }
    }

    // If any element succeeded, return the intersection of successful results.
    if !results.is_empty() {
        let mut builder = IntersectionBuilder::new(db);
        for result in results {
            builder = builder.add_positive(result);
        }
        return Ok(builder.build());
    }

    // All elements failed. Check if any element has the method available
    // (even if the call failed). If so, filter out `NotSubscriptable` errors
    // for elements that lack the method.
    let any_has_method = errors.iter().any(SubscriptError::any_method_available);

    let mut builder = IntersectionBuilder::new(db);
    let mut collected_errors = Vec::new();

    for error in errors {
        if !any_has_method || error.any_method_available() {
            builder = builder.add_positive(error.result_type());
            let error_iter = error.into_errors().into_iter();
            if any_has_method {
                collected_errors.extend(error_iter.filter(SubscriptErrorKind::method_available));
            } else {
                collected_errors.extend(error_iter);
            }
        }
    }

    Err(SubscriptError::with_errors(
        builder.build(),
        collected_errors,
    ))
}

impl<'db> Type<'db> {
    pub(super) fn subscript(