add autofix for D301 (#7970)

## Summary

Add fix for `D301`

## Test Plan

`cargo test` and manually
This commit is contained in:
Steve C 2023-10-17 22:19:29 -04:00 committed by GitHub
parent 195c000f5a
commit dda4ceda71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 31 deletions

View file

@ -10,6 +10,10 @@ def double_quotes_backslash_uppercase():
R"""Sum\\mary.""" R"""Sum\\mary."""
def shouldnt_add_raw_here():
"Ruff \U000026a1"
def make_unique_pod_id(pod_id: str) -> str | None: def make_unique_pod_id(pod_id: str) -> str | None:
r""" r"""
Generate a unique Pod name. Generate a unique Pod name.

View file

@ -1,6 +1,6 @@
use memchr::memchr_iter; 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_macros::{derive_message_formats, violation};
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
@ -46,18 +46,21 @@ use crate::docstrings::Docstring;
#[violation] #[violation]
pub struct EscapeSequenceInDocstring; pub struct EscapeSequenceInDocstring;
impl Violation for EscapeSequenceInDocstring { impl AlwaysFixableViolation for EscapeSequenceInDocstring {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!(r#"Use `r"""` if any backslashes in a docstring"#) format!(r#"Use `r"""` if any backslashes in a docstring"#)
} }
fn fix_title(&self) -> String {
format!(r#"Add `r` prefix"#)
}
} }
/// D301 /// D301
pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) { pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
// Docstring is already raw. // Docstring is already raw.
let contents = docstring.contents; if docstring.leading_quote().contains(['r', 'R']) {
if contents.starts_with('r') || contents.starts_with("ur") {
return; return;
} }
@ -67,11 +70,15 @@ pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
if memchr_iter(b'\\', bytes).any(|position| { if memchr_iter(b'\\', bytes).any(|position| {
let escaped_char = bytes.get(position.saturating_add(1)); let escaped_char = bytes.get(position.saturating_add(1));
// Allow continuations (backslashes followed by newlines) and Unicode escapes. // 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( let mut diagnostic = Diagnostic::new(EscapeSequenceInDocstring, docstring.range());
EscapeSequenceInDocstring,
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
"r".to_owned() + docstring.contents,
docstring.range(), docstring.range(),
)); )));
checker.diagnostics.push(diagnostic);
} }
} }

View file

@ -1,28 +1,23 @@
--- ---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
--- ---
D.py:328:5: D301 Use `r"""` if any backslashes in a docstring D.py:333: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
| |
331 | @expect('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(): 332 | def double_quotes_backslash():
333 | """Sum\\mary.""" 333 | """Sum\\mary."""
| ^^^^^^^^^^^^^^^^ D301 | ^^^^^^^^^^^^^^^^ D301
| |
= help: Add `r` prefix
D.py:338:5: D301 Use `r"""` if any backslashes in a docstring Suggested fix
| 330 330 |
336 | @expect('D301: Use r""" if any backslashes in a docstring') 331 331 | @expect('D301: Use r""" if any backslashes in a docstring')
337 | def double_quotes_backslash_uppercase(): 332 332 | def double_quotes_backslash():
338 | R"""Sum\\mary.""" 333 |- """Sum\\mary."""
| ^^^^^^^^^^^^^^^^^ D301 333 |+ r"""Sum\\mary."""
| 334 334 |
335 335 |
336 336 | @expect('D301: Use r""" if any backslashes in a docstring')

View file

@ -1,18 +1,20 @@
--- ---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs 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(): 1 | def double_quotes_backslash():
2 | """Sum\\mary.""" 2 | """Sum\\mary."""
| ^^^^^^^^^^^^^^^^ D301 | ^^^^^^^^^^^^^^^^ D301
| |
= help: Add `r` prefix
D301.py:10:5: D301 Use `r"""` if any backslashes in a docstring Suggested fix
| 1 1 | def double_quotes_backslash():
9 | def double_quotes_backslash_uppercase(): 2 |- """Sum\\mary."""
10 | R"""Sum\\mary.""" 2 |+ r"""Sum\\mary."""
| ^^^^^^^^^^^^^^^^^ D301 3 3 |
| 4 4 |
5 5 | def double_quotes_backslash_raw():