Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid trimming escaped whitespace in D210 #3635

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ruff/src/rules/pydocstyle/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ pub(crate) fn should_ignore_definition(
false
}

/// Return true if a line ends with an odd number of backslashes (i.e., ends with an escape).
pub(crate) fn ends_with_backslash(line: &str) -> bool {
line.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
}

/// Check if a docstring should be ignored.
pub(crate) fn should_ignore_docstring(contents: &str) -> bool {
// Avoid analyzing docstrings that contain implicit string concatenations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring;
use crate::message::Location;
use crate::registry::AsRule;
use crate::rules::pydocstyle::helpers::ends_with_backslash;

#[violation]
pub struct SurroundingWhitespace;
Expand Down Expand Up @@ -46,6 +47,7 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) {
// characters, avoid applying the fix.
if !trimmed.ends_with(pattern.chars().last().unwrap())
&& !trimmed.starts_with(pattern.chars().last().unwrap())
&& !ends_with_backslash(trimmed)
{
diagnostic.amend(Fix::replacement(
trimmed.to_string(),
Expand Down