Skip to content

Commit

Permalink
Seemingly functional prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jan 3, 2025
1 parent 209c949 commit 949cf8f
Show file tree
Hide file tree
Showing 13 changed files with 1,092 additions and 16 deletions.
99 changes: 85 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cargo-platform = "0.1"
cargo-util = "0.2"
cargo-util-schemas = "0.7"
cargo_metadata = "0.19"
chrono = { version = "0.4", default-features = false }
clap = "4.5"
compiletest_rs = "0.11"
ctor = "0.2"
Expand All @@ -51,6 +52,7 @@ serde = "1.0"
serde-untagged = "0.1"
serde_json = "1.0"
similar-asserts = "1.6"
syntect = { version = "5.2", default-features = false }
tempfile = "3.14"
thiserror = "2.0"
toml = "0.8"
Expand Down
11 changes: 11 additions & 0 deletions cargo-dylint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ Combine with `--all` to list all lints in all discovered libraries."

#[clap(help = "Path to library package")]
path: String,

#[clap(
help_heading = Some("Experimental"),
long,
help = "Try to extract fixes from Clippy repository commits whose date is that of the \
un-upgraded toolchain or later",
default_value = "false",
)]
auto_correct: bool,
},
}

Expand Down Expand Up @@ -278,9 +287,11 @@ impl From<Dylint> for dylint::opts::Dylint {
allow_downgrade,
rust_version,
path,
auto_correct,
}) => dylint::opts::Operation::Upgrade(dylint::opts::Upgrade {
allow_downgrade,
rust_version,
auto_correct,
path,
}),
};
Expand Down
64 changes: 62 additions & 2 deletions cargo-dylint/tests/integration/package_options.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use anyhow::{anyhow, Context, Result};
use assert_cmd::prelude::*;
use cargo_metadata::{Dependency, MetadataCommand};
use dylint_internal::{rustup::SanitizeEnvironment, CommandExt};
use dylint_internal::{clone, env::enabled, rustup::SanitizeEnvironment, CommandExt};
use predicates::prelude::*;
use regex::Regex;
use semver::Version;
use std::{fs::read_to_string, path::Path};
use std::{
fs::read_to_string,
io::{stderr, Write},
path::Path,
};
use tempfile::tempdir;

// smoelius: I expected `git2-0.17.2` to build with nightly-2022-06-30, which corresponds to
Expand Down Expand Up @@ -139,6 +143,62 @@ fn downgrade_upgrade_package() {
.unwrap();
}

const DYLINT_URL: &str = "https://github.com/trailofbits/dylint";

// smoelius: Each of the following commits is just before an "Upgrade examples" commit. In the
// upgrades, the changes to the `restriction` lints are small. Thus, the auto-correct option should
// be able to generate fixes for them.
const REVS_AND_RUST_VERSIONS: &[(&str, &str)] = &[
// smoelius: "Upgrade examples" commit:
// https://github.com/trailofbits/dylint/commit/33969746aef6947c68d7adb55137ce8a13d9cc47
("5b3792515ac255fdb06a31b10eb3c9f7949a3ed5", "1.80.0"),
// smoelius: "Upgrade examples" commit:
// https://github.com/trailofbits/dylint/commit/7bc453f0778dee3b13bc1063773774304ac96cad
("23c08c8a0b043d26f66653bf173a0e6722a2d699", "1.79.0"),
];

#[test]
fn upgrade_with_auto_correct() {
for (rev, rust_version) in REVS_AND_RUST_VERSIONS {
let tempdir = tempdir().unwrap();

clone(DYLINT_URL, rev, tempdir.path(), false).unwrap();

let mut command = std::process::Command::cargo_bin("cargo-dylint").unwrap();
command.args([
"dylint",
"upgrade",
&tempdir
.path()
.join("examples/restriction")
.to_string_lossy(),
"--auto-correct",
"--rust-version",
rust_version,
]);
command.success().unwrap();

if enabled("DEBUG_DIFF") {
let mut command = std::process::Command::new("git");
command.current_dir(&tempdir);
command.args(["--no-pager", "diff", "--", "*.rs"]);
command.success().unwrap();
}

dylint_internal::cargo::check("auto-corrected, upgraded library package")
.build()
.sanitize_environment()
.current_dir(tempdir.path().join("examples/restriction"))
.env("RUSTFLAGS", "--allow=warnings")
.arg("--quiet")
.success()
.unwrap();

#[allow(clippy::explicit_write)]
writeln!(stderr(), "Success").unwrap();
}
}

#[allow(dead_code)]
fn rust_version(path: &Path) -> Result<Version> {
let re = Regex::new(r#"^clippy_utils = .*\btag = "rust-([^"]*)""#).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions dylint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cargo-platform = { workspace = true, optional = true }
cargo-util = { workspace = true, optional = true }
cargo-util-schemas = { workspace = true, optional = true }
cargo_metadata = { workspace = true }
chrono = { workspace = true, optional = true }
dirs = { workspace = true }
dunce = { workspace = true, optional = true }
fs_extra = { workspace = true, optional = true }
Expand All @@ -32,6 +33,12 @@ semver = { workspace = true }
serde = { workspace = true }
serde-untagged = { workspace = true, optional = true }
serde_json = { workspace = true }
# smoelius: `syntect` can tokenize incomplete Rust fragments, e.g., code with unbalanced delimiters.
syntect = { workspace = true, features = [
"default-syntaxes",
"regex-fancy",
"parsing",
], optional = true }
tempfile = { workspace = true }
toml = { workspace = true, optional = true }
url = { workspace = true, optional = true }
Expand Down Expand Up @@ -61,11 +68,13 @@ dylint_internal = { version = "=3.3.0", path = "../internal", features = [
default = []
library_packages = ["__cargo_cli"]
package_options = [
"chrono",
"dylint_internal/clippy_utils",
"dylint_internal/git",
"heck",
"if_chain",
"rewriter",
"syntect",
]
__cargo_cli = [
"cargo-util",
Expand Down
Loading

0 comments on commit 949cf8f

Please sign in to comment.