From a2da04cd1c42175022ea9aed7c2ea44751a21ee0 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Sun, 5 Nov 2023 15:50:19 +0530 Subject: [PATCH] Avoid `D301` autofix for `u` prefixed strings --- .../test/fixtures/pydocstyle/D301.py | 4 ++++ .../src/rules/pydocstyle/rules/backslashes.rs | 20 +++++++++++-------- ...ules__pydocstyle__tests__D301_D301.py.snap | 8 ++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D301.py b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D301.py index 6af8362ad8dcf..5950a0ce5374b 100644 --- a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D301.py +++ b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D301.py @@ -31,3 +31,7 @@ def make_unique_pod_id(pod_id: str) -> str | None: :param pod_id: requested pod name :return: ``str`` valid Pod name of appropriate length """ + + +def shouldnt_add_raw_here2(): + u"Sum\\mary." diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/backslashes.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/backslashes.rs index 0c02b457a642f..97403748ce4c5 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/backslashes.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/backslashes.rs @@ -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 { + 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); } diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D301_D301.py.snap b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D301_D301.py.snap index f64cbcd59ad57..9289200eea535 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D301_D301.py.snap +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D301_D301.py.snap @@ -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 +