Skip to content

Commit

Permalink
Make D410/D411 autofixes mutually exclusive (#4110)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Apr 28, 2023
1 parent ee6d8f7 commit b34804c
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions crates/ruff/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,12 @@ fn blanks_and_section_underline(
}
}

fn common_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
fn common_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next: Option<&SectionContext>,
) {
if checker.settings.rules.enabled(Rule::CapitalizeSectionName) {
let capitalized_section_name = context.kind().as_str();
if context.section_name() != capitalized_section_name {
Expand Down Expand Up @@ -626,43 +631,42 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio
let line_end = checker.stylist.line_ending().as_str();
let last_line = context.following_lines().last();
if last_line.map_or(true, |line| !line.trim().is_empty()) {
if context.is_last() {
if let Some(next) = next {
if checker
.settings
.rules
.enabled(Rule::BlankLineAfterLastSection)
.enabled(Rule::NoBlankLineAfterSection)
{
let mut diagnostic = Diagnostic::new(
BlankLineAfterLastSection {
NoBlankLineAfterSection {
name: context.section_name().to_string(),
},
docstring.range(),
);
if checker.patch(diagnostic.kind.rule()) {
// Add a newline after the section.
diagnostic.set_fix(Edit::insertion(
format!("{}{}", line_end, docstring.indentation),
context.range().end(),
));
// Add a newline at the beginning of the next section.
diagnostic.set_fix(Edit::insertion(line_end.to_string(), next.range().start()));
}
checker.diagnostics.push(diagnostic);
}
} else {
if checker
.settings
.rules
.enabled(Rule::NoBlankLineAfterSection)
.enabled(Rule::BlankLineAfterLastSection)
{
let mut diagnostic = Diagnostic::new(
NoBlankLineAfterSection {
BlankLineAfterLastSection {
name: context.section_name().to_string(),
},
docstring.range(),
);
if checker.patch(diagnostic.kind.rule()) {
// Add a newline after the section.
diagnostic
.set_fix(Edit::insertion(line_end.to_string(), context.range().end()));
diagnostic.set_fix(Edit::insertion(
format!("{}{}", line_end, docstring.indentation),
context.range().end(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -861,8 +865,13 @@ fn parameters_section(checker: &mut Checker, docstring: &Docstring, context: &Se
missing_args(checker, docstring, &docstring_args);
}

fn numpy_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
common_section(checker, docstring, context);
fn numpy_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next: Option<&SectionContext>,
) {
common_section(checker, docstring, context, next);

if checker
.settings
Expand Down Expand Up @@ -897,8 +906,13 @@ fn numpy_section(checker: &mut Checker, docstring: &Docstring, context: &Section
}
}

fn google_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
common_section(checker, docstring, context);
fn google_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next: Option<&SectionContext>,
) {
common_section(checker, docstring, context, next);

if checker.settings.rules.enabled(Rule::SectionNameEndsInColon) {
let suffix = context.summary_after_section_name();
Expand Down Expand Up @@ -927,8 +941,9 @@ fn parse_numpy_sections(
docstring: &Docstring,
section_contexts: &SectionContexts,
) {
for section_context in section_contexts {
numpy_section(checker, docstring, &section_context);
let mut iterator = section_contexts.iter().peekable();
while let Some(context) = iterator.next() {
numpy_section(checker, docstring, &context, iterator.peek());
}
}

Expand All @@ -937,8 +952,9 @@ fn parse_google_sections(
docstring: &Docstring,
section_contexts: &SectionContexts,
) {
for section_context in section_contexts {
google_section(checker, docstring, &section_context);
let mut iterator = section_contexts.iter().peekable();
while let Some(context) = iterator.next() {
google_section(checker, docstring, &context, iterator.peek());
}

if checker.settings.rules.enabled(Rule::UndocumentedParam) {
Expand Down

0 comments on commit b34804c

Please sign in to comment.