lint: port drop trait/glue diagnostics

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-06-27 17:11:53 +01:00
parent 8e836566f0
commit 7ef610c003
3 changed files with 20 additions and 13 deletions

View File

@ -141,3 +141,9 @@ lint-redundant-semicolons =
[true] these semicolons [true] these semicolons
*[false] this semicolon *[false] this semicolon
} }
lint-drop-trait-constraints =
bounds on `{$predicate}` are most likely incorrect, consider instead using `{$needs_drop}` to detect whether a type can be trivially dropped
lint-drop-glue =
types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped

View File

@ -1,6 +1,7 @@
use crate::LateContext; use crate::LateContext;
use crate::LateLintPass; use crate::LateLintPass;
use crate::LintContext; use crate::LintContext;
use rustc_errors::fluent;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@ -103,13 +104,10 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
return return
}; };
let msg = format!( lint.build(fluent::lint::drop_trait_constraints)
"bounds on `{}` are most likely incorrect, consider instead \ .set_arg("predicate", predicate)
using `{}` to detect whether a type can be trivially dropped", .set_arg("needs_drop", cx.tcx.def_path_str(needs_drop))
predicate, .emit();
cx.tcx.def_path_str(needs_drop)
);
lint.build(&msg).emit();
}); });
} }
} }
@ -126,12 +124,9 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
return return
}; };
let msg = format!( lint.build(fluent::lint::drop_glue)
"types that do not implement `Drop` can still have drop glue, consider \ .set_arg("needs_drop", cx.tcx.def_path_str(needs_drop))
instead using `{}` to detect whether a type is trivially dropped", .emit();
cx.tcx.def_path_str(needs_drop)
);
lint.build(&msg).emit();
}); });
} }
} }

View File

@ -611,6 +611,12 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
} }
} }
impl rustc_errors::IntoDiagnosticArg for Predicate<'_> {
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
rustc_errors::DiagnosticArgValue::Str(std::borrow::Cow::Owned(self.to_string()))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable)] #[derive(HashStable, TypeFoldable)]
pub enum PredicateKind<'tcx> { pub enum PredicateKind<'tcx> {