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

Code before:
            return is_annotation_dict_with_tuple_keys(annotation, semantic);
        }
        // Empty dict without annotation should allow the fix
        return false;
    }

    // For non-empty dicts, check if all keys are 2-tuples
    dict_expr
        .iter_keys()
        .all(|key| matches!(key, Some(Expr::Tuple(tuple)) if tuple.len() == 2))
}

/// 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
}

Review comment (suggestion):
It might make sense to do something like:

```suggestion
    // 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);
    }
```

just to make sure there's the expected number of elements.

Code after:
        .all(|key| matches!(key, Some(Expr::Tuple(tuple)) if tuple.len() == 2))
}

/// 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 {
    // Check if it's a subscript: dict[...]
    let Expr::Subscript(subscript) = annotation else {
        return false;
    };

    // Check if it's dict or typing.Dict
    if !semantic.match_builtin_expr(subscript.value.as_ref(), "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 [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
}