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

Handle UP032 autofix with adjacent keywords #3636

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP032.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ async def c():

async def c():
return "{}".format(1 + await 3)


def d(osname, version, release):
return"{}-{}.{}".format(osname, version, release)


def e():
yield"{}".format(1)


assert"{}".format(1)
16 changes: 14 additions & 2 deletions crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_hash::FxHashMap;
use rustpython_common::format::{
FieldName, FieldNamePart, FieldType, FormatPart, FormatString, FromTemplate,
};
use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData};
use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData, Location};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -247,7 +247,7 @@ pub(crate) fn f_strings(checker: &mut Checker, summary: &FormatSummary, expr: &E

// Currently, the only issue we know of is in LibCST:
// https://github.com/Instagram/LibCST/issues/846
let Some(contents) = try_convert_to_f_string(checker, expr) else {
let Some(mut contents) = try_convert_to_f_string(checker, expr) else {
return;
};

Expand All @@ -257,6 +257,18 @@ pub(crate) fn f_strings(checker: &mut Checker, summary: &FormatSummary, expr: &E
return;
}

// If necessary, add a space between any leading keyword (`return`, `yield`, `assert`, etc.)
// and the string. For example, `return"foo"` is valid, but `returnf"foo"` is not.
if expr.location.column() > 0 {
let existing = checker.locator.slice(Range::new(
Location::new(expr.location.row(), expr.location.column() - 1),
expr.end_location.unwrap(),
));
if existing.chars().next().unwrap().is_ascii_alphabetic() {
contents.insert(0, ' ');
}
}

let mut diagnostic = Diagnostic::new(FString, Range::from(expr));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,64 @@ expression: diagnostics
row: 47
column: 24
parent: ~
- kind:
name: FString
body: "Use f-string instead of `format` call"
suggestion: Convert to f-string
fixable: true
location:
row: 92
column: 10
end_location:
row: 92
column: 53
fix:
content: " f\"{osname}-{version}.{release}\""
location:
row: 92
column: 10
end_location:
row: 92
column: 53
parent: ~
- kind:
name: FString
body: "Use f-string instead of `format` call"
suggestion: Convert to f-string
fixable: true
location:
row: 96
column: 9
end_location:
row: 96
column: 23
fix:
content: " f\"{1}\""
location:
row: 96
column: 9
end_location:
row: 96
column: 23
parent: ~
- kind:
name: FString
body: "Use f-string instead of `format` call"
suggestion: Convert to f-string
fixable: true
location:
row: 99
column: 6
end_location:
row: 99
column: 20
fix:
content: " f\"{1}\""
location:
row: 99
column: 6
end_location:
row: 99
column: 20
parent: ~