|
| 1 | +use rustc_hash::FxHashMap; |
| 2 | + |
| 3 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 4 | +use ruff_macros::{derive_message_formats, violation}; |
| 5 | +use ruff_python_ast::helpers::NameFinder; |
| 6 | +use ruff_python_ast::visitor::Visitor; |
| 7 | +use ruff_python_ast::{self as ast, Expr}; |
| 8 | +use ruff_text_size::Ranged; |
| 9 | + |
| 10 | +use crate::checkers::ast::Checker; |
| 11 | +use crate::fix::snippet::SourceCodeSnippet; |
| 12 | + |
| 13 | +/// ## What it does |
| 14 | +/// Checks for dictionary comprehensions that use a static key, like a string |
| 15 | +/// literal or a variable defined outside the comprehension. |
| 16 | +/// |
| 17 | +/// ## Why is this bad? |
| 18 | +/// Using a static key (like a string literal) in a dictionary comprehension |
| 19 | +/// is usually a mistake, as it will result in a dictionary with only one key, |
| 20 | +/// despite the comprehension iterating over multiple values. |
| 21 | +/// |
| 22 | +/// ## Example |
| 23 | +/// ```python |
| 24 | +/// data = ["some", "Data"] |
| 25 | +/// {"key": value.upper() for value in data} |
| 26 | +/// ``` |
| 27 | +/// |
| 28 | +/// Use instead: |
| 29 | +/// ```python |
| 30 | +/// data = ["some", "Data"] |
| 31 | +/// {value: value.upper() for value in data} |
| 32 | +/// ``` |
| 33 | +#[violation] |
| 34 | +pub struct StaticKeyDictComprehension { |
| 35 | + key: SourceCodeSnippet, |
| 36 | +} |
| 37 | + |
| 38 | +impl Violation for StaticKeyDictComprehension { |
| 39 | + #[derive_message_formats] |
| 40 | + fn message(&self) -> String { |
| 41 | + let StaticKeyDictComprehension { key } = self; |
| 42 | + if let Some(key) = key.full_display() { |
| 43 | + format!("Dictionary comprehension uses static key: `{key}`") |
| 44 | + } else { |
| 45 | + format!("Dictionary comprehension uses static key") |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// RUF011 |
| 51 | +pub(crate) fn static_key_dict_comprehension(checker: &mut Checker, dict_comp: &ast::ExprDictComp) { |
| 52 | + // Collect the bound names in the comprehension's generators. |
| 53 | + let names = { |
| 54 | + let mut visitor = NameFinder::default(); |
| 55 | + for generator in &dict_comp.generators { |
| 56 | + visitor.visit_expr(&generator.target); |
| 57 | + } |
| 58 | + visitor.names |
| 59 | + }; |
| 60 | + |
| 61 | + if is_constant(&dict_comp.key, &names) { |
| 62 | + checker.diagnostics.push(Diagnostic::new( |
| 63 | + StaticKeyDictComprehension { |
| 64 | + key: SourceCodeSnippet::from_str(checker.locator().slice(dict_comp.key.as_ref())), |
| 65 | + }, |
| 66 | + dict_comp.key.range(), |
| 67 | + )); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Returns `true` if the given expression is a constant in the context of the dictionary |
| 72 | +/// comprehension. |
| 73 | +fn is_constant(key: &Expr, names: &FxHashMap<&str, &ast::ExprName>) -> bool { |
| 74 | + match key { |
| 75 | + Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(|elt| is_constant(elt, names)), |
| 76 | + Expr::Name(ast::ExprName { id, .. }) => !names.contains_key(id.as_str()), |
| 77 | + Expr::Attribute(ast::ExprAttribute { value, .. }) => is_constant(value, names), |
| 78 | + Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { |
| 79 | + is_constant(value, names) && is_constant(slice, names) |
| 80 | + } |
| 81 | + Expr::BinOp(ast::ExprBinOp { left, right, .. }) => { |
| 82 | + is_constant(left, names) && is_constant(right, names) |
| 83 | + } |
| 84 | + Expr::BoolOp(ast::ExprBoolOp { values, .. }) => { |
| 85 | + values.iter().all(|value| is_constant(value, names)) |
| 86 | + } |
| 87 | + Expr::UnaryOp(ast::ExprUnaryOp { operand, .. }) => is_constant(operand, names), |
| 88 | + expr if expr.is_literal_expr() => true, |
| 89 | + _ => false, |
| 90 | + } |
| 91 | +} |
0 commit comments