Skip to content

Commit 67285f1

Browse files
committed
Enhance error messages
with rustc style
1 parent 4c39e5a commit 67285f1

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

src/lib.rs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern crate unicode_segmentation;
2727
use std::collections::HashMap;
2828
use std::fmt;
2929
use std::io::{self, stdout, Write};
30+
use std::iter::repeat;
3031
use std::ops::{Add, Sub};
3132
use std::path::{Path, PathBuf};
3233
use std::rc::Rc;
@@ -456,7 +457,7 @@ impl fmt::Display for ErrorKind {
456457
match *self {
457458
ErrorKind::LineOverflow(found, maximum) => write!(
458459
fmt,
459-
"line exceeded maximum length (maximum: {}, found: {})",
460+
"line exceeded maximum width (maximum: {}, found: {})",
460461
maximum,
461462
found
462463
),
@@ -477,16 +478,36 @@ pub struct FormattingError {
477478
impl FormattingError {
478479
fn msg_prefix(&self) -> &str {
479480
match self.kind {
480-
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => "Rustfmt failed at",
481+
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => "error:",
481482
ErrorKind::BadIssue(_) => "WARNING:",
482483
}
483484
}
484485

485-
fn msg_suffix(&self) -> &str {
486+
fn msg_suffix(&self) -> String {
486487
match self.kind {
488+
ErrorKind::LineOverflow(..) if self.is_comment => format!(
489+
"use `error_on_lineoverflow_comments = false` to suppress \
490+
the warning against line comments\n",
491+
),
487492
_ => String::from(""),
488493
}
489494
}
495+
496+
// (space, target)
497+
pub fn format_len(&self) -> (usize, usize) {
498+
match self.kind {
499+
ErrorKind::LineOverflow(found, max) => (max, found - max),
500+
ErrorKind::TrailingWhitespace => {
501+
let trailing_ws_len = self.line_buffer
502+
.chars()
503+
.rev()
504+
.take_while(|c| c.is_whitespace())
505+
.count();
506+
(self.line_buffer.len() - trailing_ws_len, trailing_ws_len)
507+
}
508+
_ => (0, 0), // unreachable
509+
}
510+
}
490511
}
491512

492513
pub struct FormatReport {
@@ -518,17 +539,50 @@ impl fmt::Display for FormatReport {
518539
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
519540
for (file, errors) in &self.file_error_map {
520541
for error in errors {
542+
let prefix_space_len = error.line.to_string().len();
543+
let prefix_spaces: String = repeat(" ").take(1 + prefix_space_len).collect();
544+
545+
let error_line_buffer = if error.line_buffer.is_empty() {
546+
String::from(" ")
547+
} else {
548+
let (space_len, target_len) = error.format_len();
549+
format!(
550+
"{}|\n{} | {}\n{}| {}",
551+
prefix_spaces,
552+
error.line,
553+
error.line_buffer,
554+
prefix_spaces,
555+
target_str(space_len, target_len)
556+
)
557+
};
558+
559+
let error_info = format!("{} {}", error.msg_prefix(), error.kind);
560+
let file_info = format!("{}--> {}:{}", &prefix_spaces[1..], file, error.line);
561+
let msg_suffix = error.msg_suffix();
562+
let note = if msg_suffix.is_empty() {
563+
String::new()
564+
} else {
565+
format!("{}note= ", prefix_spaces)
566+
};
567+
521568
write!(
522569
fmt,
523-
"{} {}:{}: {} {}\n",
524-
error.msg_prefix(),
525-
file,
526-
error.line,
527-
error.kind,
570+
"{}\n{}\n{}\n{}{}\n",
571+
error_info,
572+
file_info,
573+
error_line_buffer,
574+
note,
528575
error.msg_suffix()
529576
)?;
530577
}
531578
}
579+
if !self.file_error_map.is_empty() {
580+
write!(
581+
fmt,
582+
"warning: rustfmt may have failed to format. See previous {} errors.\n",
583+
self.warning_count(),
584+
)?;
585+
}
532586
Ok(())
533587
}
534588
}

0 commit comments

Comments
 (0)