Skip to content

Commit

Permalink
Remove functor from autofix title (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored May 10, 2023
1 parent 8969ad5 commit a2b8487
Show file tree
Hide file tree
Showing 61 changed files with 234 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,12 @@ impl Violation for UnusedLoopControlVariable {
}
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
let UnusedLoopControlVariable {
certainty, rename, ..
} = self;
if certainty.to_bool() && rename.is_some() {
Some(|UnusedLoopControlVariable { name, rename, .. }| {
let rename = rename.as_ref().unwrap();
format!("Rename unused `{name}` to `{rename}`")
})
} else {
None
}
fn autofix_title(&self) -> Option<String> {
let UnusedLoopControlVariable { rename, name, .. } = self;

rename
.as_ref()
.map(|rename| format!("Rename unused `{name}` to `{rename}`"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ B007.py:34:10: B007 Loop control variable `bar` may not be used within loop body
36 | if foo:
37 | print(FMT.format(**locals()))
|
= help: Rename unused `bar` to `_bar`

B007.py:38:10: B007 Loop control variable `bar` may not be used within loop body
|
Expand All @@ -75,6 +76,7 @@ B007.py:38:10: B007 Loop control variable `bar` may not be used within loop body
41 | if foo:
42 | print(FMT.format(**globals()))
|
= help: Rename unused `bar` to `_bar`

B007.py:42:10: B007 Loop control variable `bar` may not be used within loop body
|
Expand All @@ -85,6 +87,7 @@ B007.py:42:10: B007 Loop control variable `bar` may not be used within loop body
45 | if foo:
46 | print(FMT.format(**vars()))
|
= help: Rename unused `bar` to `_bar`

B007.py:46:10: B007 Loop control variable `bar` may not be used within loop body
|
Expand All @@ -94,6 +97,7 @@ B007.py:46:10: B007 Loop control variable `bar` may not be used within loop body
| ^^^ B007
49 | print(FMT.format(foo=foo, bar=eval("bar")))
|
= help: Rename unused `bar` to `_bar`

B007.py:52:14: B007 [*] Loop control variable `bar` not used within loop body
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl Violation for UnnecessaryComprehensionAnyAll {
format!("Unnecessary list comprehension.")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
Some(|_| "Remove unnecessary list comprehension".to_string())
fn autofix_title(&self) -> Option<String> {
Some("Remove unnecessary list comprehension".to_string())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ impl Violation for UnnecessaryMap {
}
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
Some(|UnnecessaryMap { obj_type }| {
if obj_type == "generator" {
format!("Replace `map` using a generator expression")
} else {
format!("Replace `map` using a `{obj_type}` comprehension")
}
fn autofix_title(&self) -> Option<String> {
let UnnecessaryMap { obj_type } = self;
Some(if obj_type == "generator" {
format!("Replace `map` using a generator expression")
} else {
format!("Replace `map` using a `{obj_type}` comprehension")
})
}
}
Expand Down
50 changes: 14 additions & 36 deletions crates/ruff/src/rules/flake8_errmsg/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ use crate::registry::{AsRule, Rule};
/// RuntimeError: 'Some value' is incorrect
/// ```
#[violation]
pub struct RawStringInException {
fixable: bool,
}
pub struct RawStringInException;

impl Violation for RawStringInException {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
Expand All @@ -59,9 +57,8 @@ impl Violation for RawStringInException {
format!("Exception must not use a string literal, assign to variable first")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable
.then_some(|_| format!("Assign to variable; remove string literal"))
fn autofix_title(&self) -> Option<String> {
Some("Assign to variable; remove string literal".to_string())
}
}

Expand Down Expand Up @@ -104,9 +101,7 @@ impl Violation for RawStringInException {
/// RuntimeError: 'Some value' is incorrect
/// ```
#[violation]
pub struct FStringInException {
fixable: bool,
}
pub struct FStringInException;

impl Violation for FStringInException {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
Expand All @@ -116,9 +111,8 @@ impl Violation for FStringInException {
format!("Exception must not use an f-string literal, assign to variable first")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable
.then_some(|_| format!("Assign to variable; remove f-string literal"))
fn autofix_title(&self) -> Option<String> {
Some("Assign to variable; remove f-string literal".to_string())
}
}

Expand Down Expand Up @@ -163,9 +157,7 @@ impl Violation for FStringInException {
/// RuntimeError: 'Some value' is incorrect
/// ```
#[violation]
pub struct DotFormatInException {
fixable: bool,
}
pub struct DotFormatInException;

impl Violation for DotFormatInException {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
Expand All @@ -175,9 +167,8 @@ impl Violation for DotFormatInException {
format!("Exception must not use a `.format()` string directly, assign to variable first")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable
.then_some(|_| format!("Assign to variable; remove `.format()` string"))
fn autofix_title(&self) -> Option<String> {
Some("Assign to variable; remove `.format()` string".to_string())
}
}

Expand Down Expand Up @@ -241,12 +232,8 @@ pub fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr) {
None
}
});
let mut diagnostic = Diagnostic::new(
RawStringInException {
fixable: indentation.is_some(),
},
first.range(),
);
let mut diagnostic =
Diagnostic::new(RawStringInException, first.range());
if let Some(indentation) = indentation {
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(generate_fix(
Expand All @@ -273,12 +260,7 @@ pub fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr) {
}
},
);
let mut diagnostic = Diagnostic::new(
FStringInException {
fixable: indentation.is_some(),
},
first.range(),
);
let mut diagnostic = Diagnostic::new(FStringInException, first.range());
if let Some(indentation) = indentation {
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(generate_fix(
Expand All @@ -305,12 +287,8 @@ pub fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr) {
None
}
});
let mut diagnostic = Diagnostic::new(
DotFormatInException {
fixable: indentation.is_some(),
},
first.range(),
);
let mut diagnostic =
Diagnostic::new(DotFormatInException, first.range());
if let Some(indentation) = indentation {
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(generate_fix(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ EM.py:28:24: EM101 Exception must not use a string literal, assign to variable f
30 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

EM.py:35:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
Expand Down Expand Up @@ -93,6 +94,7 @@ EM.py:42:28: EM101 Exception must not use a string literal, assign to variable f
43 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

EM.py:47:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
Expand Down Expand Up @@ -164,6 +166,7 @@ EM.py:55:28: EM101 Exception must not use a string literal, assign to variable f
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
57 | if foo: x = 1; raise RuntimeError("This is an example exception")
|
= help: Assign to variable; remove string literal

EM.py:56:35: EM101 Exception must not use a string literal, assign to variable first
|
Expand All @@ -172,5 +175,6 @@ EM.py:56:35: EM101 Exception must not use a string literal, assign to variable f
58 | if foo: x = 1; raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal


Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ EM.py:28:24: EM101 Exception must not use a string literal, assign to variable f
30 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

EM.py:35:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
Expand Down Expand Up @@ -112,6 +113,7 @@ EM.py:42:28: EM101 Exception must not use a string literal, assign to variable f
43 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

EM.py:47:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
Expand Down Expand Up @@ -183,6 +185,7 @@ EM.py:55:28: EM101 Exception must not use a string literal, assign to variable f
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
57 | if foo: x = 1; raise RuntimeError("This is an example exception")
|
= help: Assign to variable; remove string literal

EM.py:56:35: EM101 Exception must not use a string literal, assign to variable first
|
Expand All @@ -191,5 +194,6 @@ EM.py:56:35: EM101 Exception must not use a string literal, assign to variable f
58 | if foo: x = 1; raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal


23 changes: 8 additions & 15 deletions crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ use super::unittest_assert::UnittestAssert;
/// assert not something_else
/// ```
#[violation]
pub struct PytestCompositeAssertion {
fixable: bool,
}
pub struct PytestCompositeAssertion;

impl Violation for PytestCompositeAssertion {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
Expand All @@ -63,9 +61,8 @@ impl Violation for PytestCompositeAssertion {
format!("Assertion should be broken down into multiple parts")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable
.then_some(|_| format!("Break down assertion into multiple parts"))
fn autofix_title(&self) -> Option<String> {
Some("Break down assertion into multiple parts".to_string())
}
}

Expand Down Expand Up @@ -97,23 +94,20 @@ impl Violation for PytestAssertAlwaysFalse {
#[violation]
pub struct PytestUnittestAssertion {
assertion: String,
fixable: bool,
}

impl Violation for PytestUnittestAssertion {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
let PytestUnittestAssertion { assertion, .. } = self;
let PytestUnittestAssertion { assertion } = self;
format!("Use a regular `assert` instead of unittest-style `{assertion}`")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
self.fixable
.then_some(|PytestUnittestAssertion { assertion, .. }| {
format!("Replace `{assertion}(...)` with `assert ...`")
})
fn autofix_title(&self) -> Option<String> {
let PytestUnittestAssertion { assertion } = self;
Some(format!("Replace `{assertion}(...)` with `assert ...`"))
}
}

Expand Down Expand Up @@ -198,7 +192,6 @@ pub fn unittest_assertion(
let mut diagnostic = Diagnostic::new(
PytestUnittestAssertion {
assertion: unittest_assert.to_string(),
fixable,
},
func.range(),
);
Expand Down Expand Up @@ -426,7 +419,7 @@ pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:
let fixable = matches!(composite, CompositionKind::Simple)
&& msg.is_none()
&& !has_comments_in(stmt.range(), checker.locator);
let mut diagnostic = Diagnostic::new(PytestCompositeAssertion { fixable }, stmt.range());
let mut diagnostic = Diagnostic::new(PytestCompositeAssertion, stmt.range());
if fixable && checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.try_set_fix_from_edit(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ impl Violation for PytestParametrizeNamesWrongType {
format!("Wrong name(s) type in `@pytest.mark.parametrize`, expected `{expected}`")
}

fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
Some(|violation| {
let PytestParametrizeNamesWrongType { expected } = violation;
format!("Use a `{expected}` for parameter names")
})
fn autofix_title(&self) -> Option<String> {
let PytestParametrizeNamesWrongType { expected } = self;
Some(format!("Use a `{expected}` for parameter names"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ PT009.py:21:13: PT009 Use a regular `assert` instead of unittest-style `assertIs
24 | if expect_condition
25 | else self.assertIsNone(value) # Error, unfixable
|
= help: Replace `assertIsNotNone(...)` with `assert ...`

PT009.py:23:18: PT009 Use a regular `assert` instead of unittest-style `assertIsNone`
|
Expand All @@ -169,6 +170,7 @@ PT009.py:23:18: PT009 Use a regular `assert` instead of unittest-style `assertIs
26 | )
27 | return self.assertEqual(True, False) # Error, unfixable
|
= help: Replace `assertIsNone(...)` with `assert ...`

PT009.py:25:16: PT009 Use a regular `assert` instead of unittest-style `assertEqual`
|
Expand All @@ -179,6 +181,7 @@ PT009.py:25:16: PT009 Use a regular `assert` instead of unittest-style `assertEq
28 |
29 | def test_assert_false(self):
|
= help: Replace `assertEqual(...)` with `assert ...`

PT009.py:28:9: PT009 [*] Use a regular `assert` instead of unittest-style `assertFalse`
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ PT018.py:30:5: PT018 Assertion should be broken down into multiple parts
32 | assert not (something or something_else and something_third), "with message"
33 | # detected, but no autofix for mixed conditions (e.g. `a or b and c`)
|
= help: Break down assertion into multiple parts

PT018.py:31:5: PT018 Assertion should be broken down into multiple parts
|
Expand All @@ -239,6 +240,7 @@ PT018.py:31:5: PT018 Assertion should be broken down into multiple parts
34 | # detected, but no autofix for mixed conditions (e.g. `a or b and c`)
35 | assert not (something or something_else and something_third)
|
= help: Break down assertion into multiple parts

PT018.py:33:5: PT018 Assertion should be broken down into multiple parts
|
Expand All @@ -249,6 +251,7 @@ PT018.py:33:5: PT018 Assertion should be broken down into multiple parts
36 | # detected, but no autofix for parenthesized conditions
37 | assert (
|
= help: Break down assertion into multiple parts

PT018.py:35:5: PT018 Assertion should be broken down into multiple parts
|
Expand Down
Loading

0 comments on commit a2b8487

Please sign in to comment.