use anyhow::{Context, Result};
use crate::deps::UnusedDependency;
use crate::project::Project;
use std::fs;
use std::path::Path;
use walkdir::WalkDir;
fn extract_dependencies(cargo_toml_path: &Path) -> Result<Vec<(String, String)>> {
let content = fs::read_to_string(cargo_toml_path)
.with_context(|| format!("Failed to read Cargo.toml: {:?}", cargo_toml_path))?;
let toml: toml::Value = toml::from_str(&content)
.with_context(|| format!("Failed to parse Cargo.toml: {:?}", cargo_toml_path))?;
let mut deps = Vec::new();
if let Some(deps_table) = toml.get("dependencies").and_then(|v| v.as_table()) {
for (name, _) in deps_table {
deps.push((name.clone(), "[dependencies]".to_string()));
}
}
if let Some(dev_deps_table) = toml.get("dev-dependencies").and_then(|v| v.as_table()) {
for (name, _) in dev_deps_table {
deps.push((name.clone(), "[dev-dependencies]".to_string()));
}
}
if let Some(build_deps_table) = toml.get("build-dependencies").and_then(|v| v.as_table()) {
for (name, _) in build_deps_table {
deps.push((name.clone(), "[build-dependencies]".to_string()));
}
}
Ok(deps)
}
fn normalize_crate_name(name: &str) -> String {
name.replace('-', "_")
}
fn search_patterns_for(normalized_dep: &str) -> [String; 7] {
[
format!("use {}::", normalized_dep),
format!("use {};", normalized_dep),
format!("use crate::{}", normalized_dep),
format!("{}::", normalized_dep),
format!("extern crate {}", normalized_dep),
format!("{}!", normalized_dep),
format!("#[{}", normalized_dep),
]
}
fn collect_source_text(project_path: &Path) -> Vec<String> {
let mut contents: Vec<String> = Vec::new();
for sub in ["src", "examples", "tests"] {
let dir = project_path.join(sub);
if !dir.exists() {
continue;
}
for entry in WalkDir::new(&dir).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file()
&& entry.path().extension().is_some_and(|e| e == "rs")
&& let Ok(content) = fs::read_to_string(entry.path())
{
contents.push(content);
}
}
}
let build_rs = project_path.join("build.rs");
if let Ok(content) = fs::read_to_string(&build_rs) {
contents.push(content);
}
contents
}
fn is_dependency_used(
dep_name: &str,
sources: &[String],
cargo_toml_content: &str,
) -> bool {
let normalized = normalize_crate_name(dep_name);
let patterns = search_patterns_for(&normalized);
for content in sources {
for pattern in &patterns {
if content.contains(pattern.as_str()) {
return true;
}
}
}
if cargo_toml_content.contains(&format!("{}/", dep_name))
|| cargo_toml_content.contains(&format!("{}-", dep_name))
|| cargo_toml_content.contains(&format!("{}/", normalized))
|| cargo_toml_content.contains(&format!("{}-", normalized))
{
return true;
}
false
}
pub fn check_unused_dependencies(project: &Project) -> Result<Vec<UnusedDependency>> {
let cargo_toml = project.path.join("Cargo.toml");
if !cargo_toml.exists() {
return Ok(vec![]);
}
let all_deps = extract_dependencies(&cargo_toml)?;
const SKIP_LIST: &[&str] = &[
"proc-macro2",
"quote",
"syn",
"serde",
"serde_derive",
"serde_json", ];
let deps_to_check: Vec<(String, String)> = all_deps
.into_iter()
.filter(|(name, _)| {
!SKIP_LIST.contains(&name.as_str())
&& !name.ends_with("_derive")
&& !name.contains("proc-macro")
})
.collect();
if deps_to_check.is_empty() {
return Ok(vec![]);
}
let sources = collect_source_text(&project.path);
let cargo_content = fs::read_to_string(&cargo_toml).unwrap_or_default();
let mut unused = Vec::new();
for (dep_name, location) in deps_to_check {
if !is_dependency_used(&dep_name, &sources, &cargo_content) {
unused.push(UnusedDependency {
name: dep_name,
location,
});
}
}
Ok(unused)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalize_crate_name() {
assert_eq!(normalize_crate_name("my-crate"), "my_crate");
assert_eq!(normalize_crate_name("my_crate"), "my_crate");
assert_eq!(normalize_crate_name("serde-json"), "serde_json");
}
#[test]
fn test_extract_dependencies() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
tokio = "1.0"
[dev-dependencies]
tempfile = "3.0"
"#,
).unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
assert!(deps.len() >= 2);
let dep_names: Vec<String> = deps.iter().map(|(n, _)| n.clone()).collect();
assert!(dep_names.contains(&"serde".to_string()));
assert!(dep_names.contains(&"tokio".to_string()));
}
#[test]
fn test_extract_dependencies_includes_build_deps() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
[build-dependencies]
cc = "1.0"
"#,
)
.unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
let names: Vec<String> = deps.iter().map(|(n, _)| n.clone()).collect();
assert!(names.contains(&"cc".to_string()));
let cc_loc = deps
.iter()
.find(|(n, _)| n == "cc")
.map(|(_, l)| l.as_str())
.unwrap();
assert_eq!(cc_loc, "[build-dependencies]");
}
#[test]
fn test_is_dependency_used_with_use_statement() {
let sources = vec!["use serde::Deserialize;".to_string()];
assert!(is_dependency_used("serde", &sources, ""));
}
#[test]
fn test_is_dependency_used_unused() {
let sources = vec!["fn main() {}".to_string()];
assert!(!is_dependency_used("serde", &sources, ""));
}
#[test]
fn test_is_dependency_used_dash_underscore_normalization() {
let sources = vec!["use serde_json::Value;".to_string()];
assert!(is_dependency_used("serde-json", &sources, ""));
}
#[test]
fn test_is_dependency_used_macro_invocation() {
let sources = vec!["println!(\"hi\");".to_string()];
assert!(is_dependency_used("println", &sources, ""));
}
#[test]
fn test_is_dependency_used_attribute() {
let sources = vec!["#[derive(Debug)]".to_string()];
assert!(is_dependency_used("derive", &sources, ""));
}
#[test]
fn test_is_dependency_used_cargo_feature_reference() {
let sources = vec!["fn main() {}".to_string()];
let cargo = r#"[features]
feat1 = ["some-crate/feature"]
"#;
assert!(is_dependency_used("some-crate", &sources, cargo));
}
#[test]
fn test_is_dependency_used_bare_use_statement() {
let sources = vec!["use tempfile;".to_string()];
assert!(is_dependency_used("tempfile", &sources, ""));
}
#[test]
fn test_is_dependency_used_use_crate_path() {
let sources = vec!["use crate::serde_json::Value;".to_string()];
assert!(is_dependency_used("serde-json", &sources, ""));
}
#[test]
fn test_is_dependency_used_extern_crate() {
let sources = vec!["extern crate libc;".to_string()];
assert!(is_dependency_used("libc", &sources, ""));
}
#[test]
fn test_is_dependency_used_manifest_normalized_variants() {
let sources = vec!["fn main() {}".to_string()];
assert!(is_dependency_used(
"serde-json",
&sources,
"[features]\nbig = [\"serde_json/fancy\"]\n"
));
assert!(is_dependency_used(
"serde-json",
&sources,
"[package]\nname = \"serde_json-something\"\n"
));
}
#[test]
fn test_is_dependency_used_plain_name_no_false_positive() {
let sources = vec!["let serdejson = 1;".to_string()];
assert!(!is_dependency_used("serde-json", &sources, ""));
}
#[test]
fn test_check_unused_dependencies_finds_unused() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
rayon = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
names.contains(&"rayon".to_string()),
"rayon should be detected as unused, got: {:?}",
names
);
}
#[test]
fn test_check_unused_dependencies_no_false_positive_for_used() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
rayon = "1.0"
"#,
)
.unwrap();
fs::write(
temp_dir.path().join("src/main.rs"),
"use rayon::prelude::*;\nfn main() {}",
)
.unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
!names.contains(&"rayon".to_string()),
"rayon is used and should not be flagged as unused"
);
}
#[test]
fn test_check_unused_dependencies_skips_skip_list() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
serde = "1.0"
serde_derive = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
!names.contains(&"serde".to_string()),
"serde is in skip list and should not be flagged"
);
}
#[test]
fn test_check_unused_dependencies_missing_manifest_is_empty() {
let temp_dir = tempfile::TempDir::new().unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
assert!(unused.is_empty());
}
#[test]
fn test_check_unused_dependencies_dev_and_build_locations() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dev-dependencies]
unused-dev = "1.0"
[build-dependencies]
cc = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
fs::write(
temp_dir.path().join("build.rs"),
"fn main() { let _ = cc::compiler(); }",
)
.unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(names.contains(&"unused-dev".to_string()));
assert!(!names.contains(&"cc".to_string()), "cc is used in build.rs");
let dev = unused.iter().find(|d| d.name == "unused-dev").unwrap();
assert_eq!(dev.location, "[dev-dependencies]");
}
#[test]
fn test_check_unused_dependencies_skips_derive_and_proc_macro_names() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(
temp_dir.path().join("Cargo.toml"),
r#"
[package]
name = "test"
version = "0.1.0"
[dependencies]
some_derive = "1.0"
my-proc-macro-helper = "1.0"
thiserror = "1.0"
"#,
)
.unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let project = crate::project::Project {
path: temp_dir.path().to_path_buf(),
is_workspace: false,
};
let unused = check_unused_dependencies(&project).unwrap();
let names: Vec<String> = unused.iter().map(|d| d.name.clone()).collect();
assert!(
!names.contains(&"some_derive".to_string()),
"*_derive names are skipped (likely used via re-exported macros)"
);
assert!(
!names.contains(&"my-proc-macro-helper".to_string()),
"names containing proc-macro are skipped"
);
assert!(
names.contains(&"thiserror".to_string()),
"normal deps outside the skip filters are still checked"
);
}
#[test]
fn test_collect_source_text_includes_build_rs() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::create_dir_all(temp_dir.path().join("examples")).unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
fs::write(
temp_dir.path().join("examples/demo.rs"),
"fn main() {}",
)
.unwrap();
fs::write(temp_dir.path().join("build.rs"), "fn main() {}").unwrap();
let texts = collect_source_text(temp_dir.path());
assert_eq!(texts.len(), 3);
}
#[test]
fn test_collect_source_text_includes_tests_dir() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("tests")).unwrap();
fs::write(
temp_dir.path().join("tests/integration.rs"),
"use my_crate::thing;",
)
.unwrap();
let texts = collect_source_text(temp_dir.path());
assert_eq!(texts.len(), 1);
assert!(texts[0].contains("my_crate"));
}
#[test]
fn test_collect_source_text_empty_project() {
let temp_dir = tempfile::TempDir::new().unwrap();
let texts = collect_source_text(temp_dir.path());
assert!(texts.is_empty());
}
#[test]
fn test_collect_source_text_skips_invalid_utf8() {
let temp_dir = tempfile::TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src")).unwrap();
fs::write(temp_dir.path().join("src/binary.bin.rs"), [0xFF, 0xFE, 0x00]).unwrap();
fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let texts = collect_source_text(temp_dir.path());
assert_eq!(texts.len(), 1, "invalid UTF-8 file skipped without error");
}
#[test]
fn test_extract_dependencies_renamed_dep_uses_manifest_key() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[dependencies]
foo = { package = "bar", version = "1.0" }
"#,
)
.unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
let names: Vec<String> = deps.iter().map(|(n, _)| n.clone()).collect();
assert_eq!(names, vec!["foo".to_string()]);
}
#[test]
fn test_extract_dependencies_ignores_workspace_dependencies() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(
&cargo_toml,
r#"
[workspace.dependencies]
serde = "1.0"
"#,
)
.unwrap();
let deps = extract_dependencies(&cargo_toml).unwrap();
assert!(deps.is_empty());
}
#[test]
fn test_extract_dependencies_invalid_toml_is_err() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cargo_toml = temp_dir.path().join("Cargo.toml");
fs::write(&cargo_toml, "not [ valid toml").unwrap();
assert!(extract_dependencies(&cargo_toml).is_err());
}
#[test]
fn test_extract_dependencies_missing_file_is_err() {
let temp_dir = tempfile::TempDir::new().unwrap();
assert!(extract_dependencies(&temp_dir.path().join("Cargo.toml")).is_err());
}
}