Generate string literal assertion with error collection String literals always use .as_ref() to handle String/&str matching
(value_expr: &TokenStream, pattern: &PatternString)
| 503 | /// Generate string literal assertion with error collection |
| 504 | /// String literals always use .as_ref() to handle String/&str matching |
| 505 | fn expand_string_assertion(value_expr: &TokenStream, pattern: &PatternString) -> TokenStream { |
| 506 | let lit = &pattern.lit; |
| 507 | |
| 508 | // String patterns always use .as_ref() to handle String/&str matching |
| 509 | let span = lit.span(); |
| 510 | let error_push = generate_error_push( |
| 511 | span, |
| 512 | quote!(format!("{:?}", actual)), |
| 513 | quote!(None), |
| 514 | pattern.node_id, |
| 515 | ); |
| 516 | |
| 517 | quote_spanned! {span=> { |
| 518 | // Take a reference to the expression result so that: |
| 519 | // 1. Temporaries (e.g. from method calls returning String) live for the |
| 520 | // entire block - fixes E0716 "temporary dropped while borrowed". |
| 521 | // 2. Reference-typed expressions (e.g. from index operations) are not |
| 522 | // moved - fixes E0507 "cannot move out of shared reference". |
| 523 | let __assert_struct_tmp = &#value_expr; |
| 524 | let actual = (*__assert_struct_tmp).as_ref(); |
| 525 | if !matches!(actual, #lit) { |
| 526 | #error_push |
| 527 | } |
| 528 | }} |
| 529 | } |
| 530 | |
| 531 | /// Generate simple assertion with error collection |
| 532 | fn expand_simple_assertion(actual: &TokenStream, pattern: &PatternSimple) -> TokenStream { |
no test coverage detected