|
2 | 2 | //! through the body using inference results: mismatched arg counts, missing |
3 | 3 | //! fields, etc. |
4 | 4 |
|
| 5 | +use std::fmt; |
5 | 6 | use std::sync::Arc; |
6 | 7 |
|
7 | 8 | use hir_def::{path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId, HasModule}; |
@@ -378,32 +379,34 @@ fn missing_match_arms<'p>( |
378 | 379 | witnesses: Vec<DeconstructedPat<'p>>, |
379 | 380 | arms: &[MatchArm], |
380 | 381 | ) -> String { |
| 382 | + struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>); |
| 383 | + impl<'a, 'p> fmt::Display for DisplayWitness<'a, 'p> { |
| 384 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 385 | + let DisplayWitness(witness, cx) = *self; |
| 386 | + let pat = witness.to_pat(cx); |
| 387 | + write!(f, "{}", pat.display(cx.db)) |
| 388 | + } |
| 389 | + } |
| 390 | + |
381 | 391 | let non_empty_enum = match scrut_ty.as_adt() { |
382 | 392 | Some((AdtId::EnumId(e), _)) => !cx.db.enum_data(e).variants.is_empty(), |
383 | 393 | _ => false, |
384 | 394 | }; |
385 | 395 | if arms.is_empty() && !non_empty_enum { |
386 | 396 | format!("type `{}` is non-empty", scrut_ty.display(cx.db)) |
387 | 397 | } else { |
| 398 | + let pat_display = |witness| DisplayWitness(witness, cx); |
388 | 399 | const LIMIT: usize = 3; |
389 | 400 | match &*witnesses { |
390 | | - [witness] => format!("`{}` not covered", witness.to_pat(&cx).display(cx.db)), |
| 401 | + [witness] => format!("`{}` not covered", pat_display(witness)), |
391 | 402 | [head @ .., tail] if head.len() < LIMIT => { |
392 | | - let head: Vec<_> = head.iter().map(|w| w.to_pat(cx)).collect(); |
393 | | - format!( |
394 | | - "`{}` and `{}` not covered", |
395 | | - head.iter().map(|p| p.display(cx.db)).join("`, `"), |
396 | | - tail.to_pat(&cx).display(cx.db) |
397 | | - ) |
| 403 | + let head = head.iter().map(pat_display); |
| 404 | + format!("`{}` and `{}` not covered", head.format("`, `"), pat_display(tail)) |
398 | 405 | } |
399 | 406 | _ => { |
400 | 407 | let (head, tail) = witnesses.split_at(LIMIT); |
401 | | - let head: Vec<_> = head.iter().map(|w| w.to_pat(cx)).collect(); |
402 | | - format!( |
403 | | - "`{}` and {} more not covered", |
404 | | - head.iter().map(|p| p.display(cx.db)).join("`, `"), |
405 | | - tail.len() |
406 | | - ) |
| 408 | + let head = head.iter().map(pat_display); |
| 409 | + format!("`{}` and {} more not covered", head.format("`, `"), tail.len()) |
407 | 410 | } |
408 | 411 | } |
409 | 412 | } |
|
0 commit comments