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 1 commit
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)
26 changes: 21 additions & 5 deletions crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,33 @@ 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;
};

// Avoid refactors that increase the resulting string length.
let existing = checker.locator.slice(expr);
if contents.len() > existing.len() {
return;
let expr_range = Range::from(expr);
if expr_range.location.column() == 0 {
let existing = checker.locator.slice(expr_range);
if contents.len() > existing.len() {
return;
}
} else {
let existing = checker.locator.slice(Range::new(
expr_range.location.with_col_offset(-1),
expr_range.end_location,
));
if contents.len() > existing.len() - 1 {
return;
}
// Add a space if there is none between a keyword and the string.
// `return`, `yield`, `assert`, …
charliermarsh marked this conversation as resolved.
Show resolved Hide resolved
if existing.chars().next().unwrap().is_ascii_alphabetic() {
contents.insert(0, ' ');
}
}

let mut diagnostic = Diagnostic::new(FString, Range::from(expr));
let mut diagnostic = Diagnostic::new(FString, expr_range);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
contents,
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: ~