-
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.
Move duplicate-value rule to flake8-bugbear (#4882)
- Loading branch information
1 parent
bbf9628
commit 7d04aca
Showing
12 changed files
with
110 additions
and
52 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
57 changes: 57 additions & 0 deletions
57
crates/ruff/src/rules/flake8_bugbear/rules/duplicate_value.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,57 @@ | ||
use rustc_hash::FxHashSet; | ||
use rustpython_parser::ast::{self, Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::comparable::ComparableExpr; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for set literals that contain duplicate items. | ||
/// | ||
/// ## Why is this bad? | ||
/// In Python, sets are unordered collections of unique elements. Including a | ||
/// duplicate item in a set literal is redundant, as the duplicate item will be | ||
/// replaced with a single item at runtime. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// {1, 2, 3, 1} | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// {1, 2, 3} | ||
/// ``` | ||
#[violation] | ||
pub struct DuplicateValue { | ||
value: String, | ||
} | ||
|
||
impl Violation for DuplicateValue { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let DuplicateValue { value } = self; | ||
format!("Sets should not contain duplicate item `{value}`") | ||
} | ||
} | ||
|
||
/// B033 | ||
pub(crate) fn duplicate_value(checker: &mut Checker, elts: &Vec<Expr>) { | ||
let mut seen_values: FxHashSet<ComparableExpr> = FxHashSet::default(); | ||
for elt in elts { | ||
if let Expr::Constant(ast::ExprConstant { value, .. }) = elt { | ||
let comparable_value: ComparableExpr = elt.into(); | ||
|
||
if !seen_values.insert(comparable_value) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
DuplicateValue { | ||
value: checker.generator().constant(value), | ||
}, | ||
elt.range(), | ||
)); | ||
} | ||
}; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
.../src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B033_B033.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
B033.py:4:35: B033 Sets should not contain duplicate item `"value1"` | ||
| | ||
4 | # Errors. | ||
5 | ### | ||
6 | incorrect_set = {"value1", 23, 5, "value1"} | ||
| ^^^^^^^^ B033 | ||
7 | incorrect_set = {1, 1} | ||
| | ||
|
||
B033.py:5:21: B033 Sets should not contain duplicate item `1` | ||
| | ||
5 | ### | ||
6 | incorrect_set = {"value1", 23, 5, "value1"} | ||
7 | incorrect_set = {1, 1} | ||
| ^ B033 | ||
8 | | ||
9 | ### | ||
| | ||
|
||
|
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
23 changes: 0 additions & 23 deletions
23
...ff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW0130_duplicate_value.py.snap
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.