Skip to content

Commit

Permalink
tweak logic
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 14, 2024
1 parent d07dcbb commit ac694f5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Errors.
###
incorrect_set = {"value1", 23, 5, "value1"}
incorrect_set = {1, 1}
incorrect_set = {1, 1, 2, 1}

###
# Non-errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,45 @@ impl AlwaysFixableViolation for DuplicateValue {
/// B033
pub(crate) fn duplicate_value(checker: &mut Checker, elts: &[Expr], range: TextRange) {
let mut seen_values: FxHashSet<ComparableExpr> = FxHashSet::default();
let mut duplicate_indices: Vec<usize> = Vec::new();
let mut unique_elts: Vec<Expr> = Vec::new();

for (index, elt) in elts.iter().enumerate() {
if elt.is_literal_expr() {
let comparable_value: ComparableExpr = elt.into();

if !seen_values.insert(comparable_value) {
let mut diagnostic = Diagnostic::new(
DuplicateValue {
value: checker.generator().expr(elt),
},
elt.range(),
);
if seen_values.insert(comparable_value) {
unique_elts.push(elt.to_owned());
} else {
duplicate_indices.push(index);
}
} else {
unique_elts.push(elt.to_owned());
}
}

if duplicate_indices.is_empty() {
return;
}

let mut elts_without_duplicate = elts.to_owned();
elts_without_duplicate.remove(index);
let replacement = checker.generator().expr(&Expr::Set(ExprSet {
elts: unique_elts,
range: TextRange::default(),
}));

diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
checker.generator().expr(&Expr::Set(ExprSet {
elts: elts_without_duplicate,
range,
})),
range,
)));
let fix = Fix::safe_edit(Edit::range_replacement(replacement, range));

checker.diagnostics.push(diagnostic);
}
};
for index in duplicate_indices {
let elt = &elts[index];
let mut diagnostic = Diagnostic::new(
DuplicateValue {
value: checker.generator().expr(elt),
},
elt.range(),
);

diagnostic.set_fix(fix.clone());

checker.diagnostics.push(diagnostic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ B033.py:4:35: B033 [*] Sets should not contain duplicate item `"value1"`
3 | ###
4 | incorrect_set = {"value1", 23, 5, "value1"}
| ^^^^^^^^ B033
5 | incorrect_set = {1, 1}
5 | incorrect_set = {1, 1, 2, 1}
|
= help: Remove duplicate item `"value1"`

Expand All @@ -17,15 +17,15 @@ B033.py:4:35: B033 [*] Sets should not contain duplicate item `"value1"`
3 3 | ###
4 |-incorrect_set = {"value1", 23, 5, "value1"}
4 |+incorrect_set = {"value1", 23, 5}
5 5 | incorrect_set = {1, 1}
5 5 | incorrect_set = {1, 1, 2, 1}
6 6 |
7 7 | ###

B033.py:5:21: B033 [*] Sets should not contain duplicate item `1`
|
3 | ###
4 | incorrect_set = {"value1", 23, 5, "value1"}
5 | incorrect_set = {1, 1}
5 | incorrect_set = {1, 1, 2, 1}
| ^ B033
6 |
7 | ###
Expand All @@ -36,8 +36,29 @@ B033.py:5:21: B033 [*] Sets should not contain duplicate item `1`
2 2 | # Errors.
3 3 | ###
4 4 | incorrect_set = {"value1", 23, 5, "value1"}
5 |-incorrect_set = {1, 1}
5 |+incorrect_set = {1}
5 |-incorrect_set = {1, 1, 2, 1}
5 |+incorrect_set = {1, 2}
6 6 |
7 7 | ###
8 8 | # Non-errors.

B033.py:5:27: B033 [*] Sets should not contain duplicate item `1`
|
3 | ###
4 | incorrect_set = {"value1", 23, 5, "value1"}
5 | incorrect_set = {1, 1, 2, 1}
| ^ B033
6 |
7 | ###
|
= help: Remove duplicate item `1`

Safe fix
2 2 | # Errors.
3 3 | ###
4 4 | incorrect_set = {"value1", 23, 5, "value1"}
5 |-incorrect_set = {1, 1, 2, 1}
5 |+incorrect_set = {1, 2}
6 6 |
7 7 | ###
8 8 | # Non-errors.
Expand Down

0 comments on commit ac694f5

Please sign in to comment.