@@ -313,12 +313,28 @@ fn crate_name(ast_func: &ast::Fn, ctx: &AssistContext<'_>) -> Option<String> {
313313/// `None` if function without a body; some bool to guess if function can panic
314314fn can_panic ( ast_func : & ast:: Fn ) -> Option < bool > {
315315 let body = ast_func. body ( ) ?. to_string ( ) ;
316- let can_panic = body. contains ( "panic!(" )
317- // FIXME it would be better to not match `debug_assert*!` macro invocations
318- || body. contains ( "assert!(" )
319- || body. contains ( ".unwrap()" )
320- || body. contains ( ".expect(" ) ;
321- Some ( can_panic)
316+ let mut iter = body. chars ( ) ;
317+ let assert_postfix = |s| {
318+ [ "!(" , "_eq!(" , "_ne!(" , "_matches!(" ] . iter ( ) . any ( |postfix| str:: starts_with ( s, postfix) )
319+ } ;
320+
321+ while !iter. as_str ( ) . is_empty ( ) {
322+ let s = iter. as_str ( ) ;
323+ iter. next ( ) ;
324+ if s. strip_prefix ( "debug_assert" ) . is_some_and ( assert_postfix) {
325+ iter. nth ( 10 ) ;
326+ continue ;
327+ }
328+ if s. strip_prefix ( "assert" ) . is_some_and ( assert_postfix)
329+ || s. starts_with ( "panic!(" )
330+ || s. starts_with ( ".unwrap()" )
331+ || s. starts_with ( ".expect(" )
332+ {
333+ return Some ( true ) ;
334+ }
335+ }
336+
337+ Some ( false )
322338}
323339
324340/// Helper function to get the name that should be given to `self` arguments
@@ -677,6 +693,24 @@ pub fn panics_if(a: bool) {
677693 ) ;
678694 }
679695
696+ #[ test]
697+ fn guesses_debug_assert_macro_cannot_panic ( ) {
698+ check_assist (
699+ generate_documentation_template,
700+ r#"
701+ pub fn $0debug_panics_if_not(a: bool) {
702+ debug_assert!(a == true);
703+ }
704+ "# ,
705+ r#"
706+ /// .
707+ pub fn debug_panics_if_not(a: bool) {
708+ debug_assert!(a == true);
709+ }
710+ "# ,
711+ ) ;
712+ }
713+
680714 #[ test]
681715 fn guesses_assert_macro_can_panic ( ) {
682716 check_assist (
@@ -699,6 +733,28 @@ pub fn panics_if_not(a: bool) {
699733 ) ;
700734 }
701735
736+ #[ test]
737+ fn guesses_assert_eq_macro_can_panic ( ) {
738+ check_assist (
739+ generate_documentation_template,
740+ r#"
741+ pub fn $0panics_if_not(a: bool) {
742+ assert_eq!(a, true);
743+ }
744+ "# ,
745+ r#"
746+ /// .
747+ ///
748+ /// # Panics
749+ ///
750+ /// Panics if .
751+ pub fn panics_if_not(a: bool) {
752+ assert_eq!(a, true);
753+ }
754+ "# ,
755+ ) ;
756+ }
757+
702758 #[ test]
703759 fn guesses_unwrap_can_panic ( ) {
704760 check_assist (
0 commit comments