Skip to content

Commit 4165fe2

Browse files
committed
Migrate 'is_empty' diagnostics
1 parent 77c78f1 commit 4165fe2

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 6 additions & 0 deletions
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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,26 +1068,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10681068
if let Some((deref_ty, _)) = derefed {
10691069
// Give a note about what the expr derefs to.
10701070
if deref_ty != self.expr_ty.peel_refs() {
1071-
err.span_note(
1072-
self.expr_span,
1073-
format!(
1074-
"this expression `Deref`s to `{}` which implements `is_empty`",
1075-
fcx.ty_to_string(deref_ty)
1076-
),
1077-
);
1071+
err.subdiagnostic(errors::DerefImplsIsEmpty {
1072+
span: self.expr_span,
1073+
deref_ty: fcx.ty_to_string(deref_ty),
1074+
});
10781075
}
10791076

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

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 22 additions & 0 deletions
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)