Avoid trimming docstring if ends in trailing quote (#2025)

Closes #2017.
This commit is contained in:
Charlie Marsh 2023-01-20 07:41:58 -05:00 committed by GitHub
parent 6072edf5bf
commit 4af2353ef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 21 deletions

View file

@ -606,3 +606,12 @@ def one_liner():
r"""Wrong.
"""
@expect('D200: One-line docstring should fit on one line with quotes '
'(found 3)')
@expect('D212: Multi-line docstring summary should start at the first line')
def one_liner():
"""Wrong."
"""

View file

@ -30,10 +30,9 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) {
);
if checker.patch(diagnostic.kind.rule()) {
if let Some(pattern) = leading_quote(contents) {
if let Some(quote) = pattern.chars().last() {
// If removing whitespace would lead to an invalid string of quote
// characters, avoid applying the fix.
if !trimmed.ends_with(quote) {
if !trimmed.ends_with(pattern.chars().last().unwrap()) {
diagnostic.amend(Fix::replacement(
trimmed.to_string(),
Location::new(
@ -48,6 +47,5 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) {
}
}
}
}
checker.diagnostics.push(diagnostic);
}

View file

@ -31,13 +31,18 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) {
helpers::leading_quote(docstring.contents),
helpers::trailing_quote(docstring.contents),
) {
// If removing whitespace would lead to an invalid string of quote
// characters, avoid applying the fix.
let trimmed = docstring.body.trim();
if !trimmed.ends_with(trailing.chars().last().unwrap()) {
diagnostic.amend(Fix::replacement(
format!("{leading}{}{trailing}", docstring.body.trim()),
format!("{leading}{trimmed}{trailing}"),
docstring.expr.location,
docstring.expr.end_location.unwrap(),
));
}
}
}
checker.diagnostics.push(diagnostic);
}
}

View file

@ -53,4 +53,14 @@ expression: diagnostics
row: 608
column: 7
parent: ~
- kind:
FitsOnOneLine: ~
location:
row: 615
column: 4
end_location:
row: 617
column: 7
fix: ~
parent: ~

View file

@ -212,4 +212,14 @@ expression: diagnostics
column: 7
fix: ~
parent: ~
- kind:
MultiLineSummarySecondLine: ~
location:
row: 615
column: 4
end_location:
row: 617
column: 7
fix: ~
parent: ~

View file

@ -240,4 +240,21 @@ expression: diagnostics
row: 581
column: 47
parent: ~
- kind:
EndsInPeriod: ~
location:
row: 615
column: 4
end_location:
row: 617
column: 7
fix:
content: "."
location:
row: 615
column: 14
end_location:
row: 615
column: 14
parent: ~

View file

@ -223,4 +223,21 @@ expression: diagnostics
row: 581
column: 47
parent: ~
- kind:
EndsInPunctuation: ~
location:
row: 615
column: 4
end_location:
row: 617
column: 7
fix:
content: "."
location:
row: 615
column: 14
end_location:
row: 615
column: 14
parent: ~