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

Avoid auto-fixing UP032 if comments are present around format call arguments #6342

Merged
merged 3 commits into from
Aug 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,7 @@ async def c():
).format(a=1)

"{}".format(**c)

"{}".format(
1 # comment
)
26 changes: 20 additions & 6 deletions crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_python_parser::{lexer, Mode, Tok};
use ruff_text_size::TextRange;
use rustc_hash::FxHashMap;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::str::{leading_quote, trailing_quote};
use ruff_source_file::Locator;
Expand Down Expand Up @@ -42,14 +42,16 @@ use crate::rules::pyupgrade::helpers::curly_escape;
#[violation]
pub struct FString;

impl AlwaysAutofixableViolation for FString {
impl Violation for FString {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
format!("Use f-string instead of `format` call")
}

fn autofix_title(&self) -> String {
"Convert to f-string".to_string()
fn autofix_title(&self) -> Option<String> {
Some("Convert to f-string".to_string())
}
}

Expand Down Expand Up @@ -316,7 +318,7 @@ pub(crate) fn f_strings(
return;
}

let Expr::Call(ast::ExprCall { func, .. }) = expr else {
let Expr::Call(ast::ExprCall { func, arguments, .. }) = expr else {
return;
};

Expand Down Expand Up @@ -420,7 +422,19 @@ pub(crate) fn f_strings(
}

let mut diagnostic = Diagnostic::new(FString, expr.range());
if checker.patch(diagnostic.kind.rule()) {

// Avoid autofix if there are comments within the call:
// ```
// "{}".format(
// 0, # 0
// )
// ```
if checker.patch(diagnostic.kind.rule())
&& !checker
.indexer()
.comment_ranges()
.intersects(arguments.range())
{
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
contents,
expr.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,15 @@ UP032_0.py:129:1: UP032 [*] Use f-string instead of `format` call
131 131 | ###
132 132 | # Non-errors

UP032_0.py:202:1: UP032 Use f-string instead of `format` call
|
200 | "{}".format(**c)
201 |
202 | / "{}".format(
203 | | 1 # comment
204 | | )
| |_^ UP032
|
= help: Convert to f-string


Loading