1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//
// Unused import checking
//
// Although this is mostly a lint pass, it lives in here because it depends on
// resolve data structures and because it finalises the privacy information for
// `use` items.
//
// Unused trait imports can't be checked until the method resolution. We save
// candidates here, and do the actual check in rustc_hir_analysis/check_unused.rs.
//
// Checking for unused imports is split into three steps:
//
//  - `UnusedImportCheckVisitor` walks the AST to find all the unused imports
//    inside of `UseTree`s, recording their `NodeId`s and grouping them by
//    the parent `use` item
//
//  - `calc_unused_spans` then walks over all the `use` items marked in the
//    previous step to collect the spans associated with the `NodeId`s and to
//    calculate the spans that can be removed by rustfix; This is done in a
//    separate step to be able to collapse the adjacent spans that rustfix
//    will remove
//
//  - `check_unused` finally emits the diagnostics based on the data generated
//    in the last step

use rustc_ast as ast;
use rustc_ast::visit::{self, Visitor};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::MultiSpan;
use rustc_hir::def::{DefKind, Res};
use rustc_session::lint::builtin::{
    MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS, UNUSED_QUALIFICATIONS,
};
use rustc_session::lint::BuiltinLintDiag;
use rustc_span::symbol::{kw, Ident};
use rustc_span::{Span, DUMMY_SP};

use crate::imports::{Import, ImportKind};
use crate::{module_to_string, LexicalScopeBinding, NameBindingKind, Resolver};

struct UnusedImport {
    use_tree: ast::UseTree,
    use_tree_id: ast::NodeId,
    item_span: Span,
    unused: UnordSet<ast::NodeId>,
}

impl UnusedImport {
    fn add(&mut self, id: ast::NodeId) {
        self.unused.insert(id);
    }
}

struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
    r: &'a mut Resolver<'b, 'tcx>,
    /// All the (so far) unused imports, grouped path list
    unused_imports: FxIndexMap<ast::NodeId, UnusedImport>,
    extern_crate_items: Vec<ExternCrateToLint>,
    base_use_tree: Option<&'a ast::UseTree>,
    base_id: ast::NodeId,
    item_span: Span,
}

struct ExternCrateToLint {
    id: ast::NodeId,
    /// Span from the item
    span: Span,
    /// Span to use to suggest complete removal.
    span_with_attributes: Span,
    /// Span of the visibility, if any.
    vis_span: Span,
    /// Whether the item has attrs.
    has_attrs: bool,
    /// Name used to refer to the crate.
    ident: Ident,
    /// Whether the statement renames the crate `extern crate orig_name as new_name;`.
    renames: bool,
}

impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
    // We have information about whether `use` (import) items are actually
    // used now. If an import is not used at all, we signal a lint error.
    fn check_import(&mut self, id: ast::NodeId) {
        let used = self.r.used_imports.contains(&id);
        let def_id = self.r.local_def_id(id);
        if !used {
            if self.r.maybe_unused_trait_imports.contains(&def_id) {
                // Check later.
                return;
            }
            self.unused_import(self.base_id).add(id);
        } else {
            // This trait import is definitely used, in a way other than
            // method resolution.
            // FIXME(#120456) - is `swap_remove` correct?
            self.r.maybe_unused_trait_imports.swap_remove(&def_id);
            if let Some(i) = self.unused_imports.get_mut(&self.base_id) {
                i.unused.remove(&id);
            }
        }
    }

    fn unused_import(&mut self, id: ast::NodeId) -> &mut UnusedImport {
        let use_tree_id = self.base_id;
        let use_tree = self.base_use_tree.unwrap().clone();
        let item_span = self.item_span;

        self.unused_imports.entry(id).or_insert_with(|| UnusedImport {
            use_tree,
            use_tree_id,
            item_span,
            unused: Default::default(),
        })
    }

    fn check_import_as_underscore(&mut self, item: &ast::UseTree, id: ast::NodeId) {
        match item.kind {
            ast::UseTreeKind::Simple(Some(ident)) => {
                if ident.name == kw::Underscore
                    && !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
                        per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
                            matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
                        })
                    })
                {
                    self.unused_import(self.base_id).add(id);
                }
            }
            ast::UseTreeKind::Nested { ref items, .. } => self.check_imports_as_underscore(items),
            _ => {}
        }
    }

    fn check_imports_as_underscore(&mut self, items: &[(ast::UseTree, ast::NodeId)]) {
        for (item, id) in items {
            self.check_import_as_underscore(item, *id);
        }
    }

    fn report_unused_extern_crate_items(
        &mut self,
        maybe_unused_extern_crates: FxHashMap<ast::NodeId, Span>,
    ) {
        let tcx = self.r.tcx();
        for extern_crate in &self.extern_crate_items {
            let warn_if_unused = !extern_crate.ident.name.as_str().starts_with('_');

            // If the crate is fully unused, we suggest removing it altogether.
            // We do this in any edition.
            if warn_if_unused {
                if let Some(&span) = maybe_unused_extern_crates.get(&extern_crate.id) {
                    self.r.lint_buffer.buffer_lint(
                        UNUSED_EXTERN_CRATES,
                        extern_crate.id,
                        span,
                        BuiltinLintDiag::UnusedExternCrate {
                            removal_span: extern_crate.span_with_attributes,
                        },
                    );
                    continue;
                }
            }

            // If we are not in Rust 2018 edition, then we don't make any further
            // suggestions.
            if !tcx.sess.at_least_rust_2018() {
                continue;
            }

            // If the extern crate has any attributes, they may have funky
            // semantics we can't faithfully represent using `use` (most
            // notably `#[macro_use]`). Ignore it.
            if extern_crate.has_attrs {
                continue;
            }

            // If the extern crate is renamed, then we cannot suggest replacing it with a use as this
            // would not insert the new name into the prelude, where other imports in the crate may be
            // expecting it.
            if extern_crate.renames {
                continue;
            }

            // If the extern crate isn't in the extern prelude,
            // there is no way it can be written as a `use`.
            if !self
                .r
                .extern_prelude
                .get(&extern_crate.ident)
                .is_some_and(|entry| !entry.introduced_by_item)
            {
                continue;
            }

            let vis_span = extern_crate
                .vis_span
                .find_ancestor_inside(extern_crate.span)
                .unwrap_or(extern_crate.vis_span);
            let ident_span = extern_crate
                .ident
                .span
                .find_ancestor_inside(extern_crate.span)
                .unwrap_or(extern_crate.ident.span);
            self.r.lint_buffer.buffer_lint(
                UNUSED_EXTERN_CRATES,
                extern_crate.id,
                extern_crate.span,
                BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span },
            );
        }
    }
}

impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
    fn visit_item(&mut self, item: &'a ast::Item) {
        match item.kind {
            // Ignore is_public import statements because there's no way to be sure
            // whether they're used or not. Also ignore imports with a dummy span
            // because this means that they were generated in some fashion by the
            // compiler and we don't need to consider them.
            ast::ItemKind::Use(..) if item.span.is_dummy() => return,
            ast::ItemKind::ExternCrate(orig_name) => {
                self.extern_crate_items.push(ExternCrateToLint {
                    id: item.id,
                    span: item.span,
                    vis_span: item.vis.span,
                    span_with_attributes: item.span_with_attributes(),
                    has_attrs: !item.attrs.is_empty(),
                    ident: item.ident,
                    renames: orig_name.is_some(),
                });
            }
            _ => {}
        }

        self.item_span = item.span_with_attributes();
        visit::walk_item(self, item);
    }

    fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: ast::NodeId, nested: bool) {
        // Use the base UseTree's NodeId as the item id
        // This allows the grouping of all the lints in the same item
        if !nested {
            self.base_id = id;
            self.base_use_tree = Some(use_tree);
        }

        if self.r.effective_visibilities.is_exported(self.r.local_def_id(id)) {
            self.check_import_as_underscore(use_tree, id);
            return;
        }

        if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
            if items.is_empty() {
                self.unused_import(self.base_id).add(id);
            }
        } else {
            self.check_import(id);
        }

        visit::walk_use_tree(self, use_tree, id);
    }
}

enum UnusedSpanResult {
    Used,
    Unused { spans: Vec<Span>, remove: Span },
    PartialUnused { spans: Vec<Span>, remove: Vec<Span> },
}

fn calc_unused_spans(
    unused_import: &UnusedImport,
    use_tree: &ast::UseTree,
    use_tree_id: ast::NodeId,
) -> UnusedSpanResult {
    // The full span is the whole item's span if this current tree is not nested inside another
    // This tells rustfix to remove the whole item if all the imports are unused
    let full_span = if unused_import.use_tree.span == use_tree.span {
        unused_import.item_span
    } else {
        use_tree.span
    };
    match use_tree.kind {
        ast::UseTreeKind::Simple(..) | ast::UseTreeKind::Glob => {
            if unused_import.unused.contains(&use_tree_id) {
                UnusedSpanResult::Unused { spans: vec![use_tree.span], remove: full_span }
            } else {
                UnusedSpanResult::Used
            }
        }
        ast::UseTreeKind::Nested { items: ref nested, span: tree_span } => {
            if nested.is_empty() {
                return UnusedSpanResult::Unused { spans: vec![use_tree.span], remove: full_span };
            }

            let mut unused_spans = Vec::new();
            let mut to_remove = Vec::new();
            let mut used_children = 0;
            let mut contains_self = false;
            let mut previous_unused = false;
            for (pos, (use_tree, use_tree_id)) in nested.iter().enumerate() {
                let remove = match calc_unused_spans(unused_import, use_tree, *use_tree_id) {
                    UnusedSpanResult::Used => {
                        used_children += 1;
                        None
                    }
                    UnusedSpanResult::Unused { mut spans, remove } => {
                        unused_spans.append(&mut spans);
                        Some(remove)
                    }
                    UnusedSpanResult::PartialUnused { mut spans, remove: mut to_remove_extra } => {
                        used_children += 1;
                        unused_spans.append(&mut spans);
                        to_remove.append(&mut to_remove_extra);
                        None
                    }
                };
                if let Some(remove) = remove {
                    let remove_span = if nested.len() == 1 {
                        remove
                    } else if pos == nested.len() - 1 || used_children > 0 {
                        // Delete everything from the end of the last import, to delete the
                        // previous comma
                        nested[pos - 1].0.span.shrink_to_hi().to(use_tree.span)
                    } else {
                        // Delete everything until the next import, to delete the trailing commas
                        use_tree.span.to(nested[pos + 1].0.span.shrink_to_lo())
                    };

                    // Try to collapse adjacent spans into a single one. This prevents all cases of
                    // overlapping removals, which are not supported by rustfix
                    if previous_unused && !to_remove.is_empty() {
                        let previous = to_remove.pop().unwrap();
                        to_remove.push(previous.to(remove_span));
                    } else {
                        to_remove.push(remove_span);
                    }
                }
                contains_self |= use_tree.prefix == kw::SelfLower
                    && matches!(use_tree.kind, ast::UseTreeKind::Simple(None));
                previous_unused = remove.is_some();
            }
            if unused_spans.is_empty() {
                UnusedSpanResult::Used
            } else if used_children == 0 {
                UnusedSpanResult::Unused { spans: unused_spans, remove: full_span }
            } else {
                // If there is only one remaining child that is used, the braces around the use
                // tree are not needed anymore. In that case, we determine the span of the left
                // brace and the right brace, and tell rustfix to remove them as well.
                //
                // This means that `use a::{B, C};` will be turned into `use a::B;` rather than
                // `use a::{B};`, removing a rustfmt roundtrip.
                //
                // Note that we cannot remove the braces if the only item inside the use tree is
                // `self`: `use foo::{self};` is valid Rust syntax, while `use foo::self;` errors
                // out. We also cannot turn `use foo::{self}` into `use foo`, as the former doesn't
                // import types with the same name as the module.
                if used_children == 1 && !contains_self {
                    // Left brace, from the start of the nested group to the first item.
                    to_remove.push(
                        tree_span.shrink_to_lo().to(nested.first().unwrap().0.span.shrink_to_lo()),
                    );
                    // Right brace, from the end of the last item to the end of the nested group.
                    to_remove.push(
                        nested.last().unwrap().0.span.shrink_to_hi().to(tree_span.shrink_to_hi()),
                    );
                }

                UnusedSpanResult::PartialUnused { spans: unused_spans, remove: to_remove }
            }
        }
    }
}

impl Resolver<'_, '_> {
    pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
        let tcx = self.tcx;
        let mut maybe_unused_extern_crates = FxHashMap::default();

        for import in self.potentially_unused_imports.iter() {
            match import.kind {
                _ if import.vis.is_public()
                    || import.span.is_dummy()
                    || self.import_use_map.contains_key(import) =>
                {
                    if let ImportKind::MacroUse { .. } = import.kind {
                        if !import.span.is_dummy() {
                            self.lint_buffer.buffer_lint(
                                MACRO_USE_EXTERN_CRATE,
                                import.root_id,
                                import.span,
                                BuiltinLintDiag::MacroUseDeprecated,
                            );
                        }
                    }
                }
                ImportKind::ExternCrate { id, .. } => {
                    let def_id = self.local_def_id(id);
                    if self.extern_crate_map.get(&def_id).map_or(true, |&cnum| {
                        !tcx.is_compiler_builtins(cnum)
                            && !tcx.is_panic_runtime(cnum)
                            && !tcx.has_global_allocator(cnum)
                            && !tcx.has_panic_handler(cnum)
                    }) {
                        maybe_unused_extern_crates.insert(id, import.span);
                    }
                }
                ImportKind::MacroUse { .. } => {
                    self.lint_buffer.buffer_lint(
                        UNUSED_IMPORTS,
                        import.root_id,
                        import.span,
                        BuiltinLintDiag::UnusedMacroUse,
                    );
                }
                _ => {}
            }
        }

        let mut visitor = UnusedImportCheckVisitor {
            r: self,
            unused_imports: Default::default(),
            extern_crate_items: Default::default(),
            base_use_tree: None,
            base_id: ast::DUMMY_NODE_ID,
            item_span: DUMMY_SP,
        };
        visit::walk_crate(&mut visitor, krate);

        visitor.report_unused_extern_crate_items(maybe_unused_extern_crates);

        for unused in visitor.unused_imports.values() {
            let (spans, remove_spans) =
                match calc_unused_spans(unused, &unused.use_tree, unused.use_tree_id) {
                    UnusedSpanResult::Used => continue,
                    UnusedSpanResult::Unused { spans, remove } => (spans, vec![remove]),
                    UnusedSpanResult::PartialUnused { spans, remove } => (spans, remove),
                };

            let ms = MultiSpan::from_spans(spans);

            let mut span_snippets = ms
                .primary_spans()
                .iter()
                .filter_map(|span| tcx.sess.source_map().span_to_snippet(*span).ok())
                .map(|s| format!("`{s}`"))
                .collect::<Vec<String>>();
            span_snippets.sort();

            let remove_whole_use = remove_spans.len() == 1 && remove_spans[0] == unused.item_span;
            let num_to_remove = ms.primary_spans().len();

            // If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
            // attribute; however, if not, suggest adding the attribute. There is no way to
            // retrieve attributes here because we do not have a `TyCtxt` yet.
            let test_module_span = if tcx.sess.is_test_crate() {
                None
            } else {
                let parent_module = visitor.r.get_nearest_non_block_module(
                    visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
                );
                match module_to_string(parent_module) {
                    Some(module)
                        if module == "test"
                            || module == "tests"
                            || module.starts_with("test_")
                            || module.starts_with("tests_")
                            || module.ends_with("_test")
                            || module.ends_with("_tests") =>
                    {
                        Some(parent_module.span)
                    }
                    _ => None,
                }
            };

            visitor.r.lint_buffer.buffer_lint(
                UNUSED_IMPORTS,
                unused.use_tree_id,
                ms,
                BuiltinLintDiag::UnusedImports {
                    remove_whole_use,
                    num_to_remove,
                    remove_spans,
                    test_module_span,
                    span_snippets,
                },
            );
        }

        let unused_imports = visitor.unused_imports;
        let mut check_redundant_imports = FxIndexSet::default();
        for module in self.arenas.local_modules().iter() {
            for (_key, resolution) in self.resolutions(*module).borrow().iter() {
                let resolution = resolution.borrow();

                if let Some(binding) = resolution.binding
                    && let NameBindingKind::Import { import, .. } = binding.kind
                    && let ImportKind::Single { id, .. } = import.kind
                {
                    if let Some(unused_import) = unused_imports.get(&import.root_id)
                        && unused_import.unused.contains(&id)
                    {
                        continue;
                    }

                    check_redundant_imports.insert(import);
                }
            }
        }

        let mut redundant_imports = UnordSet::default();
        for import in check_redundant_imports {
            if self.check_for_redundant_imports(import)
                && let Some(id) = import.id()
            {
                redundant_imports.insert(id);
            }
        }

        // The lint fixes for unused_import and unnecessary_qualification may conflict.
        // Deleting both unused imports and unnecessary segments of an item may result
        // in the item not being found.
        for unn_qua in &self.potentially_unnecessary_qualifications {
            if let LexicalScopeBinding::Item(name_binding) = unn_qua.binding
                && let NameBindingKind::Import { import, .. } = name_binding.kind
                && (is_unused_import(import, &unused_imports)
                    || is_redundant_import(import, &redundant_imports))
            {
                continue;
            }

            self.lint_buffer.buffer_lint(
                UNUSED_QUALIFICATIONS,
                unn_qua.node_id,
                unn_qua.path_span,
                BuiltinLintDiag::UnusedQualifications { removal_span: unn_qua.removal_span },
            );
        }

        fn is_redundant_import(
            import: Import<'_>,
            redundant_imports: &UnordSet<ast::NodeId>,
        ) -> bool {
            if let Some(id) = import.id()
                && redundant_imports.contains(&id)
            {
                return true;
            }
            false
        }

        fn is_unused_import(
            import: Import<'_>,
            unused_imports: &FxIndexMap<ast::NodeId, UnusedImport>,
        ) -> bool {
            if let Some(unused_import) = unused_imports.get(&import.root_id)
                && let Some(id) = import.id()
                && unused_import.unused.contains(&id)
            {
                return true;
            }
            false
        }
    }
}