-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a3bb67
commit 9558c11
Showing
11 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
crates/ruff_linter/src/rules/flake8_bugbear/rules/static_key_dict_comprehension.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use rustc_hash::FxHashMap; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::NameFinder; | ||
use ruff_python_ast::visitor::Visitor; | ||
use ruff_python_ast::{self as ast, Expr}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::fix::snippet::SourceCodeSnippet; | ||
|
||
/// ## What it does | ||
/// Checks for dictionary comprehensions that use a static key, like a string | ||
/// literal or a variable defined outside the comprehension. | ||
/// | ||
/// ## Why is this bad? | ||
/// Using a static key (like a string literal) in a dictionary comprehension | ||
/// is usually a mistake, as it will result in a dictionary with only one key, | ||
/// despite the comprehension iterating over multiple values. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// data = ["some", "Data"] | ||
/// {"key": value.upper() for value in data} | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// data = ["some", "Data"] | ||
/// {value: value.upper() for value in data} | ||
/// ``` | ||
#[violation] | ||
pub struct StaticKeyDictComprehension { | ||
key: SourceCodeSnippet, | ||
} | ||
|
||
impl Violation for StaticKeyDictComprehension { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let StaticKeyDictComprehension { key } = self; | ||
if let Some(key) = key.full_display() { | ||
format!("Dictionary comprehension uses static key: `{key}`") | ||
} else { | ||
format!("Dictionary comprehension uses static key") | ||
} | ||
} | ||
} | ||
|
||
/// RUF011 | ||
pub(crate) fn static_key_dict_comprehension(checker: &mut Checker, dict_comp: &ast::ExprDictComp) { | ||
// Collect the bound names in the comprehension's generators. | ||
let names = { | ||
let mut visitor = NameFinder::default(); | ||
for generator in &dict_comp.generators { | ||
visitor.visit_expr(&generator.target); | ||
} | ||
visitor.names | ||
}; | ||
|
||
if is_constant(&dict_comp.key, &names) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
StaticKeyDictComprehension { | ||
key: SourceCodeSnippet::from_str(checker.locator().slice(dict_comp.key.as_ref())), | ||
}, | ||
dict_comp.key.range(), | ||
)); | ||
} | ||
} | ||
|
||
/// Returns `true` if the given expression is a constant in the context of the dictionary | ||
/// comprehension. | ||
fn is_constant(key: &Expr, names: &FxHashMap<&str, &ast::ExprName>) -> bool { | ||
match key { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(|elt| is_constant(elt, names)), | ||
Expr::Name(ast::ExprName { id, .. }) => !names.contains_key(id.as_str()), | ||
Expr::Attribute(ast::ExprAttribute { value, .. }) => is_constant(value, names), | ||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { | ||
is_constant(value, names) && is_constant(slice, names) | ||
} | ||
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => { | ||
is_constant(left, names) && is_constant(right, names) | ||
} | ||
Expr::BoolOp(ast::ExprBoolOp { values, .. }) => { | ||
values.iter().all(|value| is_constant(value, names)) | ||
} | ||
Expr::UnaryOp(ast::ExprUnaryOp { operand, .. }) => is_constant(operand, names), | ||
expr if expr.is_literal_expr() => true, | ||
_ => false, | ||
} | ||
} |
34 changes: 17 additions & 17 deletions
34
...rules__ruff__tests__RUF011_RUF011.py.snap → ..._flake8_bugbear__tests__B035_B035.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,80 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
RUF011.py:15:2: RUF011 Dictionary comprehension uses static key: `"key"` | ||
B035.py:15:2: B035 Dictionary comprehension uses static key: `"key"` | ||
| | ||
14 | # Errors | ||
15 | {"key": value.upper() for value in data} | ||
| ^^^^^ RUF011 | ||
| ^^^^^ B035 | ||
16 | {True: value.upper() for value in data} | ||
17 | {0: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:16:2: RUF011 Dictionary comprehension uses static key: `True` | ||
B035.py:16:2: B035 Dictionary comprehension uses static key: `True` | ||
| | ||
14 | # Errors | ||
15 | {"key": value.upper() for value in data} | ||
16 | {True: value.upper() for value in data} | ||
| ^^^^ RUF011 | ||
| ^^^^ B035 | ||
17 | {0: value.upper() for value in data} | ||
18 | {(1, "a"): value.upper() for value in data} # Constant tuple | ||
| | ||
|
||
RUF011.py:17:2: RUF011 Dictionary comprehension uses static key: `0` | ||
B035.py:17:2: B035 Dictionary comprehension uses static key: `0` | ||
| | ||
15 | {"key": value.upper() for value in data} | ||
16 | {True: value.upper() for value in data} | ||
17 | {0: value.upper() for value in data} | ||
| ^ RUF011 | ||
| ^ B035 | ||
18 | {(1, "a"): value.upper() for value in data} # Constant tuple | ||
19 | {constant: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:18:2: RUF011 Dictionary comprehension uses static key: `(1, "a")` | ||
B035.py:18:2: B035 Dictionary comprehension uses static key: `(1, "a")` | ||
| | ||
16 | {True: value.upper() for value in data} | ||
17 | {0: value.upper() for value in data} | ||
18 | {(1, "a"): value.upper() for value in data} # Constant tuple | ||
| ^^^^^^^^ RUF011 | ||
| ^^^^^^^^ B035 | ||
19 | {constant: value.upper() for value in data} | ||
20 | {constant + constant: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:19:2: RUF011 Dictionary comprehension uses static key: `constant` | ||
B035.py:19:2: B035 Dictionary comprehension uses static key: `constant` | ||
| | ||
17 | {0: value.upper() for value in data} | ||
18 | {(1, "a"): value.upper() for value in data} # Constant tuple | ||
19 | {constant: value.upper() for value in data} | ||
| ^^^^^^^^ RUF011 | ||
| ^^^^^^^^ B035 | ||
20 | {constant + constant: value.upper() for value in data} | ||
21 | {constant.attribute: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:20:2: RUF011 Dictionary comprehension uses static key: `constant + constant` | ||
B035.py:20:2: B035 Dictionary comprehension uses static key: `constant + constant` | ||
| | ||
18 | {(1, "a"): value.upper() for value in data} # Constant tuple | ||
19 | {constant: value.upper() for value in data} | ||
20 | {constant + constant: value.upper() for value in data} | ||
| ^^^^^^^^^^^^^^^^^^^ RUF011 | ||
| ^^^^^^^^^^^^^^^^^^^ B035 | ||
21 | {constant.attribute: value.upper() for value in data} | ||
22 | {constant[0]: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:21:2: RUF011 Dictionary comprehension uses static key: `constant.attribute` | ||
B035.py:21:2: B035 Dictionary comprehension uses static key: `constant.attribute` | ||
| | ||
19 | {constant: value.upper() for value in data} | ||
20 | {constant + constant: value.upper() for value in data} | ||
21 | {constant.attribute: value.upper() for value in data} | ||
| ^^^^^^^^^^^^^^^^^^ RUF011 | ||
| ^^^^^^^^^^^^^^^^^^ B035 | ||
22 | {constant[0]: value.upper() for value in data} | ||
| | ||
|
||
RUF011.py:22:2: RUF011 Dictionary comprehension uses static key: `constant[0]` | ||
B035.py:22:2: B035 Dictionary comprehension uses static key: `constant[0]` | ||
| | ||
20 | {constant + constant: value.upper() for value in data} | ||
21 | {constant.attribute: value.upper() for value in data} | ||
22 | {constant[0]: value.upper() for value in data} | ||
| ^^^^^^^^^^^ RUF011 | ||
| ^^^^^^^^^^^ B035 | ||
| | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.