Skip to content

Commit

Permalink
Fix in preview
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 14, 2024
1 parent 4e4734e commit a88fd83
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 93 deletions.
19 changes: 19 additions & 0 deletions crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests {

use crate::assert_messages;
use crate::registry::Rule;
use crate::settings::types::PreviewMode;
use crate::settings::LinterSettings;
use crate::test::test_path;

Expand Down Expand Up @@ -69,6 +70,24 @@ mod tests {
Ok(())
}

#[test_case(Rule::DuplicateValue, Path::new("B033.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("flake8_bugbear").join(path).as_path(),
&LinterSettings {
preview: PreviewMode::Enabled,
..LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test]
fn zip_without_explicit_strict() -> Result<()> {
let snapshot = "B905.py";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Context, Result};
use rustc_hash::FxHashSet;

use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast as ast;
use ruff_python_ast::comparable::ComparableExpr;
Expand Down Expand Up @@ -32,15 +32,17 @@ pub struct DuplicateValue {
value: String,
}

impl AlwaysFixableViolation for DuplicateValue {
impl Violation for DuplicateValue {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

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

fn fix_title(&self) -> String {
format!("Remove duplicate item")
fn fix_title(&self) -> Option<String> {
Some(format!("Remove duplicate item"))
}
}

Expand All @@ -59,9 +61,11 @@ pub(crate) fn duplicate_value(checker: &mut Checker, set: &ast::ExprSet) {
elt.range(),
);

diagnostic.try_set_fix(|| {
remove_member(set, index, checker.locator().contents()).map(Fix::safe_edit)
});
if checker.settings.preview.is_enabled() {
diagnostic.try_set_fix(|| {
remove_member(set, index, checker.locator().contents()).map(Fix::safe_edit)
});
}

checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
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 | ###
Expand All @@ -12,17 +12,7 @@ B033.py:4:35: B033 [*] Sets should not contain duplicate item `"value1"`
|
= help: Remove duplicate item

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`
B033.py:5:21: B033 Sets should not contain duplicate item `1`
|
3 | ###
4 | incorrect_set = {"value1", 23, 5, "value1"}
Expand All @@ -33,17 +23,7 @@ B033.py:5:21: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

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"`
B033.py:10:5: B033 Sets should not contain duplicate item `"value1"`
|
8 | 23,
9 | 5,
Expand All @@ -54,16 +34,7 @@ B033.py:10:5: B033 [*] Sets should not contain duplicate item `"value1"`
|
= help: Remove duplicate item

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`
B033.py:13:21: B033 Sets should not contain duplicate item `1`
|
11 | # B033
12 | }
Expand All @@ -74,17 +45,7 @@ B033.py:13:21: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

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 | incorrect_set = {0, 1, 1,}
16 16 | incorrect_set = {0, 1, 1}

B033.py:14:21: B033 [*] Sets should not contain duplicate item `1`
B033.py:14:21: B033 Sets should not contain duplicate item `1`
|
12 | }
13 | incorrect_set = {1, 1}
Expand All @@ -95,17 +56,7 @@ B033.py:14:21: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

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

B033.py:15:24: B033 [*] Sets should not contain duplicate item `1`
B033.py:15:24: B033 Sets should not contain duplicate item `1`
|
13 | incorrect_set = {1, 1}
14 | incorrect_set = {1, 1,}
Expand All @@ -116,17 +67,7 @@ B033.py:15:24: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

Safe fix
12 12 | }
13 13 | incorrect_set = {1, 1}
14 14 | incorrect_set = {1, 1,}
15 |-incorrect_set = {0, 1, 1,}
15 |+incorrect_set = {0, 1,}
16 16 | incorrect_set = {0, 1, 1}
17 17 | incorrect_set = {
18 18 | 0,

B033.py:16:24: B033 [*] Sets should not contain duplicate item `1`
B033.py:16:24: B033 Sets should not contain duplicate item `1`
|
14 | incorrect_set = {1, 1,}
15 | incorrect_set = {0, 1, 1,}
Expand All @@ -137,17 +78,7 @@ B033.py:16:24: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

Safe fix
13 13 | incorrect_set = {1, 1}
14 14 | incorrect_set = {1, 1,}
15 15 | incorrect_set = {0, 1, 1,}
16 |-incorrect_set = {0, 1, 1}
16 |+incorrect_set = {0, 1}
17 17 | incorrect_set = {
18 18 | 0,
19 19 | 1,

B033.py:20:5: B033 [*] Sets should not contain duplicate item `1`
B033.py:20:5: B033 Sets should not contain duplicate item `1`
|
18 | 0,
19 | 1,
Expand All @@ -157,13 +88,4 @@ B033.py:20:5: B033 [*] Sets should not contain duplicate item `1`
|
= help: Remove duplicate item

Safe fix
17 17 | incorrect_set = {
18 18 | 0,
19 19 | 1,
20 |- 1,
21 20 | }
22 21 |
23 22 | ###


0 comments on commit a88fd83

Please sign in to comment.