diff --git a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py index 62105959cf259..83954cc1f6703 100644 --- a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py +++ b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py @@ -41,3 +41,13 @@ def first_word_lots_of_whitespace(): What do you think? """ + +def single_word_then_newline(): + """singleword + + """ + +def single_word_on_second_line(): + """ + singleword + """ diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs index 0c6e7a3154d85..3bec13ba98b40 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs @@ -60,14 +60,10 @@ pub(crate) fn capitalized(checker: &mut Checker, docstring: &Docstring) { let body = docstring.body(); let trim_start_body = body.trim_start(); - let first_word = trim_start_body.split_once(' ').map_or_else( - || { - // If the docstring is a single word, trim the punctuation marks because - // it makes the ASCII test below fail. - body.trim_end_matches(['.', '!', '?']) - }, - |(first_word, _)| first_word, - ); + let first_word = trim_start_body + .find(char::is_whitespace) + .map_or(trim_start_body, |idx| &trim_start_body[..idx]) + .trim_end_matches(['.', '!', '?']); let mut first_word_chars = first_word.chars(); let Some(first_char) = first_word_chars.next() else { diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D403_D403.py.snap b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D403_D403.py.snap index 86fbe97384caf..3424025756818 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D403_D403.py.snap +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D403_D403.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/pydocstyle/mod.rs +snapshot_kind: text --- D403.py:2:5: D403 [*] First word of the docstring should be capitalized: `this` -> `This` | @@ -72,6 +73,8 @@ D403.py:36:5: D403 [*] First word of the docstring should be capitalized: `here` 42 | | What do you think? 43 | | """ | |_______^ D403 +44 | +45 | def single_word_then_newline(): | = help: Capitalize `here` to `Here` @@ -84,3 +87,45 @@ D403.py:36:5: D403 [*] First word of the docstring should be capitalized: `here` 41 41 | 42 42 | What do you think? 43 43 | """ + +D403.py:46:5: D403 [*] First word of the docstring should be capitalized: `singleword` -> `Singleword` + | +45 | def single_word_then_newline(): +46 | """singleword + | _____^ +47 | | +48 | | """ + | |_______^ D403 +49 | +50 | def single_word_on_second_line(): + | + = help: Capitalize `singleword` to `Singleword` + +ℹ Safe fix +43 43 | """ +44 44 | +45 45 | def single_word_then_newline(): +46 |- """singleword + 46 |+ """Singleword +47 47 | +48 48 | """ +49 49 | + +D403.py:51:5: D403 [*] First word of the docstring should be capitalized: `singleword` -> `Singleword` + | +50 | def single_word_on_second_line(): +51 | """ + | _____^ +52 | | singleword +53 | | """ + | |_______^ D403 + | + = help: Capitalize `singleword` to `Singleword` + +ℹ Safe fix +49 49 | +50 50 | def single_word_on_second_line(): +51 51 | """ +52 |- singleword + 52 |+ Singleword +53 53 | """