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

Code before:
/// Returns true if the annotation is `dict[tuple[T1, T2], ...]` where tuple has exactly 2 elements.
fn is_annotation_dict_with_tuple_keys(annotation: &Expr, semantic: &SemanticModel) -> bool {
    // Handle stringized annotations
    let annotation = if let Expr::StringLiteral(_) = annotation {
        // For stringized annotations, we'd need to parse them, but for now,
        // we'll be conservative and return false (allow the fix)
        return false;
    } else {
        annotation
    };

    // Check if it's a subscript: dict[...]
    let Expr::Subscript(subscript) = annotation else {
        return false;
    };

    // Check if the value is `dict`
    let value_name = match subscript.value.as_ref() {
        Expr::Name(name) => name.id.as_str(),
        _ => return false,
    };

    // Check if it's dict or typing.Dict
    if value_name != "dict" && !semantic.match_typing_expr(subscript.value.as_ref(), "Dict") {
        return false;
    }

    // Extract the slice (should be a tuple: (key_type, value_type))
    let Expr::Tuple(tuple) = subscript.slice.as_ref() else {
        return false;
    };

    // dict[K, V] format - check if K is tuple with 2 elements
    if let Some(key_type) = tuple.elts.first() {
        return is_tuple_type_with_two_elements(key_type, semantic);
    }

    false
}

/// Returns true if the expression represents a tuple type with exactly 2 elements.
fn is_tuple_type_with_two_elements(expr: &Expr, semantic: &SemanticModel) -> bool {
    // Handle tuple[...] subscript
    if let Expr::Subscript(subscript) = expr {
        let value_name = match subscript.value.as_ref() {
            Expr::Name(name) => name.id.as_str(),
            _ => return false,
        };

        // Check if it's tuple or typing.Tuple
        if value_name == "tuple" || semantic.match_typing_expr(subscript.value.as_ref(), "Tuple") {

Review comment (suggestion):
I think we should check that `dict` has its builtin binding with `match_builtin_expr`.

Code after:
        && !semantic.match_typing_expr(subscript.value.as_ref(), "Dict")
    {
        return false;
    }

    // Extract the slice (should be a tuple: (key_type, value_type))
    let Expr::Tuple(tuple) = subscript.slice.as_ref() else {
        return false;
    };

    // dict[K, V] format - check if K is tuple with 2 elements
    if let [key, _value] = tuple.elts.as_slice() {
        return is_tuple_type_with_two_elements(key, semantic);
    }

    false
}

/// Returns true if the expression represents a tuple type with exactly 2 elements.
fn is_tuple_type_with_two_elements(expr: &Expr, semantic: &SemanticModel) -> bool {
    // Handle tuple[...] subscript
    if let Expr::Subscript(subscript) = expr {
        // Check if it's tuple or typing.Tuple
        if semantic.match_builtin_expr(subscript.value.as_ref(), "tuple")
            || semantic.match_typing_expr(subscript.value.as_ref(), "Tuple")
        {
            // Check the slice - tuple[T1, T2]
            if let Expr::Tuple(tuple_slice) = subscript.slice.as_ref() {
                return tuple_slice.elts.len() == 2;
            }
            return false;
        }
    }

    false
}