Skip to content

Commit

Permalink
Remove call when removing final argument from format
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 7, 2025
1 parent 5e9259c commit 27b19e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F523.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
# Multiline
(''
.format(2))

# Removing the final argument.
"Hello".format("world")
22 changes: 20 additions & 2 deletions crates/ruff_linter/src/rules/pyflakes/fixes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::{Context, Ok, Result};

use libcst_native::Expression;
use ruff_diagnostics::Edit;
use ruff_python_ast as ast;
use ruff_python_ast::Expr;
use ruff_python_codegen::Stylist;
use ruff_python_semantic::Binding;
use ruff_python_trivia::{BackwardsTokenizer, SimpleTokenKind, SimpleTokenizer};
Expand Down Expand Up @@ -69,6 +70,18 @@ pub(crate) fn remove_unused_positional_arguments_from_format_call(
locator: &Locator,
stylist: &Stylist,
) -> Result<Edit> {
// If we're removing _all_ arguments, we can remove the entire call.
//
// For example, `"Hello".format(", world!")` -> `"Hello"`, as opposed to `"Hello".format()`.
if unused_arguments.len() == call.arguments.len() {
if let Expr::Attribute(attribute) = &*call.func {
return Ok(Edit::range_replacement(
locator.slice(&*attribute.value).to_string(),
call.range(),
));
}
}

let source_code = locator.slice(call);
transform_expression(source_code, stylist, |mut expression| {
let call = match_call_mut(&mut expression)?;
Expand All @@ -81,7 +94,12 @@ pub(crate) fn remove_unused_positional_arguments_from_format_call(
!is_unused
});

Ok(expression)
// If there are no arguments left, remove the parentheses.
if call.args.is_empty() {
Ok((&*call.func).clone())
} else {
Ok(expression)
}
})
.map(|output| Edit::range_replacement(output, call.range()))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
snapshot_kind: text
---
F523.py:2:1: F523 [*] `.format` call has unused arguments at position(s): 1
|
Expand Down Expand Up @@ -255,12 +254,33 @@ F523.py:32:2: F523 [*] `.format` call has unused arguments at position(s): 0
| __^
33 | | .format(2))
| |__________^ F523
34 |
35 | # Removing the final argument.
|
= help: Remove extra positional arguments at position(s): 0

Safe fix
29 29 | "{1} {8}".format(0, 1) # F523, # F524
30 30 |
31 31 | # Multiline
32 32 | (''
32 |-(''
33 |-.format(2))
33 |+.format())
32 |+('')
34 33 |
35 34 | # Removing the final argument.
36 35 | "Hello".format("world")

F523.py:36:1: F523 [*] `.format` call has unused arguments at position(s): 0
|
35 | # Removing the final argument.
36 | "Hello".format("world")
| ^^^^^^^^^^^^^^^^^^^^^^^ F523
|
= help: Remove extra positional arguments at position(s): 0

Safe fix
33 33 | .format(2))
34 34 |
35 35 | # Removing the final argument.
36 |-"Hello".format("world")
36 |+"Hello"

0 comments on commit 27b19e6

Please sign in to comment.