Skip to content

Commit 53e5fd6

Browse files
committed
Make MissingDebugImplementation a module lint.
1 parent 6c7054e commit 53e5fd6

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

compiler/rustc_lint/src/builtin.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub,
4040
BuiltinWhileTrue, SuggestChangingAssocTypes,
4141
},
42-
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
42+
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
4343
};
4444
use hir::IsAsync;
4545
use rustc_ast::attr;
@@ -51,7 +51,7 @@ use rustc_errors::{Applicability, DecorateLint, MultiSpan};
5151
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
5252
use rustc_hir as hir;
5353
use rustc_hir::def::{DefKind, Res};
54-
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
54+
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
5555
use rustc_hir::intravisit::FnKind as HirFnKind;
5656
use rustc_hir::{Body, FnDecl, GenericParamKind, Node, PatKind, PredicateOrigin};
5757
use rustc_middle::lint::in_external_macro;
@@ -774,9 +774,7 @@ declare_lint! {
774774
}
775775

776776
#[derive(Default)]
777-
pub struct MissingDebugImplementations {
778-
impling_types: Option<LocalDefIdSet>,
779-
}
777+
pub(crate) struct MissingDebugImplementations;
780778

781779
impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
782780

@@ -793,21 +791,18 @@ impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
793791

794792
let Some(debug) = cx.tcx.get_diagnostic_item(sym::Debug) else { return };
795793

796-
if self.impling_types.is_none() {
797-
let mut impls = LocalDefIdSet::default();
798-
cx.tcx.for_each_impl(debug, |d| {
799-
if let Some(ty_def) = cx.tcx.type_of(d).instantiate_identity().ty_adt_def() {
800-
if let Some(def_id) = ty_def.did().as_local() {
801-
impls.insert(def_id);
802-
}
803-
}
804-
});
805-
806-
self.impling_types = Some(impls);
807-
debug!("{:?}", self.impling_types);
794+
// Avoid listing trait impls if the trait is allowed.
795+
let (level, _) = cx.tcx.lint_level_at_node(MISSING_DEBUG_IMPLEMENTATIONS, item.hir_id());
796+
if level == Level::Allow {
797+
return;
808798
}
809799

810-
if !self.impling_types.as_ref().unwrap().contains(&item.owner_id.def_id) {
800+
let has_impl = cx
801+
.tcx
802+
.non_blanket_impls_for_ty(debug, cx.tcx.type_of(item.owner_id).instantiate_identity())
803+
.next()
804+
.is_some();
805+
if !has_impl {
811806
cx.emit_spanned_lint(
812807
MISSING_DEBUG_IMPLEMENTATIONS,
813808
item.span,

compiler/rustc_lint/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ late_lint_methods!(
193193
[
194194
// Tracks attributes of parents
195195
MissingDoc: MissingDoc::new(),
196-
// Builds a global list of all impls of `Debug`.
197-
// FIXME: Turn the computation of types which implement Debug into a query
198-
// and change this to a module lint pass
199-
MissingDebugImplementations: MissingDebugImplementations::default(),
200196
]
201197
]
202198
);
@@ -253,6 +249,7 @@ late_lint_methods!(
253249
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
254250
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
255251
MapUnitFn: MapUnitFn,
252+
MissingDebugImplementations: MissingDebugImplementations,
256253
]
257254
]
258255
);

tests/ui/lint/issue-111359.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
error: type does not implement `Debug`; consider adding `#[derive(Debug)]` or a manual implementation
1+
error: type could implement `Copy`; consider adding `impl Copy`
22
--> $DIR/issue-111359.rs:7:5
33
|
44
LL | pub struct BarPub;
55
| ^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/issue-111359.rs:1:8
8+
--> $DIR/issue-111359.rs:2:8
99
|
10-
LL | #[deny(missing_debug_implementations)]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | #[deny(missing_copy_implementations)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: type could implement `Copy`; consider adding `impl Copy`
13+
error: type does not implement `Debug`; consider adding `#[derive(Debug)]` or a manual implementation
1414
--> $DIR/issue-111359.rs:7:5
1515
|
1616
LL | pub struct BarPub;
1717
| ^^^^^^^^^^^^^^^^^^
1818
|
1919
note: the lint level is defined here
20-
--> $DIR/issue-111359.rs:2:8
20+
--> $DIR/issue-111359.rs:1:8
2121
|
22-
LL | #[deny(missing_copy_implementations)]
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
LL | #[deny(missing_debug_implementations)]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

2525
error: aborting due to 2 previous errors
2626

tests/ui/missing_debug_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ struct PrivateStruct;
3535
enum PrivateEnum {}
3636

3737
#[derive(Debug)]
38-
struct GenericType<T>(T);
38+
pub struct GenericType<T>(T);

0 commit comments

Comments
 (0)