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

Code before:
        }) = expr
        {
            let Expr::Name(name) = value.as_ref() else {
                return;
            };

            // Check that the sliced dictionary name is the same as the iterated object name.
            if name.id != self.dict_name.id {
                return;
            }

            // Check that the sliced value is the same as the target of the `for` loop.
            if ComparableExpr::from(slice) != ComparableExpr::from(self.target) {
                return;
            }

            if self.guard.is_none() {
                self.guard = Some(
                    self.checker
                        .report_diagnostic(DictIndexMissingItems, self.range),
                );
            }

            if is_plc0206_narrower_range_enabled(self.checker.settings()) {
                self.guard.as_mut().unwrap().secondary_annotation("", expr);
            }
        } else {
            visitor::walk_expr(self, expr);
        }
    }
}

/// Extracts the name of the dictionary from the expression.
fn extract_dict_name(expr: &Expr) -> Option<&ast::ExprName> {
    // Ex) `for key in obj:`
    if let Some(name_expr) = expr.as_name_expr() {
        return Some(name_expr);
    }

    // Ex) `for key in obj.keys():`
    if let Expr::Call(ast::ExprCall { func, .. }) = expr {
        if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func.as_ref() {
            if attr == "keys" {
                if let Expr::Name(var_name) = value.as_ref() {
                    return Some(var_name);
                }
            }
        }
    }

    // Ex) `for key in (my_dict := {"foo": "bar"}):`

Review comment (suggestion):
```suggestion
						let guard = self.guard.get_or_init(|| self.checker
                        .report_diagnostic(DictIndexMissingItems, self.range)
          	};

            if is_plc0206_narrower_range_enabled(self.checker.settings()) {
          	    guard.secondary_annotation("", expr);
            }
```

Code after:
            ..
        }) = expr
        {
            let Expr::Name(name) = value.as_ref() else {
                return;
            };

            // Check that the sliced dictionary name is the same as the iterated object name.
            if name.id != self.dict_name.id {
                return;
            }

            // Check that the sliced value is the same as the target of the `for` loop.
            if ComparableExpr::from(slice) != ComparableExpr::from(self.target) {
                return;
            }

            let guard = self.guard.get_or_insert_with(|| {
                self.checker.report_diagnostic(
                    DictIndexMissingItems {
                        key: self.checker.locator().slice(self.target),
                        dict: self.checker.locator().slice(self.dict_name),
                    },
                    self.range,
                )
            });

            guard.secondary_annotation("", expr);
        } else {
            visitor::walk_expr(self, expr);
        }
    }
}

/// Extracts the name of the dictionary from the expression.
fn extract_dict_name(expr: &Expr) -> Option<&ast::ExprName> {
    // Ex) `for key in obj:`
    if let Some(name_expr) = expr.as_name_expr() {
        return Some(name_expr);
    }

    // Ex) `for key in obj.keys():`
    if let Expr::Call(ast::ExprCall { func, .. }) = expr {
        if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func.as_ref() {
            if attr == "keys" {
                if let Expr::Name(var_name) = value.as_ref() {
                    return Some(var_name);
                }
            }
        }
    }