From 1a22eae98c005b4da36dde13fff0f991cba11e1d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 23 Sep 2023 18:44:55 -0400 Subject: [PATCH] Use deletion for D215 full-line removals (#7625) Closes https://github.com/astral-sh/ruff/issues/7619. --- .../test/fixtures/pydocstyle/D215.py | 4 ++++ .../ruff_linter/src/rules/pydocstyle/mod.rs | 1 + .../src/rules/pydocstyle/rules/sections.rs | 13 ++++++------ ...ules__pydocstyle__tests__D215_D215.py.snap | 21 +++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/pydocstyle/D215.py create mode 100644 crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D215_D215.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D215.py b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D215.py new file mode 100644 index 0000000000000..f45f933ee327f --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D215.py @@ -0,0 +1,4 @@ +""" +TODO: + - +""" diff --git a/crates/ruff_linter/src/rules/pydocstyle/mod.rs b/crates/ruff_linter/src/rules/pydocstyle/mod.rs index dae662eb99e07..e3e51c00c6387 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/mod.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/mod.rs @@ -79,6 +79,7 @@ mod tests { #[test_case(Rule::SectionNameEndsInColon, Path::new("D.py"))] #[test_case(Rule::SectionNotOverIndented, Path::new("sections.py"))] #[test_case(Rule::SectionNotOverIndented, Path::new("D214_module.py"))] + #[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("D215.py"))] #[test_case(Rule::SectionUnderlineAfterName, Path::new("sections.py"))] #[test_case(Rule::SectionUnderlineMatchesSectionLength, Path::new("sections.py"))] #[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"))] diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs index 34f78655c89ca..c075ecacace09 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs @@ -1440,16 +1440,17 @@ fn blanks_and_section_underline( docstring.range(), ); if checker.patch(diagnostic.kind.rule()) { + // Replace the existing indentation with whitespace of the appropriate length. let range = TextRange::at( blank_lines_end, leading_space.text_len() + TextSize::from(1), ); - - // Replace the existing indentation with whitespace of the appropriate length. - diagnostic.set_fix(Fix::automatic(Edit::range_replacement( - clean_space(docstring.indentation), - range, - ))); + let contents = clean_space(docstring.indentation); + diagnostic.set_fix(Fix::automatic(if contents.is_empty() { + Edit::range_deletion(range) + } else { + Edit::range_replacement(contents, range) + })); }; checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D215_D215.py.snap b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D215_D215.py.snap new file mode 100644 index 0000000000000..9ac9a84da7f30 --- /dev/null +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D215_D215.py.snap @@ -0,0 +1,21 @@ +--- +source: crates/ruff_linter/src/rules/pydocstyle/mod.rs +--- +D215.py:1:1: D215 [*] Section underline is over-indented ("TODO") + | +1 | / """ +2 | | TODO: +3 | | - +4 | | """ + | |___^ D215 + | + = help: Remove over-indentation from "TODO" underline + +ℹ Fix +1 1 | """ +2 2 | TODO: +3 |- - + 3 |+ +4 4 | """ + +