Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-correct #1456

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bitflags v1.3.2
bitflags v2.6.0
thiserror v1.0.69
thiserror v2.0.9
thiserror-impl v1.0.69
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bitflags v1.3.2
bitflags v2.6.0
thiserror v1.0.69
thiserror v2.0.9
thiserror-impl v1.0.69
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bitflags v1.3.2
bitflags v2.6.0
thiserror v1.0.69
thiserror v2.0.9
thiserror-impl v1.0.69
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bitflags v1.3.2
bitflags v2.6.0
thiserror v1.0.69
thiserror v2.0.9
thiserror-impl v1.0.69
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
Loading
Loading