diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F523.py b/crates/ruff/resources/test/fixtures/pyflakes/F523.py index 33283dbd48..6b2985924b 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F523.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F523.py @@ -26,3 +26,7 @@ # With modified indexes "{1}{2}".format(1, 2, 3) # F523, # F524 "{1}{3}".format(1, 2, 3, 4) # F523, # F524 + +# Not fixable +('' +.format(2)) diff --git a/crates/ruff/src/rules/pyflakes/rules/strings.rs b/crates/ruff/src/rules/pyflakes/rules/strings.rs index 7ff92c6867..7771575019 100644 --- a/crates/ruff/src/rules/pyflakes/rules/strings.rs +++ b/crates/ruff/src/rules/pyflakes/rules/strings.rs @@ -4,7 +4,7 @@ use ruff_text_size::TextRange; use rustc_hash::FxHashSet; use rustpython_parser::ast::{self, Constant, Expr, Identifier, Keyword}; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Violation}; +use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use crate::checkers::ast::Checker; @@ -425,7 +425,9 @@ pub struct StringDotFormatExtraPositionalArguments { missing: Vec, } -impl AlwaysAutofixableViolation for StringDotFormatExtraPositionalArguments { +impl Violation for StringDotFormatExtraPositionalArguments { + const AUTOFIX: AutofixKind = AutofixKind::Sometimes; + #[derive_message_formats] fn message(&self) -> String { let StringDotFormatExtraPositionalArguments { missing } = self; @@ -433,10 +435,12 @@ impl AlwaysAutofixableViolation for StringDotFormatExtraPositionalArguments { format!("`.format` call has unused arguments at position(s): {message}") } - fn autofix_title(&self) -> String { + fn autofix_title(&self) -> Option { let StringDotFormatExtraPositionalArguments { missing } = self; let message = missing.join(", "); - format!("Remove extra positional arguments at position(s): {message}") + Some(format!( + "Remove extra positional arguments at position(s): {message}" + )) } } diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F523_F523.py.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F523_F523.py.snap index 1ec240a042..2b71c0be4e 100644 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F523_F523.py.snap +++ b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F523_F523.py.snap @@ -261,6 +261,8 @@ F523.py:27:1: F523 [*] `.format` call has unused arguments at position(s): 0 27 |-"{1}{2}".format(1, 2, 3) # F523, # F524 27 |+"{0}{1}".format(2, 3) # F523, # F524 28 28 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524 +29 29 | +30 30 | # Not fixable F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2 | @@ -268,6 +270,8 @@ F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2 29 | "{1}{2}".format(1, 2, 3) # F523, # F524 30 | "{1}{3}".format(1, 2, 3, 4) # F523, # F524 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ F523 +31 | +32 | # Not fixable | = help: Remove extra positional arguments at position(s): 0, 2 @@ -277,5 +281,18 @@ F523.py:28:1: F523 [*] `.format` call has unused arguments at position(s): 0, 2 27 27 | "{1}{2}".format(1, 2, 3) # F523, # F524 28 |-"{1}{3}".format(1, 2, 3, 4) # F523, # F524 28 |+"{0}{1}".format(2, 4) # F523, # F524 +29 29 | +30 30 | # Not fixable +31 31 | ('' + +F523.py:31:2: F523 `.format` call has unused arguments at position(s): 0 + | +31 | # Not fixable +32 | ('' + | __^ +33 | | .format(2)) + | |__________^ F523 + | + = help: Remove extra positional arguments at position(s): 0