cargo-machete 0.9.0

Find unused dependencies with this one weird trick!
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
use cargo_metadata::CargoOpt;
use grep::{
    matcher::LineTerminator,
    regex::{RegexMatcher, RegexMatcherBuilder},
    searcher::{self, BinaryDetection, Searcher, SearcherBuilder, Sink},
};
use log::{debug, trace, warn};
use meta::MetadataFields;
use rayon::prelude::*;
use std::{
    collections::{BTreeMap, HashSet},
    error::{self, Error},
    path::{Path, PathBuf},
};
use walkdir::WalkDir;

#[cfg(test)]
use crate::TOP_LEVEL;
use crate::UseCargoMetadata;

use self::meta::PackageMetadata;

mod meta {
    use std::collections::BTreeMap;

    use serde::{Deserialize, Serialize};

    #[derive(Serialize, Deserialize)]
    pub struct PackageMetadata {
        #[serde(rename = "cargo-machete")]
        pub cargo_machete: Option<MetadataFields>,
    }

    #[derive(Serialize, Deserialize)]
    pub struct MetadataFields {
        /// Crates triggering false positives in `cargo-machete`, which should not be reported as
        /// unused.
        #[serde(default)]
        pub ignored: Vec<String>,

        /// Crates whose name is different than the name in the Cargo.toml.
        ///
        /// Some crates have a different lib name (the name in `use` statements) that their package
        /// name (e.g. `rustls-webpki` is imported with `use webpki;`).
        #[serde(default)]
        pub renamed: BTreeMap<Box<str>, Box<str>>,
    }
}

pub(crate) struct PackageAnalysis {
    metadata: Option<cargo_metadata::Metadata>,
    pub manifest: cargo_toml::Manifest<meta::PackageMetadata>,
    pub package_name: String,
    pub unused: Vec<String>,
    pub ignored_used: Vec<String>,
}

impl PackageAnalysis {
    fn new(
        package_name: String,
        cargo_path: &Path,
        manifest: cargo_toml::Manifest<meta::PackageMetadata>,
        with_cargo_metadata: bool,
    ) -> anyhow::Result<Self> {
        let metadata = if with_cargo_metadata {
            Some(
                cargo_metadata::MetadataCommand::new()
                    .features(CargoOpt::AllFeatures)
                    .manifest_path(cargo_path)
                    //.other_options(["--frozen".to_owned()]) // TODO causes errors in cargo-metadata
                    .exec()?,
            )
        } else {
            None
        };

        Ok(Self {
            metadata,
            manifest,
            package_name,
            unused: Vec::default(),
            ignored_used: Vec::default(),
        })
    }
}

fn make_line_regexp(name: &str) -> String {
    // Syntax documentation: https://docs.rs/regex/latest/regex/#syntax
    //
    // Breaking down this regular expression: given a line,
    // - `use (::)?(?i){name}(?-i)(::|;| as)`: matches `use foo;`, `use foo::bar`, `use foo as bar;`, with
    // an optional "::" in front of the crate's name.
    // - `(?:[^:]|^|\W::)\b(?i){name}(?-i)::`: matches `foo::X`, but not `barfoo::X`. To ensure there's no polluting
    //   prefix we add `(?:[^:]|^|\W::)\b`, meaning that the crate name must be prefixed by either:
    //    * Not a `:` (therefore not a sub module)
    //    * The start of a line
    //    * Not a word character followed by `::` (to allow ::my_crate)
    // - `extern crate (?i){name}(?-i)( |;)`: matches `extern crate foo`, or `extern crate foo as bar`.
    // - `(?i){name}(?-i)` makes the match against the crate's name case insensitive
    format!(
        r#"use (::)?(?i){name}(?-i)(::|;| as)|(?:[^:]|^|\W::)\b(?i){name}(?-i)::|extern crate (?i){name}(?-i)( |;)"#
    )
}

fn make_multiline_regexp(name: &str) -> String {
    // Syntax documentation: https://docs.rs/regex/latest/regex/#syntax
    //
    // Breaking down this Terrible regular expression: tries to match uses of the crate's name in
    // compound `use` statement across multiple lines.
    //
    // It's split into 3 parts:
    //   1. Matches modules before the usage of the crate's name: `\s*(?:(::)?\w+{sub_modules_match}\s*,\s*)*`
    //   2. Matches the crate's name with optional sub-modules: `(::)?{name}{sub_modules_match}\s*`
    //   3. Matches modules after the usage of the crate's name: `(?:\s*,\s*(::)?\w+{sub_modules_match})*\s*,?\s*`
    //
    // In order to avoid false usage detection of `not_my_dep::my_dep` the regexp ensures that the
    // crate's name is at the top level of the use statement. However, it's not possible with
    // regexp to allow any number of matching `{` and `}` before the crate's usage (rust regexp
    // engine doesn't support recursion). Therefore, sub modules are authorized up to 4 levels
    // deep.

    let sub_modules_match = r#"(?:::\w+)*(?:::\*|\s+as\s+\w+|::\{(?:[^{}]*(?:\{(?:[^{}]*(?:\{(?:[^{}]*(?:\{[^{}]*\})?[^{}]*)*\})?[^{}]*)*\})?[^{}]*)*\})?"#;

    format!(
        r#"use \{{\s*(?:(::)?\w+{sub_modules_match}\s*,\s*)*(::)?{name}{sub_modules_match}\s*(?:\s*,\s*(::)?\w+{sub_modules_match})*\s*,?\s*\}};"#
    )
}

/// Returns all the paths to the Rust source files for a crate contained at the given path.
fn collect_paths(dir_path: &Path, analysis: &PackageAnalysis) -> Vec<PathBuf> {
    let manifest = &analysis.manifest;

    let mut root_paths: HashSet<PathBuf> = manifest
        .lib
        .iter()
        .chain(manifest.bin.iter())
        .chain(manifest.bench.iter())
        .chain(manifest.test.iter())
        .chain(manifest.example.iter())
        .filter_map(|p| {
            // Keep only files which names in `.rs`.
            let path_str = p.path.as_ref().filter(|s| s.ends_with(".rs"))?;
            // Remove the file name.
            PathBuf::from(path_str).parent().map(PathBuf::from)
        })
        .collect();

    trace!("found root paths: {root_paths:?}");

    if root_paths.is_empty() {
        // Assume "src/" if cargo_toml didn't find anything.
        root_paths.insert(PathBuf::from("src"));
        trace!("adding src/ since paths was empty");
    }

    // Collect all final paths for the crate first.
    let paths = root_paths
        .iter()
        .flat_map(|root| WalkDir::new(dir_path.join(root)).into_iter())
        .filter_map(|result| {
            result
                .inspect_err(|err| eprintln!("{err}"))
                .ok()
                .and_then(|entry| {
                    (entry.file_type().is_file()
                        && entry.path().extension().is_some_and(|ext| ext == "rs"))
                    .then(|| entry.path().to_owned())
                })
        })
        .collect();

    trace!("found transitive paths: {paths:?}");

    paths
}

/// Performs search of the given crate name with the following strategy: first try to use the line
/// matcher, then the multiline matcher if the line matcher failed.
///
/// Splitting the single line matcher from the multiline matcher makes maintenance of the regular
/// expressions simpler (oh well), and likely faster too since most use statements will be caught
/// by the single line matcher.
struct Search {
    line_matcher: RegexMatcher,
    line_searcher: Searcher,
    multiline_matcher: RegexMatcher,
    multiline_searcher: Searcher,
    sink: StopAfterFirstMatch,
}

impl Search {
    fn new(crate_name: &str) -> anyhow::Result<Self> {
        assert!(!crate_name.contains('-'));

        let line_matcher = RegexMatcher::new_line_matcher(&make_line_regexp(crate_name))?;
        let line_searcher = SearcherBuilder::new()
            .binary_detection(BinaryDetection::quit(b'\x00'))
            .line_terminator(LineTerminator::byte(b'\n'))
            .line_number(false)
            .build();

        let multiline_matcher = RegexMatcherBuilder::new()
            .multi_line(true)
            .build(&make_multiline_regexp(crate_name))?;
        let multiline_searcher = SearcherBuilder::new()
            .binary_detection(BinaryDetection::quit(b'\x00'))
            .multi_line(true)
            .line_number(false)
            .build();

        // Sanity-check: the matcher must allow multi-line searching.
        debug_assert!(multiline_searcher.multi_line_with_matcher(&multiline_matcher));

        let sink = StopAfterFirstMatch::new();

        Ok(Self {
            line_matcher,
            line_searcher,
            multiline_matcher,
            multiline_searcher,
            sink,
        })
    }

    fn try_singleline_then_multiline<
        F: FnMut(&mut Searcher, &RegexMatcher, &mut StopAfterFirstMatch) -> Result<(), Box<dyn Error>>,
    >(
        &mut self,
        mut func: F,
    ) -> anyhow::Result<bool> {
        match func(&mut self.line_searcher, &self.line_matcher, &mut self.sink) {
            Ok(()) => {
                if self.sink.found {
                    return Ok(true);
                }
                // Single line matcher didn't work, try the multiline matcher now.
                func(
                    &mut self.multiline_searcher,
                    &self.multiline_matcher,
                    &mut self.sink,
                )
                .map_err(|err| anyhow::anyhow!("when searching with complex pattern: {err}"))
                .map(|()| self.sink.found)
            }
            Err(err) => anyhow::bail!("when searching with line pattern: {err}"),
        }
    }

    fn search_path(&mut self, path: &Path) -> anyhow::Result<bool> {
        self.try_singleline_then_multiline(|searcher, matcher, sink| {
            searcher.search_path(matcher, path, sink)
        })
    }

    #[cfg(test)]
    fn search_string(&mut self, s: &str) -> anyhow::Result<bool> {
        self.try_singleline_then_multiline(|searcher, matcher, sink| {
            searcher.search_reader(matcher, s.as_bytes(), sink)
        })
    }
}

/// Read a manifest and try to find a workspace manifest to complete the data available in the
/// manifest.
///
/// This will look up the file tree to find the Cargo.toml workspace manifest, assuming it's on a
/// parent directory.
fn get_full_manifest(
    dir_path: &Path,
    manifest_path: &Path,
) -> anyhow::Result<(
    cargo_toml::Manifest<PackageMetadata>,
    Option<meta::MetadataFields>,
)> {
    // HACK: we can't plain use `from_path_with_metadata` here, because it calls
    // `complete_from_path` just a bit too early (before we've had a chance to call
    // `inherit_workspace`). See https://gitlab.com/crates.rs/cargo_toml/-/issues/20 for details,
    // and a possible future fix.
    let cargo_toml_content = std::fs::read(manifest_path)?;
    let mut manifest =
        cargo_toml::Manifest::<PackageMetadata>::from_slice_with_metadata(&cargo_toml_content)?;

    let mut ws_manifest_and_path = None;

    // Canonicalize the path, so as to get the full "parenthood" of relative paths.
    let mut dir_path = std::fs::canonicalize(dir_path).unwrap_or_else(|err| {
        warn!("error when canonicalizing dir_path: {err}");
        dir_path.to_owned()
    });

    // Try to find a workspace manifest, starting from the current directory, going up to the
    // filesystem's root.
    while dir_path.pop() {
        let workspace_cargo_path = dir_path.join("Cargo.toml");
        if let Ok(workspace_manifest) =
            cargo_toml::Manifest::<PackageMetadata>::from_path_with_metadata(&workspace_cargo_path)
            && workspace_manifest.workspace.is_some()
        {
            ws_manifest_and_path = Some((workspace_manifest, workspace_cargo_path));
            break;
        }
    }

    manifest.complete_from_path_and_workspace(
        manifest_path,
        ws_manifest_and_path.as_ref().map(|(m, p)| (m, p.as_path())),
    )?;

    Ok((
        manifest,
        // Look for `workspace.metadata.cargo-machete` custom metadata in the workspace Cargo.toml.
        ws_manifest_and_path
            .and_then(|(manifest, _path)| manifest.workspace?.metadata?.cargo_machete),
    ))
}

pub(crate) fn find_unused(
    manifest_path: &Path,
    with_cargo_metadata: UseCargoMetadata,
) -> anyhow::Result<Option<PackageAnalysis>> {
    let mut dir_path = manifest_path.to_path_buf();
    dir_path.pop();

    trace!("trying to open {}...", manifest_path.display());

    let (manifest, workspace_metadata) = get_full_manifest(&dir_path, manifest_path)?;

    let package_name = match manifest.package {
        Some(ref package) => package.name.clone(),
        None => return Ok(None),
    };

    debug!("handling {} ({})", package_name, dir_path.display());

    let mut analysis = PackageAnalysis::new(
        package_name,
        manifest_path,
        manifest,
        matches!(with_cargo_metadata, UseCargoMetadata::Yes),
    )?;

    let paths = collect_paths(&dir_path, &analysis);

    // TODO extend to dev dependencies + build dependencies, and be smarter in the grouping of
    // searched paths
    // Maps dependency name (the name of the key in the Cargo.toml dependency
    // table, can have dashes, not necessarily the name in the crate registry)
    // to crate name (extern crate, snake case)
    let dependencies: BTreeMap<String, String> = if let Some((metadata, resolve)) = analysis
        .metadata
        .as_ref()
        .and_then(|metadata| metadata.resolve.as_ref().map(|resolve| (metadata, resolve)))
    {
        if let Some(ref root) = resolve.root {
            // This gives us resolved dependencies, in crate form
            let root_node = resolve
                .nodes
                .iter()
                .find(|node| node.id == *root)
                .expect("root should be resolved by cargo-metadata");
            // This gives us the original dependency table
            // May have more than resolved if some were never enabled
            let root_package = metadata
                .packages
                .iter()
                .find(|pkg| pkg.id == *root)
                .expect("root should appear under cargo-metadata packages");
            // For every resolved dependency:
            // look it up in the package list to find the name (the one in registries)
            // look up that name in dependencies of the root_package;
            // find if it uses a different key through the rename field
            root_node
                .deps
                .iter()
                .map(|dep| {
                    let crate_name = dep.name.clone();
                    let dep_pkg = metadata
                        .packages
                        .iter()
                        .find(|pkg| pkg.id == dep.pkg)
                        .expect(
                            "resolved dependencies should appear under cargo-metadata packages",
                        );

                    let mut dep_spec_it = root_package
                        .dependencies
                        .iter()
                        .filter(|dep_spec| dep_spec.name == *dep_pkg.name);

                    // The dependency can appear more than once, for example if it is both
                    // a dependency and a dev-dependency (often with more features enabled).
                    // We'll assume cargo enforces consistency.
                    let dep_spec = dep_spec_it
                        .next()
                        .expect("resolved dependency should have a matching dependency spec");

                    // If the dependency was renamed, through key = { package = … },
                    // the original key is in dep_spec.rename.
                    let dep_key = dep_spec
                        .rename
                        .clone()
                        .unwrap_or_else(|| dep_spec.name.clone());
                    (dep_key, crate_name)
                })
                .collect()
        } else {
            // No root -> virtual workspace, empty map
            Default::default()
        }
    } else {
        analysis
            .manifest
            .dependencies
            .keys()
            .map(|k| (k.clone(), k.replace('-', "_")))
            .collect()
    };

    let meta = analysis
        .manifest
        .package
        .as_ref()
        .and_then(|package| package.metadata.as_ref()?.cargo_machete.as_ref());

    // Keep a side-list of ignored dependencies (likely false positives).
    let ignored = meta
        .map(|meta| meta.ignored.iter().collect::<HashSet<_>>())
        .unwrap_or_default();

    // Keep a list of renamed dependencies
    static NO_RENAMED: BTreeMap<Box<str>, Box<str>> = BTreeMap::new();
    let renamed = meta.map(|meta| &meta.renamed).unwrap_or(&NO_RENAMED);

    let (workspace_ignored, workspace_renamed): (HashSet<_>, _) = workspace_metadata
        .map(|MetadataFields { ignored, renamed }| (HashSet::from_iter(ignored), renamed))
        .unwrap_or_default();

    enum SingleDepResult {
        /// Dependency is unused and not marked as ignored.
        Unused(String),
        /// Dependency is marked as ignored but used.
        IgnoredButUsed(String),
    }

    let results: Vec<SingleDepResult> = dependencies
        .into_par_iter()
        .filter_map(|(dep_name, crate_name)| {
            // If the crate was renamed (in the current Cargo.toml), use the renamed name.
            let crate_name = renamed
                .get(dep_name.as_str())
                // Also try to look up the renames in the custom workspace metadata
                .or_else(|| workspace_renamed.get(dep_name.as_str()))
                // fall-back to the crate name
                .map_or(crate_name.as_str(), Box::as_ref);

            let mut search = Search::new(crate_name).expect("constructing grep context");

            let mut found_once = false;
            for path in &paths {
                trace!("looking for {} in {}", crate_name, path.to_string_lossy(),);
                match search.search_path(path) {
                    Ok(true) => {
                        found_once = true;
                        break;
                    }
                    Ok(false) => {}
                    Err(err) => {
                        eprintln!("{}: {}", path.display(), err);
                    }
                };
            }

            if !found_once {
                if ignored.contains(&dep_name) || workspace_ignored.contains(&dep_name) {
                    return None;
                }

                Some(SingleDepResult::Unused(dep_name))
            } else {
                if ignored.contains(&dep_name) {
                    return Some(SingleDepResult::IgnoredButUsed(dep_name));
                }

                None
            }
        })
        .collect();

    for result in results {
        match result {
            SingleDepResult::Unused(dep) => analysis.unused.push(dep),
            SingleDepResult::IgnoredButUsed(dep) => analysis.ignored_used.push(dep),
        }
    }

    Ok(Some(analysis))
}

struct StopAfterFirstMatch {
    found: bool,
}

impl StopAfterFirstMatch {
    fn new() -> Self {
        Self { found: false }
    }
}

impl Sink for StopAfterFirstMatch {
    type Error = Box<dyn error::Error>;

    fn matched(
        &mut self,
        _searcher: &searcher::Searcher,
        matsh: &searcher::SinkMatch<'_>,
    ) -> Result<bool, Self::Error> {
        let mat = String::from_utf8(matsh.bytes().to_vec())?;
        let mat = mat.trim();

        if mat.starts_with("//") || mat.starts_with("//!") {
            // Continue if seeing what resembles a comment or doc comment. Unfortunately we can't
            // do anything better because trying to figure whether we're within a (doc) comment
            // would require actual parsing of the Rust code.
            return Ok(true);
        }

        // Otherwise, we've found it: mark to true, and return false to indicate that we can stop
        // searching.
        self.found = true;
        Ok(false)
    }
}

#[test]
fn test_regexp() -> anyhow::Result<()> {
    fn test_one(crate_name: &str, content: &str) -> anyhow::Result<bool> {
        let mut search = Search::new(crate_name)?;
        search.search_string(content)
    }

    assert!(!test_one("log", "use da_force_luke;")?);
    assert!(!test_one("log", "use flog;")?);
    assert!(!test_one("log", "use log_once;")?);
    assert!(!test_one("log", "use log_once::info;")?);
    assert!(!test_one("log", "use flog::flag;")?);
    assert!(!test_one("log", "flog::flag;")?);
    assert!(!test_one("log", "use ::flog;")?);
    assert!(!test_one("log", "use :log;")?);

    assert!(test_one("log", "use log;")?);
    assert!(test_one("log", "use ::log;")?);
    assert!(test_one("log", "use log::{self};")?);
    assert!(test_one("log", "use log::*;")?);
    assert!(test_one("log", "use log::info;")?);
    assert!(test_one("log", "use log as logging;")?);
    assert!(test_one("log", "extern crate log;")?);
    assert!(test_one("log", "extern crate log as logging")?);
    assert!(test_one("log", r#"log::info!("fyi")"#)?);

    assert!(test_one("Log", "use log;")?);
    assert!(test_one("Log", "use ::log;")?);
    assert!(test_one("Log", "use log::{self};")?);
    assert!(test_one("Log", "use log::*;")?);
    assert!(test_one("Log", "use log::info;")?);
    assert!(test_one("Log", "use log as logging;")?);
    assert!(test_one("Log", "extern crate log;")?);
    assert!(test_one("Log", "extern crate log as logging")?);
    assert!(test_one("Log", r#"log::info!("fyi")"#)?);

    assert!(test_one("log", "use Log;")?);
    assert!(test_one("log", "use ::Log;")?);
    assert!(test_one("log", "use Log::{self};")?);
    assert!(test_one("log", "use Log::*;")?);
    assert!(test_one("log", "use Log::info;")?);
    assert!(test_one("log", "use Log as logging;")?);
    assert!(test_one("log", "extern crate Log;")?);
    assert!(test_one("log", "extern crate Log as logging")?);
    assert!(test_one("log", r#"Log::info!("fyi")"#)?);

    assert!(test_one(
        "bitflags",
        r#"
use std::fmt;
bitflags::macro! {
"#
    )?);

    assert!(test_one(
        "Bitflags",
        r#"
use std::fmt;
bitflags::macro! {
"#
    )?);

    assert!(test_one(
        "bitflags",
        r#"
use std::fmt;
Bitflags::macro! {
"#
    )?);

    // Compound `use as` statements. Here come the nightmares...
    assert!(test_one("log", "use { log as logging };")?);
    assert!(!test_one("lol", "use { log as logging };")?);

    assert!(test_one(
        "log",
        r#"
use {
    log as logging
};
"#
    )?);

    assert!(test_one(
        "log",
        r#"
use { log as
logging
};
"#
    )?);

    assert!(test_one(
        "log",
        r#"
use { log
    as
        logging
};
"#
    )?);

    assert!(test_one(
        "log",
        r#"
use {
    x::{ y },
    log as logging,
};
"#
    )?);

    // Regex must stop at the first };
    assert!(!test_one(
        "log",
        r#"
use {
    x as y
};
type logging = u64;
fn main() {
    let func = |log: u32| {
        log as logging
    };
    func(42);
}
"#
    )?);

    // Regression test.
    // Comments and spaces are meaningful here.
    assert!(test_one(
        "static_assertions",
        r#"
    // lol
    static_assertions::assert_not_impl_all!(A: B);
    "#
    )?);

    // Regression test.
    // Comments and spaces are meaningful here.
    assert!(test_one(
        "futures",
        r#"
// the [`futures::executor::block_on`] function
pub use futures::future;

    "#
    )?);

    // multi-dep single use statements
    assert!(test_one(
        "futures",
        r#"pub use {async_trait, futures, reqwest};"#
    )?);

    // multi-dep single use statements with ::
    assert!(test_one(
        "futures",
        r#"pub use {async_trait, ::futures, reqwest};"#
    )?);

    // No false usage detection of `not_my_dep::my_dep` on compound imports
    assert!(!test_one(
        "futures",
        r#"pub use {async_trait, not_futures::futures, reqwest};"#
    )?);

    // No false usage detection of `not_my_dep::my_dep` on multiple lines
    assert!(!test_one(
        "futures",
        r#"
pub use {
    async_trait,
    not_futures::futures,
    reqwest,
};"#
    )?);

    // No false usage detection on single line `not_my_dep::my_dep`
    assert!(!test_one(
        "futures",
        r#"use not_futures::futures::stuff_in_futures;"#
    )?);

    // multi-dep single use statements with nesting
    assert!(test_one(
        "futures",
        r#"pub use {
            async_trait::{mod1, dep2},
            futures::{futures_mod1, futures_mod2::{futures_mod21, futures_mod22}},
            reqwest,
        };"#
    )?);

    // multi-dep single use statements with star import and renaming
    assert!(test_one(
        "futures",
        r#"pub use {
            async_trait::sub_mod::*,
            futures as futures_renamed,
            reqwest,
        };"#
    )?);

    // multi-dep single use statements with complex imports and renaming
    assert!(test_one(
        "futures",
        r#"pub use {
            other_dep::{
                star_mod::*,
                unnamed_import::{UnnamedTrait as _, other_mod},
                renamed_import as new_name,
                sub_import::{mod1, mod2},
            },
            futures as futures_renamed,
            reqwest,
        };"#
    )?);

    // No false usage detection of `not_my_dep::my_dep` with nesting
    assert!(!test_one(
        "futures",
        r#"pub use {
            async_trait::{mod1, dep2},
            not_futures::futures::{futures_mod1, futures_mod2::{futures_mod21, futures_mod22}},
            reqwest,
        };"#
    )?);

    // Detects top level usage
    assert!(test_one("futures", r#" ::futures::mod1"#)?);

    Ok(())
}

#[cfg(test)]
fn check_analysis<F: Fn(PackageAnalysis)>(rel_path: &str, callback: F) {
    for use_cargo_metadata in UseCargoMetadata::all() {
        let analysis = find_unused(
            &PathBuf::from(TOP_LEVEL).join(rel_path),
            *use_cargo_metadata,
        )
        .expect("find_unused must return an Ok result")
        .expect("no error during processing");
        callback(analysis);
    }
}

#[test]
fn test_just_unused() {
    // a crate that simply does not use a dependency it refers to
    check_analysis("./integration-tests/just-unused/Cargo.toml", |analysis| {
        assert_eq!(analysis.unused, &["log".to_string()]);
    });
}

#[test]
fn test_just_unused_with_manifest() {
    // a crate that does not use a dependency it refers to, and uses workspace properties
    check_analysis(
        "./integration-tests/workspace-package/program/Cargo.toml",
        |analysis| {
            assert_eq!(analysis.unused, &["log".to_string()]);
        },
    );
}

#[test]
fn test_unused_transitive() {
    // lib1 has zero dependencies
    check_analysis(
        "./integration-tests/unused-transitive/lib1/Cargo.toml",
        |analysis| {
            assert!(analysis.unused.is_empty());
        },
    );

    // lib2 effectively uses lib1
    check_analysis(
        "./integration-tests/unused-transitive/lib2/Cargo.toml",
        |analysis| {
            assert!(analysis.unused.is_empty());
        },
    );

    // but top level references both lib1 and lib2, and only uses lib2
    check_analysis(
        "./integration-tests/unused-transitive/Cargo.toml",
        |analysis| {
            assert_eq!(analysis.unused, &["lib1".to_string()]);
        },
    );
}

#[test]
fn test_false_positive_macro_use() {
    // when a lib uses a dependency via a macro, there's no way we can find it by scanning the
    // source code.
    check_analysis(
        "./integration-tests/false-positive-log/Cargo.toml",
        |analysis| {
            assert_eq!(analysis.unused, &["log".to_string()]);
        },
    );
}

#[test]
fn test_with_bench() {
    // when a package has a bench file designated by binary name, it seems that `cargo_toml`
    // doesn't fill in a default path to the source code.
    check_analysis(
        "./integration-tests/with-bench/bench/Cargo.toml",
        |analysis| {
            assert!(analysis.unused.is_empty());
        },
    );
}

#[test]
fn test_renamed_field_works() -> anyhow::Result<()> {
    // cargo-machete properly handles a correct rename (rustls-webpki -> webpki), when the rename
    // happens in the crate manifest
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/renamed-dep/Cargo.toml"),
        UseCargoMetadata::No,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused.as_slice(), &["bytes", "log"]);
    Ok(())
}

#[test]
fn test_renamed_field_workspace_works() -> anyhow::Result<()> {
    // cargo-machete properly handles a correct rename (rustls-webpki -> webpki), when the rename
    // happens in the workspace manifest
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL)
            .join("./integration-tests/renamed-dep-workspace/inner/Cargo.toml"),
        UseCargoMetadata::No,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused.as_slice(), &["bytes", "flagset"]);
    Ok(())
}

#[test]
fn test_crate_renaming_works() -> anyhow::Result<()> {
    // when a lib like xml-rs is exposed with a different name, cargo-machete doesn't return false
    // positives.
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/renaming-works/Cargo.toml"),
        UseCargoMetadata::Yes,
    )?
    .expect("no error during processing");
    assert!(analysis.unused.is_empty());

    // But when not using cargo-metadata, there's a false positive!
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/renaming-works/Cargo.toml"),
        UseCargoMetadata::No,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused, &["xml-rs".to_string()]);

    Ok(())
}

#[test]
fn test_unused_renamed_in_registry() -> anyhow::Result<()> {
    // when a lib like xml-rs is exposed with a different name,
    // cargo-machete reports the unused spec properly.
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/unused-renamed-in-registry/Cargo.toml"),
        UseCargoMetadata::Yes,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused, &["xml-rs".to_string()]);

    Ok(())
}

#[test]
fn test_unused_renamed_in_spec() -> anyhow::Result<()> {
    // when a lib is renamed through key = { package = … },
    // cargo-machete reports the unused spec properly.
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/unused-renamed-in-spec/Cargo.toml"),
        UseCargoMetadata::Yes,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused, &["tracing".to_string()]);

    Ok(())
}

#[test]
fn test_unused_kebab_spec() -> anyhow::Result<()> {
    // when a lib uses kebab naming, cargo-machete reports the unused spec properly.
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/unused-kebab-spec/Cargo.toml"),
        UseCargoMetadata::Yes,
    )?
    .expect("no error during processing");
    assert_eq!(analysis.unused, &["log-once".to_string()]);

    Ok(())
}

#[test]
fn test_ignore_deps_works() {
    // ensure that ignored deps listed in Cargo.toml package.metadata.cargo-machete.ignored are
    // correctly ignored.
    check_analysis("./integration-tests/ignored-dep/Cargo.toml", |analysis| {
        assert_eq!(analysis.unused, &["rand".to_string()]);
        assert_eq!(analysis.ignored_used, &["rand_core".to_string()]);
    });
}

#[test]
fn test_ignore_deps_workspace_works() {
    // ensure that ignored deps listed in Cargo.toml workspace.metadata.cargo-machete.ignored are
    // correctly ignored.
    check_analysis(
        "./integration-tests/ignored-dep-workspace/inner/Cargo.toml",
        |analysis| {
            assert_eq!(analysis.unused, &["rand".to_string()]);
            assert_eq!(analysis.ignored_used, &["rand_core".to_string()]);
        },
    );
}

#[test]
fn test_workspace_from_relative_path() {
    // Ensure that finding a workspace using a relative path works.
    use std::env::{current_dir, set_current_dir};

    let prev_cwd = current_dir().unwrap();

    set_current_dir(
        PathBuf::from(TOP_LEVEL).join("./integration-tests/workspace-package/program/"),
    )
    .unwrap();

    let path = Path::new("./Cargo.toml");
    let analysis = find_unused(path, UseCargoMetadata::No);

    // Reset the current directory *before* running any other check.
    set_current_dir(prev_cwd).unwrap();

    let analysis = analysis
        .expect("find_unused must return an Ok result")
        .expect("no error during processing");

    assert_eq!(analysis.unused, &["log".to_string()]);
    assert!(analysis.ignored_used.is_empty());
}

#[test]
fn test_multi_key_dep() {
    let analysis = find_unused(
        &PathBuf::from(TOP_LEVEL).join("./integration-tests/multi-key-dep/Cargo.toml"),
        UseCargoMetadata::Yes,
    )
    .expect("find_unused must return an Ok result")
    .expect("no error during processing");

    assert_eq!(analysis.unused, &["cc".to_string(), "rand".to_string()]);
}