diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2ef78b5..f715507 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,12 +7,13 @@ on: tags: - "*" pull_request: - branches: [ main ] + branches: [main] jobs: ci: + name: CI env: - RUST_BACKTRACE: 1 + RUST_BACKTRACE: 1 runs-on: ubuntu-latest strategy: matrix: @@ -36,6 +37,27 @@ jobs: - run: cargo test --workspace --verbose + autofix: + name: Autofix Tests + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + components: clippy, rustfmt + - run: cargo build --release + - run: cargo install --path . + - run: | + for dir in cli-tests/actual/*; do + if [ -d "$dir" ]; then + cd "$dir" && cargo machete --fix || true + fi + done + - run: cd cli-tests && diff -r expected actual + # Code mutably borrowed from https://github.com/EmbarkStudios/cargo-deny/, thanks Embark! release: name: Release diff --git a/cli-tests/actual/autofix-with-feature/Cargo.toml b/cli-tests/actual/autofix-with-feature/Cargo.toml new file mode 100644 index 0000000..2178fd6 --- /dev/null +++ b/cli-tests/actual/autofix-with-feature/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.14" + +[features] +default = ["std"] +std = ["log/std", "other"] +other = [] diff --git a/cli-tests/actual/autofix-with-feature/src/main.rs b/cli-tests/actual/autofix-with-feature/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/actual/autofix-with-feature/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/cli-tests/actual/feature-named-like-dep-2/Cargo.toml b/cli-tests/actual/feature-named-like-dep-2/Cargo.toml new file mode 100644 index 0000000..27e1088 --- /dev/null +++ b/cli-tests/actual/feature-named-like-dep-2/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.14" + +[features] +default = ["std"] +std = ["log/std", "other"] +dontlog = [] +log = [] +other = ["log", "dontlog"] diff --git a/cli-tests/actual/feature-named-like-dep-2/src/main.rs b/cli-tests/actual/feature-named-like-dep-2/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/actual/feature-named-like-dep-2/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/cli-tests/actual/feature-named-like-dep/Cargo.toml b/cli-tests/actual/feature-named-like-dep/Cargo.toml new file mode 100644 index 0000000..f722449 --- /dev/null +++ b/cli-tests/actual/feature-named-like-dep/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.14" + +[features] +default = ["std"] +std = ["log/std", "log"] +log = [] diff --git a/cli-tests/actual/feature-named-like-dep/src/main.rs b/cli-tests/actual/feature-named-like-dep/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/actual/feature-named-like-dep/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/cli-tests/expected/autofix-with-feature/Cargo.toml b/cli-tests/expected/autofix-with-feature/Cargo.toml new file mode 100644 index 0000000..f02faf0 --- /dev/null +++ b/cli-tests/expected/autofix-with-feature/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[features] +default = ["std"] +std = [ "other"] +other = [] diff --git a/cli-tests/expected/autofix-with-feature/src/main.rs b/cli-tests/expected/autofix-with-feature/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/expected/autofix-with-feature/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/cli-tests/expected/feature-named-like-dep-2/Cargo.toml b/cli-tests/expected/feature-named-like-dep-2/Cargo.toml new file mode 100644 index 0000000..27e1088 --- /dev/null +++ b/cli-tests/expected/feature-named-like-dep-2/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.14" + +[features] +default = ["std"] +std = ["log/std", "other"] +dontlog = [] +log = [] +other = ["log", "dontlog"] diff --git a/cli-tests/expected/feature-named-like-dep-2/src/main.rs b/cli-tests/expected/feature-named-like-dep-2/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/expected/feature-named-like-dep-2/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/cli-tests/expected/feature-named-like-dep/Cargo.toml b/cli-tests/expected/feature-named-like-dep/Cargo.toml new file mode 100644 index 0000000..f722449 --- /dev/null +++ b/cli-tests/expected/feature-named-like-dep/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "just-unused" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.14" + +[features] +default = ["std"] +std = ["log/std", "log"] +log = [] diff --git a/cli-tests/expected/feature-named-like-dep/src/main.rs b/cli-tests/expected/feature-named-like-dep/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/cli-tests/expected/feature-named-like-dep/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/main.rs b/src/main.rs index eb8352b..df451a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -280,6 +280,29 @@ fn remove_dependencies(manifest: &str, dependencies_list: &[String]) -> anyhow:: .with_context(|| format!("Dependency {k} not found"))?; } + let features = manifest + .iter_mut() + .find_map(|(k, v)| (v.is_table_like() && k == "features").then_some(Some(v))) + .flatten() + .and_then(|v| v.as_table_mut()); + + if let Some(features) = features { + for (_, deps) in features.iter_mut() { + if let Some(deps) = deps.as_array_mut() { + deps.retain(|dep| { + if let Some(dep) = dep.as_str() { + !dependencies_list.iter().any(|d| { + let prefix = d.to_string() + "/"; + dep.contains(&prefix) + }) + } else { + true + } + }); + } + } + } + let serialized = manifest.to_string(); Ok(serialized) }