@@ -346,34 +346,41 @@ impl Expect {
346346
347347 let literal_start = literal_start + ( lit_to_eof. len ( ) - lit_to_eof_trimmed. len ( ) ) ;
348348
349- let literal_len =
350- locate_end ( lit_to_eof_trimmed) . expect ( "Couldn't find closing delimiter for `expect!`." ) ;
349+ let literal_len = locate_end ( lit_to_eof_trimmed) ;
351350 let literal_range = literal_start..literal_start + literal_len;
352351 Location { line_indent, literal_range }
353352 }
354353}
355354
356355/// Returns the byte index of the closing delimiter.
357356///
358- /// `arg_start_to_eof` is the part after `expect![`, with leading whitespaces trimmed.
359- fn locate_end ( arg_start_to_eof : & str ) -> Option < usize > {
360- match arg_start_to_eof. chars ( ) . next ( ) ? {
357+ /// `arg_start_to_eof` is the part after `expect![` until the closing delimiter
358+ /// with leading whitespaces trimmed.
359+ /// Note that we can actually assume the syntax is valid according to `macro_rules`.
360+ fn locate_end ( arg_start_to_eof : & str ) -> usize {
361+ let mut chars = arg_start_to_eof. chars ( ) ;
362+ match chars. next ( ) . expect ( "after `expect![` there should be a char" ) {
361363 c if c. is_whitespace ( ) => panic ! ( "skip whitespace before calling `locate_end`" ) ,
362364
363365 // expect![[]]
364366 '[' => {
365367 let str_start_to_eof = arg_start_to_eof[ 1 ..] . trim_start ( ) ;
366- let str_len = find_str_lit_len ( str_start_to_eof) . unwrap_or ( 0 ) ;
368+ let str_len = if ']' == chars. next ( ) . expect ( "after `expect![[` there should be a char" )
369+ {
370+ 0
371+ } else {
372+ find_str_lit_len ( str_start_to_eof) . expect ( "invalid string literal in `expect![[`" )
373+ } ;
367374 let str_end_to_eof = & str_start_to_eof[ str_len..] ;
368- let closing_brace_offset = str_end_to_eof. find ( ']' ) ? ;
369- Some ( ( arg_start_to_eof. len ( ) - str_end_to_eof. len ( ) ) + closing_brace_offset + 1 )
375+ let closing_brace_offset = str_end_to_eof. find ( ']' ) . unwrap ( ) ;
376+ ( arg_start_to_eof. len ( ) - str_end_to_eof. len ( ) ) + closing_brace_offset + 1
370377 }
371378
372379 // expect![] | expect!{} | expect!()
373- ']' | '}' | ')' => Some ( 0 ) ,
380+ ']' | '}' | ')' => 0 ,
374381
375382 // expect!["..."] | expect![r#"..."#]
376- _ => find_str_lit_len ( arg_start_to_eof) ,
383+ _ => find_str_lit_len ( arg_start_to_eof) . expect ( "invalid string literal after `expect![`" ) ,
377384 }
378385}
379386
@@ -843,7 +850,7 @@ line1
843850 let lit = stringify!( $s) ;
844851 let with_trailer = format!( "[{} \t ]]\n " , lit) ;
845852 // ^ ^^ ^^ 5 additional chars
846- assert_eq!( locate_end( & with_trailer) , Some ( 4 +lit. len( ) ) ) ;
853+ assert_eq!( locate_end( & with_trailer) , 4 +lit. len( ) ) ;
847854 } ) * } ;
848855 }
849856
@@ -856,9 +863,14 @@ line1
856863 ) ;
857864
858865 // Check `expect![[ ]]`
859- assert_eq ! ( locate_end( "[]]" ) , Some ( 2 ) ) ;
866+ assert_eq ! ( locate_end( "[]]" ) , 2 ) ;
860867 // Check `expect![ ]`
861- assert_eq ! ( locate_end( "]" ) , Some ( 0 ) ) ;
868+ assert_eq ! ( locate_end( "]" ) , 0 ) ;
869+
870+ // `locate_end` returns after the closing delimiter.
871+ assert_eq ! ( locate_end( "]abc" ) , 0 ) ;
872+ // This is actually invalid syntax.
873+ assert_eq ! ( locate_end( "]]abc" ) , 0 ) ;
862874 }
863875
864876 #[ test]
0 commit comments