Skip to content

Commit f58e118

Browse files
force edition 2024 lint into action on crater
1 parent 8995f54 commit f58e118

File tree

3 files changed

+106
-153
lines changed

3 files changed

+106
-153
lines changed

Diff for: src/bin/cargo/commands/check.rs

+52-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::command_prelude::*;
2-
31
use cargo::ops;
42

3+
use crate::command_prelude::*;
4+
55
pub fn cli() -> Command {
66
subcommand("check")
77
// subcommand aliases are handled in aliased_command()
@@ -44,16 +44,62 @@ pub fn cli() -> Command {
4444
}
4545

4646
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
47+
if std::env::var("CARGO_REAL_CHECK").is_err() {
48+
fixit()?;
49+
return Ok(());
50+
}
4751
let ws = args.workspace(gctx)?;
4852
// This is a legacy behavior that causes `cargo check` to pass `--test`.
49-
let test = matches!(
50-
args.get_one::<String>("profile").map(String::as_str),
51-
Some("test")
52-
);
53+
let test = matches!(args.get_one::<String>("profile").map(String::as_str), Some("test"));
5354
let mode = CompileMode::Check { test };
5455
let compile_opts =
5556
args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
5657

5758
ops::compile(&ws, &compile_opts)?;
5859
Ok(())
5960
}
61+
62+
fn fixit() -> CliResult {
63+
use std::path::Path;
64+
65+
use anyhow::Context;
66+
use cargo_util::{paths, ProcessBuilder};
67+
68+
eprintln!("Copying to /tmp/fixit");
69+
ProcessBuilder::new("cp").args(&["-a", ".", "/tmp/fixit"]).exec()?;
70+
std::env::set_current_dir("/tmp/fixit").map_err(|e| anyhow::format_err!("cd failed {}", e))?;
71+
72+
let ed_re = regex::Regex::new(r#"(?m)^ *edition *= *['"]([^'"]+)['"]"#).unwrap();
73+
let manifest = paths::read(Path::new("Cargo.toml"))?;
74+
let ed_cap = match ed_re.captures(&manifest) {
75+
None => {
76+
eprintln!("no edition found in manifest, probably 2015, skipping");
77+
return Ok(());
78+
}
79+
Some(caps) => caps.get(1).unwrap(),
80+
};
81+
if ed_cap.as_str() != "2021" {
82+
eprintln!("skipping non-2021 edition `{}`", ed_cap.as_str());
83+
return Ok(());
84+
}
85+
eprintln!("Running `cargo fix --edition`");
86+
// Skip "cargo check"
87+
let args: Vec<_> = std::env::args().skip(2).collect();
88+
ProcessBuilder::new("cargo")
89+
.args(&["fix", "--edition", "--allow-no-vcs", "--allow-dirty"])
90+
.args(&args)
91+
.exec()
92+
.with_context(|| "failed to migrate to next edition")?;
93+
let mut manifest = paths::read(Path::new("Cargo.toml"))?;
94+
let ed_cap = ed_re.captures(&manifest).unwrap().get(1).unwrap();
95+
manifest.replace_range(ed_cap.range(), "2024");
96+
paths::write("Cargo.toml", manifest)?;
97+
eprintln!("Running `cargo check` to verify 2024");
98+
ProcessBuilder::new("cargo")
99+
.args(&["check"])
100+
.args(&args)
101+
.env("CARGO_REAL_CHECK", "1")
102+
.exec()
103+
.with_context(|| "failed to check after updating to 2024")?;
104+
Ok(())
105+
}

0 commit comments

Comments
 (0)