mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Avoid auto-fixing UP032 if comments are present around format call arguments (#6342)
This commit is contained in:
parent
9bb21283ca
commit
08dd87e04d
3 changed files with 35 additions and 6 deletions
|
@ -198,3 +198,7 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
).format(a=1)
|
).format(a=1)
|
||||||
|
|
||||||
"{}".format(**c)
|
"{}".format(**c)
|
||||||
|
|
||||||
|
"{}".format(
|
||||||
|
1 # comment
|
||||||
|
)
|
||||||
|
|
|
@ -9,7 +9,7 @@ use ruff_python_parser::{lexer, Mode, Tok};
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustc_hash::FxHashMap;
|
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_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::str::{leading_quote, trailing_quote};
|
use ruff_python_ast::str::{leading_quote, trailing_quote};
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
|
@ -42,14 +42,16 @@ use crate::rules::pyupgrade::helpers::curly_escape;
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct FString;
|
pub struct FString;
|
||||||
|
|
||||||
impl AlwaysAutofixableViolation for FString {
|
impl Violation for FString {
|
||||||
|
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
|
||||||
|
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
format!("Use f-string instead of `format` call")
|
format!("Use f-string instead of `format` call")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn autofix_title(&self) -> String {
|
fn autofix_title(&self) -> Option<String> {
|
||||||
"Convert to f-string".to_string()
|
Some("Convert to f-string".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,7 +318,7 @@ pub(crate) fn f_strings(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Expr::Call(ast::ExprCall { func, .. }) = expr else {
|
let Expr::Call(ast::ExprCall { func, arguments, .. }) = expr else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -420,7 +422,19 @@ pub(crate) fn f_strings(
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(FString, expr.range());
|
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(
|
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||||
contents,
|
contents,
|
||||||
expr.range(),
|
expr.range(),
|
||||||
|
|
|
@ -945,4 +945,15 @@ UP032_0.py:129:1: UP032 [*] Use f-string instead of `format` call
|
||||||
131 131 | ###
|
131 131 | ###
|
||||||
132 132 | # Non-errors
|
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…
Add table
Add a link
Reference in a new issue