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("//") {
) -> 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)
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 {
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)
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:",
}
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())
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
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,
}
}