-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #104: correctly parse id repr for Rust 1.78+
- Loading branch information
Showing
1 changed file
with
45 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,12 +352,51 @@ pub(crate) fn find_unused( | |
.nodes | ||
.iter() | ||
.find(|node| { | ||
// e.g. "aa 0.1.0 (path+file:///tmp/aa)" | ||
node.id | ||
.repr | ||
.split(' ') | ||
.next() // e.g. "aa" | ||
.map_or(false, |node_package_name| node_package_name == package_name) | ||
// Note: this is dependent on rustc, so it's a bit fragile, and may break with | ||
// nightly versions of the compiler (see cargo-machete issue #104). | ||
|
||
// The node id can have multiple representations: | ||
// - on rust 1.77 and before, something like "aa 0.1.0 (path+file:///tmp/aa)". | ||
// - on rust 1.78+, something like: | ||
// - "path+file:///home/ben/cargo-machete/integration-tests/aa#0.1.0" | ||
// - "path+file:///home/ben/cargo-machete/integration-tests/directory#[email protected]" | ||
let repr = &node.id.repr; | ||
|
||
let package_found = if repr.contains(' ') { | ||
// Rust < 1.78, take everything up to the space. | ||
node.id.repr.split(' ').next() // e.g. "aa" | ||
} else { | ||
// Rust >= 1.78. Oh boy. | ||
let mut temp = None; | ||
|
||
let mut slash_split = node.id.repr.split('/').peekable(); | ||
|
||
while let Some(subset) = slash_split.next() { | ||
// Continue until the last part of the path. | ||
if slash_split.peek().is_some() { | ||
continue; | ||
} | ||
|
||
// If there's no next slash separator, we've reached the end, and subset is | ||
// one of: | ||
// - aa#0.1.0 | ||
// - directory#[email protected] | ||
if subset.contains('@') { | ||
let mut hash_split = subset.split('#'); | ||
// Skip everything before the hash. | ||
hash_split.next(); | ||
// Now in the rest, take everything up to the @. | ||
temp = hash_split.next().and_then(|end| end.split('@').next()); | ||
} else { | ||
// Otherwise, take everything up to the #. | ||
temp = subset.split('#').next(); | ||
} | ||
} | ||
|
||
temp | ||
}; | ||
|
||
package_found.map_or(false, |node_package_name| node_package_name == package_name) | ||
}) | ||
.expect("the current package must be in the dependency graph") | ||
.deps | ||
|