diff options
| author | Rejyr <jerrylwang123@gmail.com> | 2022-11-24 20:03:06 -0500 |
|---|---|---|
| committer | Rejyr <jerrylwang123@gmail.com> | 2023-01-09 18:57:02 -0500 |
| commit | fe09291036ca0d4c97d67f592442d09a1c45abd0 (patch) | |
| tree | e0f9144d913157099a92c572e8767397eabeaa3c /compiler/rustc_lint/src | |
| parent | f38db48dbd534af0ef67c39299a393e7d1fe49e9 (diff) | |
| download | rust-fe09291036ca0d4c97d67f592442d09a1c45abd0.tar.gz rust-fe09291036ca0d4c97d67f592442d09a1c45abd0.zip | |
migrate: `deref_into_dyn_supertrait.rs`
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/deref_into_dyn_supertrait.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/lints.rs | 18 |
2 files changed, 30 insertions, 18 deletions
diff --git a/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs b/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs index 1d29a234a3c..dff5a645c17 100644 --- a/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs +++ b/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs @@ -1,6 +1,8 @@ -use crate::{LateContext, LateLintPass, LintContext}; +use crate::{ + lints::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel}, + LateContext, LateLintPass, LintContext, +}; -use rustc_errors::DelayDm; use rustc_hir as hir; use rustc_middle::{traits::util::supertraits, ty}; use rustc_span::sym; @@ -71,22 +73,14 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait { && supertraits(cx.tcx, t_principal.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self)) .any(|sup| sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(cx.tcx, x)) == target_principal) { - cx.struct_span_lint( - DEREF_INTO_DYN_SUPERTRAIT, - cx.tcx.def_span(item.owner_id.def_id), - DelayDm(|| { - format!( - "`{t}` implements `Deref` with supertrait `{target_principal}` as target" - ) - }), - |lint| { - if let Some(target_span) = impl_.items.iter().find_map(|i| (i.ident.name == sym::Target).then_some(i.span)) { - lint.span_label(target_span, "target type is set here"); - } - - lint - }, - ) + let label = impl_.items.iter().find_map(|i| (i.ident.name == sym::Target).then_some(i.span)).map(|label| SupertraitAsDerefTargetLabel { + label, + }); + cx.emit_spanned_lint(DEREF_INTO_DYN_SUPERTRAIT, cx.tcx.def_span(item.owner_id.def_id), SupertraitAsDerefTarget { + t, + target_principal: target_principal.to_string(), + label, + }); } } } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index a98d5cec979..c3782a49689 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -551,6 +551,24 @@ pub struct BuiltinUnexpectedCliConfigValue { pub value: Symbol, } +// deref_into_dyn_supertrait.rs +#[derive(LintDiagnostic)] +#[diag(lint_supertrait_as_deref_target)] +pub struct SupertraitAsDerefTarget<'a> { + pub t: Ty<'a>, + pub target_principal: String, + // pub target_principal: Binder<'a, ExistentialTraitRef<'b>>, + #[subdiagnostic] + pub label: Option<SupertraitAsDerefTargetLabel>, +} + +#[derive(Subdiagnostic)] +#[label(label)] +pub struct SupertraitAsDerefTargetLabel { + #[primary_span] + pub label: Span, +} + // enum_intrinsics_non_enums.rs #[derive(LintDiagnostic)] #[diag(lint_enum_intrinsics_mem_discriminant)] |
