Skip to content
This repository was archived by the owner on Dec 17, 2022. It is now read-only.

Commit a749b15

Browse files
committed
fix: ensure errors do not return an empty format
1 parent 7d6c728 commit a749b15

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/format.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::configuration::Configuration;
22

3-
use anyhow::Result;
3+
use anyhow::{bail, Result};
44
use rustfmt_nightly::{Input, Session};
55

66
pub fn format_text(file_text: &str, config: &Configuration) -> Result<String> {
@@ -9,16 +9,18 @@ pub fn format_text(file_text: &str, config: &Configuration) -> Result<String> {
99
let input = Input::Text(String::from(file_text));
1010
let mut session = Session::new(config.rustfmt_config.clone(), Some(&mut out));
1111
session.format(input)?;
12+
13+
if !session.has_no_errors() {
14+
bail!("Rustfmt had errors.");
15+
}
1216
}
1317

1418
let text = std::str::from_utf8(&out)?;
1519
// rustfmt adds this prefix, so just ignore it
16-
Ok(
17-
text
18-
.trim_start_matches("<stdin>:\n\n")
19-
.trim_start_matches("stdin:\n\n")
20-
.to_string(),
21-
)
20+
let text = text
21+
.trim_start_matches("<stdin>:\n\n")
22+
.trim_start_matches("stdin:\n\n");
23+
Ok(text.to_string())
2224
}
2325

2426
#[cfg(test)]
@@ -35,5 +37,12 @@ mod test {
3537
let config = resolve_config(Default::default(), &global_config).config;
3638
assert_eq!(format_text("use test;", &config).unwrap(), "use test;\n");
3739
assert_eq!(format_text("use test;\n", &config).unwrap(), "use test;\n");
40+
assert_eq!(
41+
format_text("let test = ...;", &config)
42+
.err()
43+
.unwrap()
44+
.to_string(),
45+
"Rustfmt had errors."
46+
);
3847
}
3948
}

0 commit comments

Comments
 (0)