From 5b6fb8cefaa4b3e3ec9f5b145725afae003bfaff Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 11 Oct 2022 12:54:30 -0400 Subject: [PATCH] Skip docstring checks for empty docstrings (#402) --- src/check_ast.rs | 6 +-- src/docstrings.rs | 8 +++- src/snapshots/ruff__linter__tests__d200.snap | 2 +- src/snapshots/ruff__linter__tests__d205.snap | 4 +- src/snapshots/ruff__linter__tests__d300.snap | 18 +++------ src/snapshots/ruff__linter__tests__d400.snap | 40 ++++++++------------ src/snapshots/ruff__linter__tests__d415.snap | 38 ++++++++----------- src/snapshots/ruff__linter__tests__d419.snap | 6 +-- 8 files changed, 51 insertions(+), 71 deletions(-) diff --git a/src/check_ast.rs b/src/check_ast.rs index 283b2de762..95564cf3df 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -1890,6 +1890,9 @@ impl<'a> Checker<'a> { fn check_docstrings(&mut self) { while let Some(docstring) = self.docstrings.pop() { + if !docstrings::not_empty(self, &docstring) { + continue; + } if self.settings.enabled.contains(&CheckCode::D200) { docstrings::one_liner(self, &docstring); } @@ -1919,9 +1922,6 @@ impl<'a> Checker<'a> { if self.settings.enabled.contains(&CheckCode::D415) { docstrings::ends_with_punctuation(self, &docstring); } - if self.settings.enabled.contains(&CheckCode::D419) { - docstrings::not_empty(self, &docstring); - } } } diff --git a/src/docstrings.rs b/src/docstrings.rs index 7341588a9d..96a02c25ec 100644 --- a/src/docstrings.rs +++ b/src/docstrings.rs @@ -308,14 +308,18 @@ pub fn ends_with_punctuation(checker: &mut Checker, docstring: &Docstring) { } /// D419 -pub fn not_empty(checker: &mut Checker, docstring: &Docstring) { +pub fn not_empty(checker: &mut Checker, docstring: &Docstring) -> bool { if let ExprKind::Constant { value: Constant::Str(string), .. } = &docstring.expr.node { if string.trim().is_empty() { - checker.add_check(Check::new(CheckKind::NonEmpty, range_for(docstring))); + if checker.settings.enabled.contains(&CheckCode::D419) { + checker.add_check(Check::new(CheckKind::NonEmpty, range_for(docstring))); + } + return false; } } + true } diff --git a/src/snapshots/ruff__linter__tests__d200.snap b/src/snapshots/ruff__linter__tests__d200.snap index bc4feeb2f6..2a22f0278f 100644 --- a/src/snapshots/ruff__linter__tests__d200.snap +++ b/src/snapshots/ruff__linter__tests__d200.snap @@ -2,7 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: OneLinerDocstring +- kind: FitsOnOneLine location: row: 124 column: 5 diff --git a/src/snapshots/ruff__linter__tests__d205.snap b/src/snapshots/ruff__linter__tests__d205.snap index a08d1cd71e..2ece8e858b 100644 --- a/src/snapshots/ruff__linter__tests__d205.snap +++ b/src/snapshots/ruff__linter__tests__d205.snap @@ -2,7 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: BlankLineAfterSummary +- kind: NoBlankLineAfterSummary location: row: 195 column: 5 @@ -10,7 +10,7 @@ expression: checks row: 198 column: 8 fix: ~ -- kind: BlankLineAfterSummary +- kind: NoBlankLineAfterSummary location: row: 205 column: 5 diff --git a/src/snapshots/ruff__linter__tests__d300.snap b/src/snapshots/ruff__linter__tests__d300.snap index cedc1fd8d4..30f0ab5f75 100644 --- a/src/snapshots/ruff__linter__tests__d300.snap +++ b/src/snapshots/ruff__linter__tests__d300.snap @@ -2,15 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: TripleQuotes - location: - row: 75 - column: 9 - end_location: - row: 75 - column: 11 - fix: ~ -- kind: TripleQuotes +- kind: UsesTripleQuotes location: row: 302 column: 6 @@ -18,7 +10,7 @@ expression: checks row: 302 column: 20 fix: ~ -- kind: TripleQuotes +- kind: UsesTripleQuotes location: row: 307 column: 6 @@ -26,7 +18,7 @@ expression: checks row: 307 column: 20 fix: ~ -- kind: TripleQuotes +- kind: UsesTripleQuotes location: row: 312 column: 6 @@ -34,7 +26,7 @@ expression: checks row: 312 column: 16 fix: ~ -- kind: TripleQuotes +- kind: UsesTripleQuotes location: row: 317 column: 6 @@ -42,7 +34,7 @@ expression: checks row: 317 column: 16 fix: ~ -- kind: TripleQuotes +- kind: UsesTripleQuotes location: row: 323 column: 6 diff --git a/src/snapshots/ruff__linter__tests__d400.snap b/src/snapshots/ruff__linter__tests__d400.snap index e62dcc20d1..4bd959b6f2 100644 --- a/src/snapshots/ruff__linter__tests__d400.snap +++ b/src/snapshots/ruff__linter__tests__d400.snap @@ -2,15 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: DocstringEndsInNonPeriod - location: - row: 69 - column: 5 - end_location: - row: 69 - column: 12 - fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 124 column: 5 @@ -18,7 +10,7 @@ expression: checks row: 126 column: 8 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 283 column: 5 @@ -26,7 +18,7 @@ expression: checks row: 283 column: 34 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 288 column: 5 @@ -34,7 +26,7 @@ expression: checks row: 288 column: 38 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 350 column: 5 @@ -42,7 +34,7 @@ expression: checks row: 350 column: 18 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 401 column: 25 @@ -50,7 +42,7 @@ expression: checks row: 401 column: 40 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 405 column: 5 @@ -58,7 +50,7 @@ expression: checks row: 405 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 411 column: 5 @@ -66,7 +58,7 @@ expression: checks row: 411 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 417 column: 35 @@ -74,7 +66,7 @@ expression: checks row: 417 column: 50 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 424 column: 49 @@ -82,7 +74,7 @@ expression: checks row: 424 column: 64 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 465 column: 5 @@ -90,7 +82,7 @@ expression: checks row: 465 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 470 column: 5 @@ -98,7 +90,7 @@ expression: checks row: 470 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 475 column: 5 @@ -106,7 +98,7 @@ expression: checks row: 475 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 482 column: 5 @@ -114,7 +106,7 @@ expression: checks row: 482 column: 25 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 504 column: 5 @@ -122,7 +114,7 @@ expression: checks row: 504 column: 35 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 509 column: 5 @@ -130,7 +122,7 @@ expression: checks row: 509 column: 34 fix: ~ -- kind: DocstringEndsInNonPeriod +- kind: EndsInPeriod location: row: 515 column: 5 diff --git a/src/snapshots/ruff__linter__tests__d415.snap b/src/snapshots/ruff__linter__tests__d415.snap index db141df312..b703fc93a4 100644 --- a/src/snapshots/ruff__linter__tests__d415.snap +++ b/src/snapshots/ruff__linter__tests__d415.snap @@ -2,15 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: DocstringEndsInNonPunctuation - location: - row: 69 - column: 5 - end_location: - row: 69 - column: 12 - fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 124 column: 5 @@ -18,7 +10,7 @@ expression: checks row: 126 column: 8 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 283 column: 5 @@ -26,7 +18,7 @@ expression: checks row: 283 column: 34 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 288 column: 5 @@ -34,7 +26,7 @@ expression: checks row: 288 column: 38 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 350 column: 5 @@ -42,7 +34,7 @@ expression: checks row: 350 column: 18 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 401 column: 25 @@ -50,7 +42,7 @@ expression: checks row: 401 column: 40 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 405 column: 5 @@ -58,7 +50,7 @@ expression: checks row: 405 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 411 column: 5 @@ -66,7 +58,7 @@ expression: checks row: 411 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 417 column: 35 @@ -74,7 +66,7 @@ expression: checks row: 417 column: 50 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 424 column: 49 @@ -82,7 +74,7 @@ expression: checks row: 424 column: 64 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 465 column: 5 @@ -90,7 +82,7 @@ expression: checks row: 465 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 470 column: 5 @@ -98,7 +90,7 @@ expression: checks row: 470 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 475 column: 5 @@ -106,7 +98,7 @@ expression: checks row: 475 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 482 column: 5 @@ -114,7 +106,7 @@ expression: checks row: 482 column: 25 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 504 column: 5 @@ -122,7 +114,7 @@ expression: checks row: 504 column: 35 fix: ~ -- kind: DocstringEndsInNonPunctuation +- kind: EndsInPunctuation location: row: 515 column: 5 diff --git a/src/snapshots/ruff__linter__tests__d419.snap b/src/snapshots/ruff__linter__tests__d419.snap index b236d8a8b7..c45ed7fa6c 100644 --- a/src/snapshots/ruff__linter__tests__d419.snap +++ b/src/snapshots/ruff__linter__tests__d419.snap @@ -2,7 +2,7 @@ source: src/linter.rs expression: checks --- -- kind: EmptyDocstring +- kind: NonEmpty location: row: 19 column: 9 @@ -10,7 +10,7 @@ expression: checks row: 19 column: 15 fix: ~ -- kind: EmptyDocstring +- kind: NonEmpty location: row: 69 column: 5 @@ -18,7 +18,7 @@ expression: checks row: 69 column: 12 fix: ~ -- kind: EmptyDocstring +- kind: NonEmpty location: row: 75 column: 9