Skip to content

Commit 949cf8f

Browse files
committed
Seemingly functional prototype
1 parent 209c949 commit 949cf8f

13 files changed

Lines changed: 1092 additions & 16 deletions

File tree

Cargo.lock

Lines changed: 85 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cargo-platform = "0.1"
2525
cargo-util = "0.2"
2626
cargo-util-schemas = "0.7"
2727
cargo_metadata = "0.19"
28+
chrono = { version = "0.4", default-features = false }
2829
clap = "4.5"
2930
compiletest_rs = "0.11"
3031
ctor = "0.2"
@@ -51,6 +52,7 @@ serde = "1.0"
5152
serde-untagged = "0.1"
5253
serde_json = "1.0"
5354
similar-asserts = "1.6"
55+
syntect = { version = "5.2", default-features = false }
5456
tempfile = "3.14"
5557
thiserror = "2.0"
5658
toml = "0.8"

cargo-dylint/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ Combine with `--all` to list all lints in all discovered libraries."
126126

127127
#[clap(help = "Path to library package")]
128128
path: String,
129+
130+
#[clap(
131+
help_heading = Some("Experimental"),
132+
long,
133+
help = "Try to extract fixes from Clippy repository commits whose date is that of the \
134+
un-upgraded toolchain or later",
135+
default_value = "false",
136+
)]
137+
auto_correct: bool,
129138
},
130139
}
131140

@@ -278,9 +287,11 @@ impl From<Dylint> for dylint::opts::Dylint {
278287
allow_downgrade,
279288
rust_version,
280289
path,
290+
auto_correct,
281291
}) => dylint::opts::Operation::Upgrade(dylint::opts::Upgrade {
282292
allow_downgrade,
283293
rust_version,
294+
auto_correct,
284295
path,
285296
}),
286297
};

cargo-dylint/tests/integration/package_options.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use anyhow::{anyhow, Context, Result};
22
use assert_cmd::prelude::*;
33
use cargo_metadata::{Dependency, MetadataCommand};
4-
use dylint_internal::{rustup::SanitizeEnvironment, CommandExt};
4+
use dylint_internal::{clone, env::enabled, rustup::SanitizeEnvironment, CommandExt};
55
use predicates::prelude::*;
66
use regex::Regex;
77
use semver::Version;
8-
use std::{fs::read_to_string, path::Path};
8+
use std::{
9+
fs::read_to_string,
10+
io::{stderr, Write},
11+
path::Path,
12+
};
913
use tempfile::tempdir;
1014

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

146+
const DYLINT_URL: &str = "https://github.com/trailofbits/dylint";
147+
148+
// smoelius: Each of the following commits is just before an "Upgrade examples" commit. In the
149+
// upgrades, the changes to the `restriction` lints are small. Thus, the auto-correct option should
150+
// be able to generate fixes for them.
151+
const REVS_AND_RUST_VERSIONS: &[(&str, &str)] = &[
152+
// smoelius: "Upgrade examples" commit:
153+
// https://github.com/trailofbits/dylint/commit/33969746aef6947c68d7adb55137ce8a13d9cc47
154+
("5b3792515ac255fdb06a31b10eb3c9f7949a3ed5", "1.80.0"),
155+
// smoelius: "Upgrade examples" commit:
156+
// https://github.com/trailofbits/dylint/commit/7bc453f0778dee3b13bc1063773774304ac96cad
157+
("23c08c8a0b043d26f66653bf173a0e6722a2d699", "1.79.0"),
158+
];
159+
160+
#[test]
161+
fn upgrade_with_auto_correct() {
162+
for (rev, rust_version) in REVS_AND_RUST_VERSIONS {
163+
let tempdir = tempdir().unwrap();
164+
165+
clone(DYLINT_URL, rev, tempdir.path(), false).unwrap();
166+
167+
let mut command = std::process::Command::cargo_bin("cargo-dylint").unwrap();
168+
command.args([
169+
"dylint",
170+
"upgrade",
171+
&tempdir
172+
.path()
173+
.join("examples/restriction")
174+
.to_string_lossy(),
175+
"--auto-correct",
176+
"--rust-version",
177+
rust_version,
178+
]);
179+
command.success().unwrap();
180+
181+
if enabled("DEBUG_DIFF") {
182+
let mut command = std::process::Command::new("git");
183+
command.current_dir(&tempdir);
184+
command.args(["--no-pager", "diff", "--", "*.rs"]);
185+
command.success().unwrap();
186+
}
187+
188+
dylint_internal::cargo::check("auto-corrected, upgraded library package")
189+
.build()
190+
.sanitize_environment()
191+
.current_dir(tempdir.path().join("examples/restriction"))
192+
.env("RUSTFLAGS", "--allow=warnings")
193+
.arg("--quiet")
194+
.success()
195+
.unwrap();
196+
197+
#[allow(clippy::explicit_write)]
198+
writeln!(stderr(), "Success").unwrap();
199+
}
200+
}
201+
142202
#[allow(dead_code)]
143203
fn rust_version(path: &Path) -> Result<Version> {
144204
let re = Regex::new(r#"^clippy_utils = .*\btag = "rust-([^"]*)""#).unwrap();

dylint/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cargo-platform = { workspace = true, optional = true }
1717
cargo-util = { workspace = true, optional = true }
1818
cargo-util-schemas = { workspace = true, optional = true }
1919
cargo_metadata = { workspace = true }
20+
chrono = { workspace = true, optional = true }
2021
dirs = { workspace = true }
2122
dunce = { workspace = true, optional = true }
2223
fs_extra = { workspace = true, optional = true }
@@ -32,6 +33,12 @@ semver = { workspace = true }
3233
serde = { workspace = true }
3334
serde-untagged = { workspace = true, optional = true }
3435
serde_json = { workspace = true }
36+
# smoelius: `syntect` can tokenize incomplete Rust fragments, e.g., code with unbalanced delimiters.
37+
syntect = { workspace = true, features = [
38+
"default-syntaxes",
39+
"regex-fancy",
40+
"parsing",
41+
], optional = true }
3542
tempfile = { workspace = true }
3643
toml = { workspace = true, optional = true }
3744
url = { workspace = true, optional = true }
@@ -61,11 +68,13 @@ dylint_internal = { version = "=3.3.0", path = "../internal", features = [
6168
default = []
6269
library_packages = ["__cargo_cli"]
6370
package_options = [
71+
"chrono",
6472
"dylint_internal/clippy_utils",
6573
"dylint_internal/git",
6674
"heck",
6775
"if_chain",
6876
"rewriter",
77+
"syntect",
6978
]
7079
__cargo_cli = [
7180
"cargo-util",

0 commit comments

Comments
 (0)