Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-bugbear] Add fix for duplicate-value (B033) #9510

Merged
merged 6 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B033.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
# Errors.
###
incorrect_set = {"value1", 23, 5, "value1"}
incorrect_set = {1, 1, 2}
incorrect_set_multiline = {
"value1",
23,
5,
"value1",
# B033
}
incorrect_set = {1, 1}
incorrect_set = {1, 1,}

###
# Non-errors.
###
correct_set = {"value1", 23, 5}
correct_set = {5, "5"}
correct_set = {5}
correct_set = {}
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
flake8_pie::rules::unnecessary_spread(checker, dict);
}
}
Expr::Set(ast::ExprSet { elts, range: _ }) => {
Expr::Set(_) => {
if checker.enabled(Rule::DuplicateValue) {
flake8_bugbear::rules::duplicate_value(checker, elts);
flake8_bugbear::rules::duplicate_value(checker, expr);
}
}
Expr::Yield(_) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use ruff_python_ast::Expr;
use anyhow::{Context, Result};

use ruff_python_ast::{Expr, ExprSet};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use rustc_hash::FxHashSet;

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -30,29 +33,99 @@ pub struct DuplicateValue {
value: String,
}

impl Violation for DuplicateValue {
impl AlwaysFixableViolation for DuplicateValue {
#[derive_message_formats]
fn message(&self) -> String {
let DuplicateValue { value } = self;
format!("Sets should not contain duplicate item `{value}`")
}

fn fix_title(&self) -> String {
let DuplicateValue { value } = self;
format!("Remove duplicate item `{value}`")
}
}

/// B033
pub(crate) fn duplicate_value(checker: &mut Checker, elts: &Vec<Expr>) {
pub(crate) fn duplicate_value(checker: &mut Checker, expr: &Expr) {
let Expr::Set(ExprSet { elts, .. }) = expr else {
return;
};

let mut seen_values: FxHashSet<ComparableExpr> = FxHashSet::default();
for elt in elts {
let mut duplicate_indices: Vec<usize> = Vec::new();
let mut unique_indices: Vec<usize> = 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) {
checker.diagnostics.push(Diagnostic::new(
DuplicateValue {
value: checker.generator().expr(elt),
},
elt.range(),
));
if seen_values.insert(comparable_value) {
unique_indices.push(index);
} else {
duplicate_indices.push(index);
}
};
} else {
unique_indices.push(index);
}
}

for index in duplicate_indices {
let elt = &elts[index];

let mut diagnostic = Diagnostic::new(
DuplicateValue {
value: checker.generator().expr(elt),
},
elt.range(),
);

diagnostic.try_set_fix(|| {
remove_member(elt, elts, checker.locator().contents()).map(Fix::safe_edit)
});

checker.diagnostics.push(diagnostic);
}
}

fn remove_member(expr: &Expr, elts: &[Expr], source: &str) -> Result<Edit> {
let (before, after): (Vec<_>, Vec<_>) = elts
.iter()
.map(Ranged::range)
.filter(|range| expr.range() != *range)
.partition(|range| range.start() < expr.start());

if !after.is_empty() {
// Case 1: expr is _not_ the last node, so delete from the start of the
// expr to the end of the subsequent comma.
let mut tokenizer = SimpleTokenizer::starts_at(expr.end(), source);

// Find the trailing comma.
tokenizer
.find(|token| token.kind == SimpleTokenKind::Comma)
.context("Unable to find trailing comma")?;

// Find the next non-whitespace token.
let next = tokenizer
.find(|token| {
token.kind != SimpleTokenKind::Whitespace && token.kind != SimpleTokenKind::Newline
})
.context("Unable to find next token")?;

Ok(Edit::deletion(expr.start(), next.start()))
} else if let Some(previous) = before.iter().map(Ranged::end).max() {
// Case 2: expr is the last node, so delete from the start of the
// previous comma to the end of the expr.
let mut tokenizer = SimpleTokenizer::starts_at(previous, source);

// Find the trailing comma.
let comma = tokenizer
.find(|token| token.kind == SimpleTokenKind::Comma)
.context("Unable to find trailing comma")?;

Ok(Edit::deletion(comma.start(), expr.end()))
} else {
// Case 3: expr is the only node, so delete it
Ok(Edit::range_deletion(expr.range()))
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,107 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B033.py:4:35: B033 Sets should not contain duplicate item `"value1"`
B033.py:4:35: B033 [*] Sets should not contain duplicate item `"value1"`
|
2 | # Errors.
3 | ###
4 | incorrect_set = {"value1", 23, 5, "value1"}
| ^^^^^^^^ B033
5 | incorrect_set = {1, 1}
5 | incorrect_set = {1, 1, 2}
6 | incorrect_set_multiline = {
|
= help: Remove duplicate item `"value1"`

B033.py:5:21: B033 Sets should not contain duplicate item `1`
ℹ Safe fix
1 1 | ###
2 2 | # Errors.
3 3 | ###
4 |-incorrect_set = {"value1", 23, 5, "value1"}
4 |+incorrect_set = {"value1", 23, 5}
5 5 | incorrect_set = {1, 1, 2}
6 6 | incorrect_set_multiline = {
7 7 | "value1",

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}
| ^ B033
6 |
7 | ###
6 | incorrect_set_multiline = {
7 | "value1",
|
= 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}
5 |+incorrect_set = {1, 2}
6 6 | incorrect_set_multiline = {
7 7 | "value1",
8 8 | 23,

B033.py:10:5: B033 [*] Sets should not contain duplicate item `"value1"`
|
8 | 23,
9 | 5,
10 | "value1",
| ^^^^^^^^ B033
11 | # B033
12 | }
|
= help: Remove duplicate item `"value1"`

ℹ Safe fix
7 7 | "value1",
8 8 | 23,
9 9 | 5,
10 |- "value1",
11 10 | # B033
12 11 | }
13 12 | incorrect_set = {1, 1}

B033.py:13:21: B033 [*] Sets should not contain duplicate item `1`
|
11 | # B033
12 | }
13 | incorrect_set = {1, 1}
| ^ B033
14 | incorrect_set = {1, 1,}
|
= help: Remove duplicate item `1`

ℹ Safe fix
10 10 | "value1",
11 11 | # B033
12 12 | }
13 |-incorrect_set = {1, 1}
13 |+incorrect_set = {1}
14 14 | incorrect_set = {1, 1,}
15 15 |
16 16 | ###

B033.py:14:21: B033 [*] Sets should not contain duplicate item `1`
|
12 | }
13 | incorrect_set = {1, 1}
14 | incorrect_set = {1, 1,}
| ^ B033
15 |
16 | ###
|
= help: Remove duplicate item `1`

ℹ Safe fix
11 11 | # B033
12 12 | }
13 13 | incorrect_set = {1, 1}
14 |-incorrect_set = {1, 1,}
14 |+incorrect_set = {1,}
15 15 |
16 16 | ###
17 17 | # Non-errors.


Loading