Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make D410/D411 autofixes mutually exclusive #4110

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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