@@ -8,7 +8,7 @@ use rustc_errors::{Applicability, Diag, SuggestionStyle};
8
8
use rustc_hir:: { ItemKind , Node } ;
9
9
use rustc_lexer:: TokenKind ;
10
10
use rustc_lint:: LateContext ;
11
- use rustc_span:: { ExpnKind , InnerSpan , Span , SpanData } ;
11
+ use rustc_span:: { BytePos , ExpnKind , InnerSpan , Span , SpanData } ;
12
12
13
13
use super :: { EMPTY_LINE_AFTER_DOC_COMMENTS , EMPTY_LINE_AFTER_OUTER_ATTR } ;
14
14
@@ -144,6 +144,19 @@ impl<'a> Gap<'a> {
144
144
prev_chunk,
145
145
} )
146
146
}
147
+
148
+ fn contiguous_empty_lines ( & self ) -> impl Iterator < Item = Span > + ' _ {
149
+ self . empty_lines
150
+ // The `+ BytePos(1)` means "next line", because each empty line span is "N:1-N:1".
151
+ . chunk_by ( |a, b| a. hi ( ) + BytePos ( 1 ) == b. lo ( ) )
152
+ . map ( |chunk| {
153
+ let first = chunk. first ( ) . expect ( "at least one empty line" ) ;
154
+ let last = chunk. last ( ) . expect ( "at least one empty line" ) ;
155
+ // The BytePos subtraction here is safe, as before an empty line, there must be at least one
156
+ // attribute/comment. The span needs to start at the end of the previous line.
157
+ first. with_lo ( first. lo ( ) - BytePos ( 1 ) ) . with_hi ( last. hi ( ) )
158
+ } )
159
+ }
147
160
}
148
161
149
162
/// If the node the attributes/docs apply to is the first in the module/crate suggest converting
@@ -192,6 +205,7 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool {
192
205
return false ;
193
206
} ;
194
207
let empty_lines = || gaps. iter ( ) . flat_map ( |gap| gap. empty_lines . iter ( ) . copied ( ) ) ;
208
+ let contiguous_empty_lines = || gaps. iter ( ) . flat_map ( Gap :: contiguous_empty_lines) ;
195
209
let mut has_comment = false ;
196
210
let mut has_attr = false ;
197
211
for gap in gaps {
@@ -227,7 +241,9 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool {
227
241
228
242
diag. multipart_suggestion_with_style (
229
243
format ! ( "if the empty {lines} {are} unintentional remove {them}" ) ,
230
- empty_lines ( ) . map ( |empty_line| ( empty_line, String :: new ( ) ) ) . collect ( ) ,
244
+ contiguous_empty_lines ( )
245
+ . map ( |empty_lines| ( empty_lines, String :: new ( ) ) )
246
+ . collect ( ) ,
231
247
Applicability :: MaybeIncorrect ,
232
248
SuggestionStyle :: HideCodeAlways ,
233
249
) ;
0 commit comments