Skip to content

Commit 38209ff

Browse files
committed
Migrate 'is_empty' diagnostics
1 parent ab1b47f commit 38209ff

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

compiler/rustc_hir_typeck/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ hir_typeck_convert_using_method = try using `{$sugg}` to convert `{$found}` to `
2929
3030
hir_typeck_ctor_is_private = tuple struct constructor `{$def}` is private
3131
32+
hir_typeck_deref_is_empty = this expression `Deref`s to `{$deref_ty}` which implements `is_empty`
33+
3234
hir_typeck_expected_default_return_type = expected `()` because of default return type
3335
3436
hir_typeck_expected_return_type = expected `{$expected}` because of return type
@@ -114,5 +116,9 @@ hir_typeck_suggest_ptr_null_mut = consider using `core::ptr::null_mut` instead
114116
hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
115117
116118
hir_typeck_union_pat_multiple_fields = union patterns should have exactly one field
119+
120+
hir_typeck_use_is_empty =
121+
consider using the `is_empty` method on `{$expr_ty}` to determine if it contains anything
122+
117123
hir_typeck_yield_expr_outside_of_generator =
118124
yield expression outside of generator literal

compiler/rustc_hir_typeck/src/cast.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1066,26 +1066,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10661066
if let Some((deref_ty, _)) = derefed {
10671067
// Give a note about what the expr derefs to.
10681068
if deref_ty != self.expr_ty.peel_refs() {
1069-
err.span_note(
1070-
self.expr_span,
1071-
format!(
1072-
"this expression `Deref`s to `{}` which implements `is_empty`",
1073-
fcx.ty_to_string(deref_ty)
1074-
),
1075-
);
1069+
err.subdiagnostic(errors::DerefImplsIsEmpty {
1070+
span: self.expr_span,
1071+
deref_ty: fcx.ty_to_string(deref_ty),
1072+
});
10761073
}
10771074

10781075
// Create a multipart suggestion: add `!` and `.is_empty()` in
10791076
// place of the cast.
1080-
let suggestion = vec![
1081-
(self.expr_span.shrink_to_lo(), "!".to_string()),
1082-
(self.span.with_lo(self.expr_span.hi()), ".is_empty()".to_string()),
1083-
];
1084-
1085-
err.multipart_suggestion_verbose(format!(
1086-
"consider using the `is_empty` method on `{}` to determine if it contains anything",
1087-
fcx.ty_to_string(self.expr_ty),
1088-
), suggestion, Applicability::MaybeIncorrect);
1077+
err.subdiagnostic(errors::UseIsEmpty {
1078+
lo: self.expr_span.shrink_to_lo(),
1079+
hi: self.span.with_lo(self.expr_span.hi()),
1080+
expr_ty: fcx.ty_to_string(self.expr_ty),
1081+
});
10891082
}
10901083
}
10911084
}

compiler/rustc_hir_typeck/src/errors.rs

+22
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,20 @@ pub struct UnionPatDotDot {
413413
pub span: Span,
414414
}
415415

416+
#[derive(Subdiagnostic)]
417+
#[multipart_suggestion(
418+
hir_typeck_use_is_empty,
419+
applicability = "maybe-incorrect",
420+
style = "verbose"
421+
)]
422+
pub struct UseIsEmpty {
423+
#[suggestion_part(code = "!")]
424+
pub lo: Span,
425+
#[suggestion_part(code = ".is_empty()")]
426+
pub hi: Span,
427+
pub expr_ty: String,
428+
}
429+
416430
#[derive(Diagnostic)]
417431
#[diag(hir_typeck_arg_mismatch_indeterminate)]
418432
pub struct ArgMismatchIndeterminate {
@@ -489,6 +503,14 @@ pub struct CtorIsPrivate {
489503
pub def: String,
490504
}
491505

506+
#[derive(Subdiagnostic)]
507+
#[note(hir_typeck_deref_is_empty)]
508+
pub struct DerefImplsIsEmpty {
509+
#[primary_span]
510+
pub span: Span,
511+
pub deref_ty: String,
512+
}
513+
492514
#[derive(Subdiagnostic)]
493515
#[multipart_suggestion(
494516
hir_typeck_convert_using_method,

0 commit comments

Comments
 (0)