Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit de0b661

Browse files
committed
cargo-fmt: Take into account the edition for each target
When formatting the crate, with `cargo fmt`, it parses each target with the specific Rust edition. Fixes: rust-lang#3104. Signed-off-by: Otavio Salvador <[email protected]>
1 parent e41fcb1 commit de0b661

File tree

1 file changed

+53
-55
lines changed

1 file changed

+53
-55
lines changed

src/cargo-fmt/main.rs

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ extern crate cargo_metadata;
1717
extern crate getopts;
1818
extern crate serde_json as json;
1919

20-
use std::collections::HashSet;
20+
use std::collections::{HashMap, HashSet};
2121
use std::env;
2222
use std::fs;
2323
use std::hash::{Hash, Hasher};
2424
use std::io::{self, Write};
2525
use std::iter::FromIterator;
2626
use std::path::{Path, PathBuf};
27-
use std::process::{Command, ExitStatus};
27+
use std::process::Command;
2828
use std::str;
2929

3030
use getopts::{Matches, Options};
@@ -122,30 +122,21 @@ pub enum Verbosity {
122122
Quiet,
123123
}
124124

125-
fn handle_command_status(status: Result<ExitStatus, io::Error>, opts: &getopts::Options) -> i32 {
125+
fn handle_command_status(status: Result<i32, io::Error>, opts: &getopts::Options) -> i32 {
126126
match status {
127127
Err(e) => {
128128
print_usage_to_stderr(opts, &e.to_string());
129129
FAILURE
130130
}
131-
Ok(status) => {
132-
if status.success() {
133-
SUCCESS
134-
} else {
135-
status.code().unwrap_or(FAILURE)
136-
}
137-
}
131+
Ok(status) => status,
138132
}
139133
}
140134

141-
fn get_version(verbosity: Verbosity) -> Result<ExitStatus, io::Error> {
142-
run_rustfmt(&[], &[String::from("--version")], verbosity)
135+
fn get_version(verbosity: Verbosity) -> Result<i32, io::Error> {
136+
run_rustfmt(&HashSet::new(), &[String::from("--version")], verbosity)
143137
}
144138

145-
fn format_crate(
146-
verbosity: Verbosity,
147-
strategy: &CargoFmtStrategy,
148-
) -> Result<ExitStatus, io::Error> {
139+
fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result<i32, io::Error> {
149140
let rustfmt_args = get_fmt_args();
150141
let targets = if rustfmt_args
151142
.iter()
@@ -157,17 +148,7 @@ fn format_crate(
157148
};
158149

159150
// Currently only bin and lib files get formatted
160-
let files: Vec<_> = targets
161-
.into_iter()
162-
.inspect(|t| {
163-
if verbosity == Verbosity::Verbose {
164-
println!("[{}] {:?}", t.kind, t.path)
165-
}
166-
})
167-
.map(|t| t.path)
168-
.collect();
169-
170-
run_rustfmt(&files, &rustfmt_args, verbosity)
151+
run_rustfmt(&targets, &rustfmt_args, verbosity)
171152
}
172153

173154
fn get_fmt_args() -> Vec<String> {
@@ -182,6 +163,8 @@ pub struct Target {
182163
path: PathBuf,
183164
/// A kind of target (e.g. lib, bin, example, ...).
184165
kind: String,
166+
/// Rust edition for this target.
167+
edition: String,
185168
}
186169

187170
impl Target {
@@ -192,6 +175,7 @@ impl Target {
192175
Target {
193176
path: canonicalized,
194177
kind: target.kind[0].clone(),
178+
edition: target.edition.clone(),
195179
}
196180
}
197181
}
@@ -334,41 +318,55 @@ fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet<Ta
334318
}
335319

336320
fn run_rustfmt(
337-
files: &[PathBuf],
321+
targets: &HashSet<Target>,
338322
fmt_args: &[String],
339323
verbosity: Verbosity,
340-
) -> Result<ExitStatus, io::Error> {
341-
let stdout = if verbosity == Verbosity::Quiet {
342-
std::process::Stdio::null()
343-
} else {
344-
std::process::Stdio::inherit()
345-
};
324+
) -> Result<i32, io::Error> {
325+
let by_edition: HashMap<_, _> = targets
326+
.iter()
327+
.inspect(|t| {
328+
if verbosity == Verbosity::Verbose {
329+
println!("[{} ({})] {:?}", t.kind, t.edition, t.path)
330+
}
331+
})
332+
.map(|t| (&t.edition, vec![&t.path]))
333+
.collect();
346334

347-
if verbosity == Verbosity::Verbose {
348-
print!("rustfmt");
349-
for a in fmt_args {
350-
print!(" {}", a);
335+
for (edition, files) in by_edition {
336+
let stdout = if verbosity == Verbosity::Quiet {
337+
std::process::Stdio::null()
338+
} else {
339+
std::process::Stdio::inherit()
340+
};
341+
342+
if verbosity == Verbosity::Verbose {
343+
print!("rustfmt");
344+
fmt_args.iter().for_each(|f| print!(" {}", f));
345+
files.iter().for_each(|f| print!(" {}", f.display()));
346+
println!();
351347
}
352-
for f in files {
353-
print!(" {}", f.display());
348+
349+
let mut command = Command::new("rustfmt")
350+
.stdout(stdout)
351+
.args(files)
352+
.args(&["--edition", edition])
353+
.args(fmt_args)
354+
.spawn()
355+
.map_err(|e| match e.kind() {
356+
io::ErrorKind::NotFound => io::Error::new(
357+
io::ErrorKind::Other,
358+
"Could not run rustfmt, please make sure it is in your PATH.",
359+
),
360+
_ => e,
361+
})?;
362+
363+
let status = command.wait()?;
364+
if !status.success() {
365+
return Ok(status.code().unwrap_or(FAILURE));
354366
}
355-
println!();
356367
}
357368

358-
let mut command = Command::new("rustfmt")
359-
.stdout(stdout)
360-
.args(files)
361-
.args(fmt_args)
362-
.spawn()
363-
.map_err(|e| match e.kind() {
364-
io::ErrorKind::NotFound => io::Error::new(
365-
io::ErrorKind::Other,
366-
"Could not run rustfmt, please make sure it is in your PATH.",
367-
),
368-
_ => e,
369-
})?;
370-
371-
command.wait()
369+
Ok(SUCCESS)
372370
}
373371

374372
fn get_cargo_metadata(manifest_path: Option<&Path>) -> Result<cargo_metadata::Metadata, io::Error> {

0 commit comments

Comments
 (0)