Skip to main content

rustc_hir_analysis/
check_unused.rs

1use rustc_data_structures::unord::{ExtendUnord, UnordSet};
2use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level};
3use rustc_hir::def::DefKind;
4use rustc_hir::def_id::LocalDefId;
5use rustc_middle::ty::TyCtxt;
6use rustc_session::lint;
7use rustc_span::Span;
8use tracing::debug;
9
10struct UnusedImport<'tcx> {
11    tcx: TyCtxt<'tcx>,
12    span: Span,
13}
14
15impl<'a, 'tcx> Diagnostic<'a, ()> for UnusedImport<'tcx> {
16    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
17        let Self { tcx, span } = self;
18        if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) {
19            Diag::new(dcx, level, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unused import: `{0}`", snippet))
    })format!("unused import: `{snippet}`"))
20        } else {
21            Diag::new(dcx, level, "unused import")
22        }
23    }
24}
25
26pub(super) fn check_unused_traits(tcx: TyCtxt<'_>, (): ()) {
27    let mut used_trait_imports = UnordSet::<LocalDefId>::default();
28
29    // FIXME: Use `tcx.hir_par_body_owners()` when we implement creating `DefId`s
30    // for anon constants during their parents' typeck.
31    // Doing so at current will produce queries cycle errors because it may typeck
32    // on anon constants directly.
33    for item_def_id in tcx.hir_body_owners() {
34        let imports = tcx.used_trait_imports(item_def_id);
35        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/check_unused.rs:35",
                        "rustc_hir_analysis::check_unused", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/check_unused.rs"),
                        ::tracing_core::__macro_support::Option::Some(35u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::check_unused"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("GatherVisitor: item_def_id={0:?} with imports {1:#?}",
                                                    item_def_id, imports) as &dyn Value))])
            });
    } else { ; }
};debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports);
36        used_trait_imports.extend_unord(imports.items().copied());
37    }
38
39    for &id in tcx.resolutions(()).maybe_unused_trait_imports.iter() {
40        if true {
    match (&tcx.def_kind(id), &DefKind::Use) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(tcx.def_kind(id), DefKind::Use);
41        if tcx.visibility(id).is_public() {
42            continue;
43        }
44        if used_trait_imports.contains(&id) {
45            continue;
46        }
47        let item = tcx.hir_expect_item(id);
48        if item.span.is_dummy() {
49            continue;
50        }
51        let (path, _) = item.expect_use();
52        tcx.emit_node_span_lint(
53            lint::builtin::UNUSED_IMPORTS,
54            item.hir_id(),
55            path.span,
56            UnusedImport { tcx, span: path.span },
57        );
58    }
59}