From 595dc26bfd709ca0e247f765081fb6b55ea9a9bb Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sun, 22 Sep 2024 18:33:50 +0200 Subject: [PATCH] Use contiguous spans for empty_line_after_* suggestion Replacing an empty span (which an empty line is) with an empty string triggers a debug assertion in rustc. This fixes the debug assertion by using contiguous spans, with the same resulting suggestion. --- clippy_lints/src/doc/empty_line_after.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/doc/empty_line_after.rs b/clippy_lints/src/doc/empty_line_after.rs index 289debe0a6a9..cfb8b7c63206 100644 --- a/clippy_lints/src/doc/empty_line_after.rs +++ b/clippy_lints/src/doc/empty_line_after.rs @@ -8,7 +8,7 @@ use rustc_errors::{Applicability, Diag, SuggestionStyle}; use rustc_hir::{ItemKind, Node}; use rustc_lexer::TokenKind; use rustc_lint::LateContext; -use rustc_span::{ExpnKind, InnerSpan, Span, SpanData}; +use rustc_span::{BytePos, ExpnKind, InnerSpan, Span, SpanData}; use super::{EMPTY_LINE_AFTER_DOC_COMMENTS, EMPTY_LINE_AFTER_OUTER_ATTR}; @@ -192,6 +192,16 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool { return false; }; let empty_lines = || gaps.iter().flat_map(|gap| gap.empty_lines.iter().copied()); + let contiguous_empty_lines = || { + gaps.iter().map(|gap| { + let first_line = gap.empty_lines.first().unwrap(); + let last_line = gap.empty_lines.last().unwrap(); + + // The BytePos subtraction here is safe, as before an empty line, there must be at least one + // attribute/doc comment. + last_line.with_lo(first_line.lo() - BytePos(1)) + }) + }; let mut has_comment = false; let mut has_attr = false; for gap in gaps { @@ -230,7 +240,9 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool { diag.multipart_suggestion_with_style( format!("if the empty {lines} {are} unintentional remove {them}"), - empty_lines().map(|empty_line| (empty_line, String::new())).collect(), + contiguous_empty_lines() + .map(|empty_lines| (empty_lines, String::new())) + .collect(), Applicability::MaybeIncorrect, SuggestionStyle::HideCodeAlways, );