Make MissingDebugImplementation a module lint.

This commit is contained in:
Camille GILLOT 2023-07-15 12:15:55 +00:00
parent 6c7054e962
commit 53e5fd6a61
4 changed files with 27 additions and 35 deletions

View file

@ -39,7 +39,7 @@
BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub,
BuiltinWhileTrue, SuggestChangingAssocTypes,
},
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
};
use hir::IsAsync;
use rustc_ast::attr;
@ -51,7 +51,7 @@
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::intravisit::FnKind as HirFnKind;
use rustc_hir::{Body, FnDecl, GenericParamKind, Node, PatKind, PredicateOrigin};
use rustc_middle::lint::in_external_macro;
@ -774,9 +774,7 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
}
#[derive(Default)]
pub struct MissingDebugImplementations {
impling_types: Option<LocalDefIdSet>,
}
pub(crate) struct MissingDebugImplementations;
impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
@ -793,21 +791,18 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
let Some(debug) = cx.tcx.get_diagnostic_item(sym::Debug) else { return };
if self.impling_types.is_none() {
let mut impls = LocalDefIdSet::default();
cx.tcx.for_each_impl(debug, |d| {
if let Some(ty_def) = cx.tcx.type_of(d).instantiate_identity().ty_adt_def() {
if let Some(def_id) = ty_def.did().as_local() {
impls.insert(def_id);
}
}
});
self.impling_types = Some(impls);
debug!("{:?}", self.impling_types);
// Avoid listing trait impls if the trait is allowed.
let (level, _) = cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
if level == Level::Allow {
return;
}
if !self.impling_types.as_ref().unwrap().contains(&item.owner_id.def_id) {
let has_impl = cx
.tcx
.non_blanket_impls_for_ty(debug, cx.tcx.type_of(item.owner_id).instantiate_identity())
.next()
.is_some();
if !has_impl {
cx.emit_spanned_lint(
MISSING_DEBUG_IMPLEMENTATIONS,
item.span,

View file

@ -193,10 +193,6 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
[
// Tracks attributes of parents
MissingDoc: MissingDoc::new(),
// Builds a global list of all impls of `Debug`.
// FIXME: Turn the computation of types which implement Debug into a query
// and change this to a module lint pass
MissingDebugImplementations: MissingDebugImplementations::default(),
]
]
);
@ -253,6 +249,7 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
MapUnitFn: MapUnitFn,
MissingDebugImplementations: MissingDebugImplementations,
]
]
);

View file

@ -1,15 +1,3 @@
error: type does not implement `Debug`; consider adding `#[derive(Debug)]` or a manual implementation
--> $DIR/issue-111359.rs:7:5
|
LL | pub struct BarPub;
| ^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/issue-111359.rs:1:8
|
LL | #[deny(missing_debug_implementations)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: type could implement `Copy`; consider adding `impl Copy`
--> $DIR/issue-111359.rs:7:5
|
@ -22,5 +10,17 @@ note: the lint level is defined here
LL | #[deny(missing_copy_implementations)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: type does not implement `Debug`; consider adding `#[derive(Debug)]` or a manual implementation
--> $DIR/issue-111359.rs:7:5
|
LL | pub struct BarPub;
| ^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/issue-111359.rs:1:8
|
LL | #[deny(missing_debug_implementations)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -35,4 +35,4 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
enum PrivateEnum {}
#[derive(Debug)]
struct GenericType<T>(T);
pub struct GenericType<T>(T);