mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Avoid D301
autofix for u
prefixed strings (#8495)
This PR avoids creating the fix for `D301` if the string is prefixed with `u` i.e., it's a unicode string. The reason being that `u` and `r` cannot be used together as it's a syntax error. Refer: https://github.com/astral-sh/ruff/issues/8402#issuecomment-1788783287
This commit is contained in:
parent
e57bccd500
commit
b3c2935fa5
3 changed files with 24 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
|||
use memchr::memchr_iter;
|
||||
|
||||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
|
||||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
|
@ -46,14 +46,16 @@ use crate::docstrings::Docstring;
|
|||
#[violation]
|
||||
pub struct EscapeSequenceInDocstring;
|
||||
|
||||
impl AlwaysFixableViolation for EscapeSequenceInDocstring {
|
||||
impl Violation for EscapeSequenceInDocstring {
|
||||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;
|
||||
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!(r#"Use `r"""` if any backslashes in a docstring"#)
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
format!(r#"Add `r` prefix"#)
|
||||
fn fix_title(&self) -> Option<String> {
|
||||
Some(format!(r#"Add `r` prefix"#))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,10 +76,12 @@ pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
|
|||
}) {
|
||||
let mut diagnostic = Diagnostic::new(EscapeSequenceInDocstring, docstring.range());
|
||||
|
||||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
|
||||
"r".to_owned() + docstring.contents,
|
||||
docstring.range(),
|
||||
)));
|
||||
if !docstring.leading_quote().contains(['u', 'U']) {
|
||||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
|
||||
"r".to_owned() + docstring.contents,
|
||||
docstring.range(),
|
||||
)));
|
||||
}
|
||||
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
|
|
@ -17,4 +17,12 @@ D301.py:2:5: D301 [*] Use `r"""` if any backslashes in a docstring
|
|||
4 4 |
|
||||
5 5 | def double_quotes_backslash_raw():
|
||||
|
||||
D301.py:37:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
36 | def shouldnt_add_raw_here2():
|
||||
37 | u"Sum\\mary."
|
||||
| ^^^^^^^^^^^^^ D301
|
||||
|
|
||||
= help: Add `r` prefix
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue