Trigger an internal error if we skip formatting due to a lost comment
This commit is contained in:
parent
6899471497
commit
b085113cbe
2 changed files with 34 additions and 7 deletions
|
@ -20,6 +20,7 @@ use rewrite::RewriteContext;
|
|||
use shape::{Indent, Shape};
|
||||
use string::{rewrite_string, StringFormat};
|
||||
use utils::{count_newlines, first_line_width, last_line_width};
|
||||
use {ErrorKind, FormattingError};
|
||||
|
||||
fn is_custom_comment(comment: &str) -> bool {
|
||||
if !comment.starts_with("//") {
|
||||
|
@ -1124,7 +1125,17 @@ pub fn recover_comment_removed(
|
|||
) -> Option<String> {
|
||||
let snippet = context.snippet(span);
|
||||
if snippet != new && changed_comment_content(snippet, &new) {
|
||||
// We missed some comments. Keep the original text.
|
||||
// We missed some comments. Warn and keep the original text.
|
||||
if context.config.error_on_unformatted() {
|
||||
context.report.append(
|
||||
context.codemap.span_to_filename(span).into(),
|
||||
vec![FormattingError::from_span(
|
||||
&span,
|
||||
&context.codemap,
|
||||
ErrorKind::LostComment,
|
||||
)],
|
||||
);
|
||||
}
|
||||
Some(snippet.to_owned())
|
||||
} else {
|
||||
Some(new)
|
||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -140,8 +140,20 @@ pub enum ErrorKind {
|
|||
ParseError,
|
||||
/// The user mandated a version and the current version of Rustfmt does not
|
||||
/// satisfy that requirement.
|
||||
#[fail(display = "Version mismatch")]
|
||||
#[fail(display = "version mismatch")]
|
||||
VersionMismatch,
|
||||
/// If we had formatted the given node, then we would have lost a comment.
|
||||
#[fail(display = "not formatted because a comment would be lost")]
|
||||
LostComment,
|
||||
}
|
||||
|
||||
impl ErrorKind {
|
||||
fn is_comment(&self) -> bool {
|
||||
match self {
|
||||
ErrorKind::LostComment => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for ErrorKind {
|
||||
|
@ -162,8 +174,8 @@ impl FormattingError {
|
|||
fn from_span(span: &Span, codemap: &CodeMap, kind: ErrorKind) -> FormattingError {
|
||||
FormattingError {
|
||||
line: codemap.lookup_char_pos(span.lo()).line,
|
||||
is_comment: kind.is_comment(),
|
||||
kind,
|
||||
is_comment: false,
|
||||
is_string: false,
|
||||
line_buffer: codemap
|
||||
.span_to_lines(*span)
|
||||
|
@ -181,7 +193,8 @@ impl FormattingError {
|
|||
ErrorKind::LineOverflow(..)
|
||||
| ErrorKind::TrailingWhitespace
|
||||
| ErrorKind::IoError(_)
|
||||
| ErrorKind::ParseError => "internal error:",
|
||||
| ErrorKind::ParseError
|
||||
| ErrorKind::LostComment => "internal error:",
|
||||
ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:",
|
||||
ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
|
||||
}
|
||||
|
@ -200,7 +213,10 @@ impl FormattingError {
|
|||
fn format_len(&self) -> (usize, usize) {
|
||||
match self.kind {
|
||||
ErrorKind::LineOverflow(found, max) => (max, found - max),
|
||||
ErrorKind::TrailingWhitespace | ErrorKind::DeprecatedAttr | ErrorKind::BadAttr => {
|
||||
ErrorKind::TrailingWhitespace
|
||||
| ErrorKind::DeprecatedAttr
|
||||
| ErrorKind::BadAttr
|
||||
| ErrorKind::LostComment => {
|
||||
let trailing_ws_start = self
|
||||
.line_buffer
|
||||
.rfind(|c: char| !c.is_whitespace())
|
||||
|
@ -501,7 +517,7 @@ fn should_report_error(
|
|||
is_string: bool,
|
||||
error_kind: &ErrorKind,
|
||||
) -> bool {
|
||||
let allow_error_report = if char_kind.is_comment() || is_string {
|
||||
let allow_error_report = if char_kind.is_comment() || is_string || error_kind.is_comment() {
|
||||
config.error_on_unformatted()
|
||||
} else {
|
||||
true
|
||||
|
@ -509,7 +525,7 @@ fn should_report_error(
|
|||
|
||||
match error_kind {
|
||||
ErrorKind::LineOverflow(..) => config.error_on_line_overflow() && allow_error_report,
|
||||
ErrorKind::TrailingWhitespace => allow_error_report,
|
||||
ErrorKind::TrailingWhitespace | ErrorKind::LostComment => allow_error_report,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue