1
- use crate :: utils:: { get_trait_def_id, implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint} ;
1
+ use crate :: utils:: {
2
+ get_trait_def_id, implements_trait, is_entrypoint_fn, match_type, paths, return_ty, snippet_opt, span_lint,
3
+ } ;
4
+ use bytecount:: count;
2
5
use if_chain:: if_chain;
3
6
use itertools:: Itertools ;
4
7
use rustc:: lint:: in_external_macro;
@@ -357,9 +360,9 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
357
360
358
361
match ( previous. 0 , current. 0 ) {
359
362
( Text ( previous) , Text ( current) ) => {
360
- let mut previous = previous. to_string ( ) ;
361
- previous . push_str ( & current ) ;
362
- Ok ( ( Text ( previous . into ( ) ) , previous_range ) )
363
+ let text = ( previous. to_string ( ) + & current ) . into ( ) ;
364
+ let range = previous_range . start ..current_range . end ;
365
+ Ok ( ( Text ( text ) , range ) )
363
366
} ,
364
367
( previous, current) => Err ( ( ( previous, previous_range) , ( current, current_range) ) ) ,
365
368
}
@@ -413,6 +416,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
413
416
} ;
414
417
let ( begin, span) = spans[ index] ;
415
418
if in_code {
419
+ let lo = span. lo ( ) + BytePos :: from_usize ( range. start - begin) ;
420
+ let span = Span :: new (
421
+ lo,
422
+ lo + BytePos :: from_usize ( text. len ( ) + count ( text. as_bytes ( ) , b'\n' ) * 4 ) ,
423
+ span. ctxt ( ) ,
424
+ ) ;
416
425
check_code ( cx, & text, span) ;
417
426
} else {
418
427
// Adjust for the beginning of the current `Event`
@@ -429,8 +438,14 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
429
438
static LEAVE_MAIN_PATTERNS : & [ & str ] = & [ "static" , "fn main() {}" , "extern crate" , "async fn main() {" ] ;
430
439
431
440
fn check_code ( cx : & LateContext < ' _ , ' _ > , text : & str , span : Span ) {
432
- if text. contains ( "fn main() {" ) && !LEAVE_MAIN_PATTERNS . iter ( ) . any ( |p| text. contains ( p) ) {
433
- span_lint ( cx, NEEDLESS_DOCTEST_MAIN , span, "needless `fn main` in doctest" ) ;
441
+ if let Some ( comment) = snippet_opt ( cx, span) {
442
+ if let Some ( offset) = comment. find ( "fn main() {" ) {
443
+ if !LEAVE_MAIN_PATTERNS . iter ( ) . any ( |p| text. contains ( p) ) {
444
+ let lo = span. lo ( ) + BytePos :: from_usize ( offset) ;
445
+ let span = Span :: new ( lo, lo + BytePos :: from_usize ( "fn main()" . len ( ) ) , span. ctxt ( ) ) ;
446
+ span_lint ( cx, NEEDLESS_DOCTEST_MAIN , span, "needless `fn main` in doctest" ) ;
447
+ }
448
+ }
434
449
}
435
450
}
436
451
0 commit comments