mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
add autofix for D301
(#7970)
## Summary Add fix for `D301` ## Test Plan `cargo test` and manually
This commit is contained in:
parent
195c000f5a
commit
dda4ceda71
4 changed files with 39 additions and 31 deletions
|
@ -10,6 +10,10 @@ def double_quotes_backslash_uppercase():
|
|||
R"""Sum\\mary."""
|
||||
|
||||
|
||||
def shouldnt_add_raw_here():
|
||||
"Ruff \U000026a1"
|
||||
|
||||
|
||||
def make_unique_pod_id(pod_id: str) -> str | None:
|
||||
r"""
|
||||
Generate a unique Pod name.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use memchr::memchr_iter;
|
||||
|
||||
use ruff_diagnostics::{Diagnostic, Violation};
|
||||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
|
@ -46,18 +46,21 @@ use crate::docstrings::Docstring;
|
|||
#[violation]
|
||||
pub struct EscapeSequenceInDocstring;
|
||||
|
||||
impl Violation for EscapeSequenceInDocstring {
|
||||
impl AlwaysFixableViolation for EscapeSequenceInDocstring {
|
||||
#[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"#)
|
||||
}
|
||||
}
|
||||
|
||||
/// D301
|
||||
pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
|
||||
// Docstring is already raw.
|
||||
let contents = docstring.contents;
|
||||
if contents.starts_with('r') || contents.starts_with("ur") {
|
||||
if docstring.leading_quote().contains(['r', 'R']) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -67,11 +70,15 @@ pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
|
|||
if memchr_iter(b'\\', bytes).any(|position| {
|
||||
let escaped_char = bytes.get(position.saturating_add(1));
|
||||
// Allow continuations (backslashes followed by newlines) and Unicode escapes.
|
||||
!matches!(escaped_char, Some(b'\r' | b'\n' | b'u' | b'N'))
|
||||
!matches!(escaped_char, Some(b'\r' | b'\n' | b'u' | b'U' | b'N'))
|
||||
}) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
EscapeSequenceInDocstring,
|
||||
let mut diagnostic = Diagnostic::new(EscapeSequenceInDocstring, docstring.range());
|
||||
|
||||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
|
||||
"r".to_owned() + docstring.contents,
|
||||
docstring.range(),
|
||||
));
|
||||
)));
|
||||
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,23 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
|
||||
---
|
||||
D.py:328:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
326 | @expect('D301: Use r""" if any backslashes in a docstring')
|
||||
327 | def single_quotes_raw_uppercase_backslash():
|
||||
328 | R'Sum\mary.'
|
||||
| ^^^^^^^^^^^^ D301
|
||||
|
|
||||
|
||||
D.py:333:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
D.py:333:5: D301 [*] Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
331 | @expect('D301: Use r""" if any backslashes in a docstring')
|
||||
332 | def double_quotes_backslash():
|
||||
333 | """Sum\\mary."""
|
||||
| ^^^^^^^^^^^^^^^^ D301
|
||||
|
|
||||
= help: Add `r` prefix
|
||||
|
||||
D.py:338:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
336 | @expect('D301: Use r""" if any backslashes in a docstring')
|
||||
337 | def double_quotes_backslash_uppercase():
|
||||
338 | R"""Sum\\mary."""
|
||||
| ^^^^^^^^^^^^^^^^^ D301
|
||||
|
|
||||
ℹ Suggested fix
|
||||
330 330 |
|
||||
331 331 | @expect('D301: Use r""" if any backslashes in a docstring')
|
||||
332 332 | def double_quotes_backslash():
|
||||
333 |- """Sum\\mary."""
|
||||
333 |+ r"""Sum\\mary."""
|
||||
334 334 |
|
||||
335 335 |
|
||||
336 336 | @expect('D301: Use r""" if any backslashes in a docstring')
|
||||
|
||||
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
|
||||
---
|
||||
D301.py:2:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
D301.py:2:5: D301 [*] Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
1 | def double_quotes_backslash():
|
||||
2 | """Sum\\mary."""
|
||||
| ^^^^^^^^^^^^^^^^ D301
|
||||
|
|
||||
= help: Add `r` prefix
|
||||
|
||||
D301.py:10:5: D301 Use `r"""` if any backslashes in a docstring
|
||||
|
|
||||
9 | def double_quotes_backslash_uppercase():
|
||||
10 | R"""Sum\\mary."""
|
||||
| ^^^^^^^^^^^^^^^^^ D301
|
||||
|
|
||||
ℹ Suggested fix
|
||||
1 1 | def double_quotes_backslash():
|
||||
2 |- """Sum\\mary."""
|
||||
2 |+ r"""Sum\\mary."""
|
||||
3 3 |
|
||||
4 4 |
|
||||
5 5 | def double_quotes_backslash_raw():
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue