Skip to content

Commit 1452349

Browse files
committed
Exit with 1 for license check in --check mode
Closes #2707
1 parent bf2581b commit 1452349

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

src/bin/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ fn main() {
3535

3636
let exit_code = match execute(&opts) {
3737
Ok((write_mode, summary)) => {
38-
if summary.has_operational_errors()
39-
|| summary.has_parsing_errors()
40-
|| (summary.has_diff && write_mode == WriteMode::Check)
38+
if summary.has_operational_errors() || summary.has_parsing_errors()
39+
|| ((summary.has_diff || summary.has_check_errors())
40+
&& write_mode == WriteMode::Check)
4141
{
4242
1
4343
} else {

src/config/summary.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub struct Summary {
2323
// Code is valid, but it is impossible to format it properly.
2424
has_formatting_errors: bool,
2525

26+
// Failed a check, such as the license check or other opt-in checking.
27+
has_check_errors: bool,
28+
2629
// Formatted code differs from existing code (--check only).
2730
pub has_diff: bool,
2831

@@ -73,6 +76,10 @@ impl Summary {
7376
self.has_formatting_errors
7477
}
7578

79+
pub fn has_check_errors(&self) -> bool {
80+
self.has_check_errors
81+
}
82+
7683
pub fn add_operational_error(&mut self) {
7784
self.has_operational_errors = true;
7885
}
@@ -85,6 +92,10 @@ impl Summary {
8592
self.has_formatting_errors = true;
8693
}
8794

95+
pub(crate) fn add_check_error(&mut self) {
96+
self.has_check_errors = true;
97+
}
98+
8899
pub(crate) fn add_diff(&mut self) {
89100
self.has_diff = true;
90101
}
@@ -100,6 +111,7 @@ impl Summary {
100111
self.has_operational_errors |= other.has_operational_errors;
101112
self.has_formatting_errors |= other.has_formatting_errors;
102113
self.has_parsing_errors |= other.has_parsing_errors;
114+
self.has_check_errors |= other.has_check_errors;
103115
self.has_diff |= other.has_diff;
104116
}
105117
}

src/lib.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,27 +200,58 @@ impl FormattingError {
200200
#[derive(Clone)]
201201
pub struct FormatReport {
202202
// Maps stringified file paths to their associated formatting errors.
203-
file_error_map: Rc<RefCell<HashMap<FileName, Vec<FormattingError>>>>,
203+
internal: Rc<RefCell<(FormatErrorMap, ReportedErrors)>>,
204+
}
205+
206+
type FormatErrorMap = HashMap<FileName, Vec<FormattingError>>;
207+
208+
#[derive(Default, Debug)]
209+
struct ReportedErrors {
210+
has_operational_errors: bool,
211+
has_check_errors: bool,
204212
}
205213

206214
impl FormatReport {
207215
fn new() -> FormatReport {
208216
FormatReport {
209-
file_error_map: Rc::new(RefCell::new(HashMap::new())),
217+
internal: Rc::new(RefCell::new((HashMap::new(), ReportedErrors::default()))),
210218
}
211219
}
212220

213221
fn append(&self, f: FileName, mut v: Vec<FormattingError>) {
214-
self.file_error_map
222+
self.track_errors(&v);
223+
self.internal
215224
.borrow_mut()
225+
.0
216226
.entry(f)
217227
.and_modify(|fe| fe.append(&mut v))
218228
.or_insert(v);
219229
}
220230

231+
fn track_errors(&self, new_errors: &[FormattingError]) {
232+
let errs = &mut self.internal.borrow_mut().1;
233+
if errs.has_operational_errors && errs.has_check_errors {
234+
return;
235+
}
236+
for err in new_errors {
237+
match err.kind {
238+
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => {
239+
errs.has_operational_errors = true;
240+
}
241+
ErrorKind::BadIssue(_)
242+
| ErrorKind::LicenseCheck
243+
| ErrorKind::DeprecatedAttr
244+
| ErrorKind::BadAttr => {
245+
errs.has_check_errors = true;
246+
}
247+
}
248+
}
249+
}
250+
221251
fn warning_count(&self) -> usize {
222-
self.file_error_map
252+
self.internal
223253
.borrow()
254+
.0
224255
.iter()
225256
.map(|(_, errors)| errors.len())
226257
.sum()
@@ -234,7 +265,7 @@ impl FormatReport {
234265
&self,
235266
mut t: Box<term::Terminal<Output = io::Stderr>>,
236267
) -> Result<(), term::Error> {
237-
for (file, errors) in &*self.file_error_map.borrow() {
268+
for (file, errors) in &self.internal.borrow().0 {
238269
for error in errors {
239270
let prefix_space_len = error.line.to_string().len();
240271
let prefix_spaces = " ".repeat(1 + prefix_space_len);
@@ -280,7 +311,7 @@ impl FormatReport {
280311
}
281312
}
282313

283-
if !self.file_error_map.borrow().is_empty() {
314+
if !self.internal.borrow().0.is_empty() {
284315
t.attr(term::Attr::Bold)?;
285316
write!(t, "warning: ")?;
286317
t.reset()?;
@@ -304,7 +335,7 @@ fn target_str(space_len: usize, target_len: usize) -> String {
304335
impl fmt::Display for FormatReport {
305336
// Prints all the formatting errors.
306337
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
307-
for (file, errors) in &*self.file_error_map.borrow() {
338+
for (file, errors) in &self.internal.borrow().0 {
308339
for error in errors {
309340
let prefix_space_len = error.line.to_string().len();
310341
let prefix_spaces = " ".repeat(1 + prefix_space_len);
@@ -343,7 +374,7 @@ impl fmt::Display for FormatReport {
343374
)?;
344375
}
345376
}
346-
if !self.file_error_map.borrow().is_empty() {
377+
if !self.internal.borrow().0.is_empty() {
347378
writeln!(
348379
fmt,
349380
"warning: rustfmt may have failed to format. See previous {} errors.",
@@ -827,6 +858,16 @@ fn format_input_inner<T: Write>(
827858
)
828859
});
829860

861+
{
862+
let report_errs = &report.internal.borrow().1;
863+
if report_errs.has_check_errors {
864+
summary.add_check_error();
865+
}
866+
if report_errs.has_operational_errors {
867+
summary.add_operational_error();
868+
}
869+
}
870+
830871
match format_result {
831872
Ok((file_map, has_diff)) => {
832873
if report.has_warnings() {

0 commit comments

Comments
 (0)