|
| 1 | +use ast::{ExprAttribute, ExprName, Identifier}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast::{self as ast, Arguments, Expr, ExprCall}; |
| 4 | +use ruff_text_size::Ranged; |
| 5 | + |
| 6 | +use crate::{checkers::ast::Checker, fix::snippet::SourceCodeSnippet}; |
| 7 | +use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; |
| 8 | +use ruff_python_semantic::analyze::typing::is_dict; |
| 9 | + |
| 10 | +/// ## What it does |
| 11 | +/// Checks for use of `zip()` to iterate over keys and values of a dictionary at once. |
| 12 | +/// |
| 13 | +/// ## Why is this bad? |
| 14 | +/// The `dict` type provides an `.items()` method which is faster and more readable. |
| 15 | +/// |
| 16 | +/// ## Example |
| 17 | +/// ```python |
| 18 | +/// flag_stars = {"USA": 50, "Slovenia": 3, "Panama": 2, "Australia": 6} |
| 19 | +/// |
| 20 | +/// for country, stars in zip(flag_stars.keys(), flag_stars.values()): |
| 21 | +/// print(f"{country}'s flag has {stars} stars.") |
| 22 | +/// ``` |
| 23 | +/// |
| 24 | +/// Use instead: |
| 25 | +/// ```python |
| 26 | +/// flag_stars = {"USA": 50, "Slovenia": 3, "Panama": 2, "Australia": 6} |
| 27 | +/// |
| 28 | +/// for country, stars in flag_stars.items(): |
| 29 | +/// print(f"{country}'s flag has {stars} stars.") |
| 30 | +/// ``` |
| 31 | +/// |
| 32 | +/// ## References |
| 33 | +/// - [Python documentation: `dict.items`](https://docs.python.org/3/library/stdtypes.html#dict.items) |
| 34 | +#[violation] |
| 35 | +pub struct ZipDictKeysAndValues { |
| 36 | + expected: SourceCodeSnippet, |
| 37 | + actual: SourceCodeSnippet, |
| 38 | +} |
| 39 | + |
| 40 | +impl AlwaysFixableViolation for ZipDictKeysAndValues { |
| 41 | + #[derive_message_formats] |
| 42 | + fn message(&self) -> String { |
| 43 | + let ZipDictKeysAndValues { expected, actual } = self; |
| 44 | + if let (Some(expected), Some(actual)) = (expected.full_display(), actual.full_display()) { |
| 45 | + format!("Use `{expected}` instead of `{actual}`") |
| 46 | + } else { |
| 47 | + format!("Use `dict.items()` instead of `zip(dict.keys(), dict.values())`") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn fix_title(&self) -> String { |
| 52 | + let ZipDictKeysAndValues { expected, actual } = self; |
| 53 | + if let (Some(expected), Some(actual)) = (expected.full_display(), actual.full_display()) { |
| 54 | + format!("Replace `{actual}` with `{expected}`") |
| 55 | + } else { |
| 56 | + "Replace `zip(dict.keys(), dict.values())` with `dict.items()`".to_string() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// SIM911 |
| 62 | +pub(crate) fn zip_dict_keys_and_values(checker: &mut Checker, expr: &ExprCall) { |
| 63 | + let ExprCall { |
| 64 | + func, |
| 65 | + arguments: Arguments { args, keywords, .. }, |
| 66 | + .. |
| 67 | + } = expr; |
| 68 | + match &keywords[..] { |
| 69 | + [] => {} |
| 70 | + [ast::Keyword { |
| 71 | + arg: Some(name), .. |
| 72 | + }] if name.as_str() == "strict" => {} |
| 73 | + _ => return, |
| 74 | + }; |
| 75 | + if matches!(func.as_ref(), Expr::Name(ExprName { id, .. }) if id != "zip") { |
| 76 | + return; |
| 77 | + } |
| 78 | + let [arg1, arg2] = &args[..] else { |
| 79 | + return; |
| 80 | + }; |
| 81 | + let Some((var1, attr1)) = get_var_attr(arg1) else { |
| 82 | + return; |
| 83 | + }; |
| 84 | + let Some((var2, attr2)) = get_var_attr(arg2) else { |
| 85 | + return; |
| 86 | + }; |
| 87 | + if var1.id != var2.id || attr1 != "keys" || attr2 != "values" { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + let Some(binding) = checker |
| 92 | + .semantic() |
| 93 | + .only_binding(var1) |
| 94 | + .map(|id| checker.semantic().binding(id)) |
| 95 | + else { |
| 96 | + return; |
| 97 | + }; |
| 98 | + if !is_dict(binding, checker.semantic()) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + let expected = format!("{}.items()", checker.locator().slice(var1)); |
| 103 | + let actual = checker.locator().slice(expr); |
| 104 | + |
| 105 | + let mut diagnostic = Diagnostic::new( |
| 106 | + ZipDictKeysAndValues { |
| 107 | + expected: SourceCodeSnippet::new(expected.clone()), |
| 108 | + actual: SourceCodeSnippet::from_str(actual), |
| 109 | + }, |
| 110 | + expr.range(), |
| 111 | + ); |
| 112 | + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( |
| 113 | + expected, |
| 114 | + expr.range(), |
| 115 | + ))); |
| 116 | + checker.diagnostics.push(diagnostic); |
| 117 | +} |
| 118 | + |
| 119 | +fn get_var_attr(expr: &Expr) -> Option<(&ExprName, &Identifier)> { |
| 120 | + let Expr::Call(ast::ExprCall { func, .. }) = expr else { |
| 121 | + return None; |
| 122 | + }; |
| 123 | + let Expr::Attribute(ExprAttribute { value, attr, .. }) = func.as_ref() else { |
| 124 | + return None; |
| 125 | + }; |
| 126 | + let Expr::Name(var_name) = value.as_ref() else { |
| 127 | + return None; |
| 128 | + }; |
| 129 | + Some((var_name, attr)) |
| 130 | +} |
0 commit comments