Skip to content

Commit f1c47ab

Browse files
committed
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.
1 parent abdf173 commit f1c47ab

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

clippy_lints/src/doc/empty_line_after.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{Applicability, Diag, SuggestionStyle};
88
use rustc_hir::{ItemKind, Node};
99
use rustc_lexer::TokenKind;
1010
use rustc_lint::LateContext;
11-
use rustc_span::{ExpnKind, InnerSpan, Span, SpanData};
11+
use rustc_span::{BytePos, ExpnKind, InnerSpan, Span, SpanData};
1212

1313
use super::{EMPTY_LINE_AFTER_DOC_COMMENTS, EMPTY_LINE_AFTER_OUTER_ATTR};
1414

@@ -192,6 +192,16 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool {
192192
return false;
193193
};
194194
let empty_lines = || gaps.iter().flat_map(|gap| gap.empty_lines.iter().copied());
195+
let contiguous_empty_lines = || {
196+
gaps.iter().map(|gap| {
197+
let first_line = gap.empty_lines.first().unwrap();
198+
let last_line = gap.empty_lines.last().unwrap();
199+
200+
// The BytePos subtraction here is save, as before an empty line, there must be at least one
201+
// attribute/doc comment.
202+
last_line.with_lo(first_line.lo() - BytePos(1))
203+
})
204+
};
195205
let mut has_comment = false;
196206
let mut has_attr = false;
197207
for gap in gaps {
@@ -230,7 +240,9 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool {
230240

231241
diag.multipart_suggestion_with_style(
232242
format!("if the empty {lines} {are} unintentional remove {them}"),
233-
empty_lines().map(|empty_line| (empty_line, String::new())).collect(),
243+
contiguous_empty_lines()
244+
.map(|empty_lines| (empty_lines, String::new()))
245+
.collect(),
234246
Applicability::MaybeIncorrect,
235247
SuggestionStyle::HideCodeAlways,
236248
);

0 commit comments

Comments
 (0)