1 #![deny(rust_2018_idioms)]
2 #![warn(unreachable_pub)]
5 extern crate derive_new;
8 extern crate lazy_static;
12 use std::cell::RefCell;
13 use std::collections::HashMap;
15 use std::io::{self, Write};
18 use std::path::PathBuf;
23 use syntax::{ast, parse::DirectoryOwnership};
25 use crate::comment::LineClasses;
26 use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
27 use crate::issues::Issue;
28 use crate::shape::Indent;
29 use crate::utils::indent_next_line;
31 pub use crate::config::{
32 load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
36 pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};
38 pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
45 pub(crate) mod checkstyle;
48 pub(crate) mod config;
50 mod format_report_formatter;
51 pub(crate) mod formatting;
60 pub(crate) mod modules;
66 pub(crate) mod rustfmt_diff;
68 pub(crate) mod source_file;
69 pub(crate) mod source_map;
76 pub(crate) mod visitor;
78 /// The various errors that can occur during formatting. Note that not all of
79 /// these can currently be propagated to clients.
80 #[derive(Fail, Debug)]
82 /// Line has exceeded character limit (found, maximum).
84 display = "line formatted, but exceeded maximum width \
85 (maximum: {} (see `max_width` option), found: {})",
88 LineOverflow(usize, usize),
89 /// Line ends in whitespace.
90 #[fail(display = "left behind trailing whitespace")]
92 /// TODO or FIXME item without an issue number.
93 #[fail(display = "found {}", _0)]
95 /// License check has failed.
96 #[fail(display = "license check failed")]
98 /// Used deprecated skip attribute.
99 #[fail(display = "`rustfmt_skip` is deprecated; use `rustfmt::skip`")]
101 /// Used a rustfmt:: attribute other than skip or skip::macros.
102 #[fail(display = "invalid attribute")]
104 /// An io error during reading or writing.
105 #[fail(display = "io error: {}", _0)]
107 /// Parse error occurred when parsing the input.
108 #[fail(display = "parse error")]
110 /// The user mandated a version and the current version of Rustfmt does not
111 /// satisfy that requirement.
112 #[fail(display = "version mismatch")]
114 /// If we had formatted the given node, then we would have lost a comment.
115 #[fail(display = "not formatted because a comment would be lost")]
117 /// Invalid glob pattern in `ignore` configuration option.
118 #[fail(display = "Invalid glob pattern found in ignore list: {}", _0)]
119 InvalidGlobPattern(ignore::Error),
123 fn is_comment(&self) -> bool {
125 ErrorKind::LostComment => true,
131 impl From<io::Error> for ErrorKind {
132 fn from(e: io::Error) -> ErrorKind {
133 ErrorKind::IoError(e)
137 /// Result of formatting a snippet of code along with ranges of lines that didn't get formatted,
138 /// i.e., that got returned as they were originally.
140 struct FormattedSnippet {
142 non_formatted_ranges: Vec<(usize, usize)>,
145 impl FormattedSnippet {
146 /// In case the snippet needed to be wrapped in a function, this shifts down the ranges of
147 /// non-formatted code.
148 fn unwrap_code_block(&mut self) {
149 self.non_formatted_ranges
151 .for_each(|(low, high)| {
157 /// Returns `true` if the line n did not get formatted.
158 fn is_line_non_formatted(&self, n: usize) -> bool {
159 self.non_formatted_ranges
161 .any(|(low, high)| *low <= n && n <= *high)
165 /// Reports on any issues that occurred during a run of Rustfmt.
167 /// Can be reported to the user using the `Display` impl on [`FormatReportFormatter`].
169 pub struct FormatReport {
170 // Maps stringified file paths to their associated formatting errors.
171 internal: Rc<RefCell<(FormatErrorMap, ReportedErrors)>>,
172 non_formatted_ranges: Vec<(usize, usize)>,
176 fn new() -> FormatReport {
178 internal: Rc::new(RefCell::new((HashMap::new(), ReportedErrors::default()))),
179 non_formatted_ranges: Vec::new(),
183 fn add_non_formatted_ranges(&mut self, mut ranges: Vec<(usize, usize)>) {
184 self.non_formatted_ranges.append(&mut ranges);
187 fn append(&self, f: FileName, mut v: Vec<FormattingError>) {
188 self.track_errors(&v);
193 .and_modify(|fe| fe.append(&mut v))
197 fn track_errors(&self, new_errors: &[FormattingError]) {
198 let errs = &mut self.internal.borrow_mut().1;
199 if !new_errors.is_empty() {
200 errs.has_formatting_errors = true;
202 if errs.has_operational_errors && errs.has_check_errors {
205 for err in new_errors {
207 ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => {
208 errs.has_operational_errors = true;
210 ErrorKind::BadIssue(_)
211 | ErrorKind::LicenseCheck
212 | ErrorKind::DeprecatedAttr
214 | ErrorKind::VersionMismatch => {
215 errs.has_check_errors = true;
222 fn add_diff(&mut self) {
223 self.internal.borrow_mut().1.has_diff = true;
226 fn add_macro_format_failure(&mut self) {
227 self.internal.borrow_mut().1.has_macro_format_failure = true;
230 fn add_parsing_error(&mut self) {
231 self.internal.borrow_mut().1.has_parsing_errors = true;
234 fn warning_count(&self) -> usize {
239 .map(|(_, errors)| errors.len())
243 /// Whether any warnings or errors are present in the report.
244 pub fn has_warnings(&self) -> bool {
245 self.internal.borrow().1.has_formatting_errors
248 /// Print the report to a terminal using colours and potentially other
250 #[deprecated(note = "Use FormatReportFormatter with colors enabled instead")]
253 mut t: Box<dyn term::Terminal<Output = io::Stderr>>,
254 ) -> Result<(), term::Error> {
258 FormatReportFormatterBuilder::new(&self)
266 #[deprecated(note = "Use FormatReportFormatter instead")]
267 impl fmt::Display for FormatReport {
268 // Prints all the formatting errors.
269 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
270 write!(fmt, "{}", FormatReportFormatterBuilder::new(&self).build())?;
275 /// Format the given snippet. The snippet is expected to be *complete* code.
276 /// When we cannot parse the given snippet, this function returns `None`.
277 fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
278 let mut config = config.clone();
279 panic::catch_unwind(|| {
280 let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
281 config.set().emit_mode(config::EmitMode::Stdout);
282 config.set().verbose(Verbosity::Quiet);
283 config.set().hide_parse_errors(true);
285 let (formatting_error, result) = {
286 let input = Input::Text(snippet.into());
287 let mut session = Session::new(config, Some(&mut out));
288 let result = session.format(input);
290 session.errors.has_macro_format_failure
291 || session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
296 if formatting_error {
299 String::from_utf8(out).ok().map(|snippet| FormattedSnippet {
301 non_formatted_ranges: result.unwrap().non_formatted_ranges,
305 // Discard panics encountered while formatting the snippet
306 // The ? operator is needed to remove the extra Option
310 /// Format the given code block. Mainly targeted for code block in comment.
311 /// The code block may be incomplete (i.e., parser may be unable to parse it).
312 /// To avoid panic in parser, we wrap the code block with a dummy function.
313 /// The returned code block does **not** end with newline.
314 fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSnippet> {
315 const FN_MAIN_PREFIX: &str = "fn main() {\n";
317 fn enclose_in_main_block(s: &str, config: &Config) -> String {
318 let indent = Indent::from_width(config, config.tab_spaces());
319 let mut result = String::with_capacity(s.len() * 2);
320 result.push_str(FN_MAIN_PREFIX);
321 let mut need_indent = true;
322 for (kind, line) in LineClasses::new(s) {
324 result.push_str(&indent.to_string(config));
326 result.push_str(&line);
328 need_indent = indent_next_line(kind, &line, config);
334 // Wrap the given code block with `fn main()` if it does not have one.
335 let snippet = enclose_in_main_block(code_snippet, config);
336 let mut result = String::with_capacity(snippet.len());
337 let mut is_first = true;
339 // While formatting the code, ignore the config's newline style setting and always use "\n"
340 // instead of "\r\n" for the newline characters. This is ok because the output here is
341 // not directly outputted by rustfmt command, but used by the comment formatter's input.
342 // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary.
343 let mut config_with_unix_newline = config.clone();
344 config_with_unix_newline
346 .newline_style(NewlineStyle::Unix);
347 let mut formatted = format_snippet(&snippet, &config_with_unix_newline)?;
348 // Remove wrapping main block
349 formatted.unwrap_code_block();
351 // Trim "fn main() {" on the first line and "}" on the last line,
352 // then unindent the whole code block.
353 let block_len = formatted
356 .unwrap_or_else(|| formatted.snippet.len());
357 let mut is_indented = true;
358 for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
364 let trimmed_line = if !is_indented {
366 } else if line.len() > config.max_width() {
367 // If there are lines that are larger than max width, we cannot tell
368 // whether we have succeeded but have some comments or strings that
369 // are too long, or we have failed to format code block. We will be
370 // conservative and just return `None` in this case.
372 } else if line.len() > config.tab_spaces() {
373 // Make sure that the line has leading whitespaces.
374 let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
375 if line.starts_with(indent_str.as_ref()) {
376 let offset = if config.hard_tabs() {
388 result.push_str(trimmed_line);
389 is_indented = indent_next_line(kind, line, config);
391 Some(FormattedSnippet {
393 non_formatted_ranges: formatted.non_formatted_ranges,
397 /// A session is a run of rustfmt across a single or multiple inputs.
398 pub struct Session<'b, T: Write> {
400 pub out: Option<&'b mut T>,
401 pub(crate) errors: ReportedErrors,
402 source_file: SourceFile,
405 impl<'b, T: Write + 'b> Session<'b, T> {
406 pub fn new(config: Config, out: Option<&'b mut T>) -> Session<'b, T> {
407 if config.emit_mode() == EmitMode::Checkstyle {
408 println!("{}", checkstyle::header());
414 errors: ReportedErrors::default(),
415 source_file: SourceFile::new(),
419 /// The main entry point for Rustfmt. Formats the given input according to the
420 /// given config. `out` is only necessary if required by the configuration.
421 pub fn format(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
422 self.format_input_inner(input)
425 pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
427 F: FnOnce(&mut Session<'b, T>) -> U,
429 mem::swap(&mut config, &mut self.config);
430 let result = f(self);
431 mem::swap(&mut config, &mut self.config);
435 pub fn add_operational_error(&mut self) {
436 self.errors.has_operational_errors = true;
439 pub fn has_operational_errors(&self) -> bool {
440 self.errors.has_operational_errors
443 pub fn has_parsing_errors(&self) -> bool {
444 self.errors.has_parsing_errors
447 pub fn has_formatting_errors(&self) -> bool {
448 self.errors.has_formatting_errors
451 pub fn has_check_errors(&self) -> bool {
452 self.errors.has_check_errors
455 pub fn has_diff(&self) -> bool {
459 pub fn has_no_errors(&self) -> bool {
460 !(self.has_operational_errors()
461 || self.has_parsing_errors()
462 || self.has_formatting_errors()
463 || self.has_check_errors()
465 || self.errors.has_macro_format_failure
469 impl<'b, T: Write + 'b> Drop for Session<'b, T> {
471 if self.config.emit_mode() == EmitMode::Checkstyle {
472 println!("{}", checkstyle::footer());
484 fn is_text(&self) -> bool {
486 Input::File(_) => false,
487 Input::Text(_) => true,
491 fn file_name(&self) -> FileName {
493 Input::File(ref file) => FileName::Real(file.clone()),
494 Input::Text(..) => FileName::Stdin,
498 fn to_directory_ownership(&self) -> Option<DirectoryOwnership> {
500 Input::File(ref file) => {
501 // If there exists a directory with the same name as an input,
502 // then the input should be parsed as a sub module.
503 let file_stem = file.file_stem()?;
504 if file.parent()?.to_path_buf().join(file_stem).is_dir() {
505 Some(DirectoryOwnership::Owned {
506 relative: file_stem.to_str().map(ast::Ident::from_str),
522 fn test_no_panic_on_format_snippet_and_format_code_block() {
523 // `format_snippet()` and `format_code_block()` should not panic
524 // even when we cannot parse the given snippet.
526 assert!(format_snippet(snippet, &Config::default()).is_none());
527 assert!(format_code_block(snippet, &Config::default()).is_none());
530 fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
532 F: Fn(&str, &Config) -> Option<FormattedSnippet>,
534 let output = formatter(input, &Config::default());
535 output.is_some() && output.unwrap().snippet == expected
539 fn test_format_snippet() {
540 let snippet = "fn main() { println!(\"hello, world\"); }";
542 let expected = "fn main() {\n \
543 println!(\"hello, world\");\n\
546 let expected = "fn main() {\r\n \
547 println!(\"hello, world\");\r\n\
549 assert!(test_format_inner(format_snippet, snippet, expected));
553 fn test_format_code_block_fail() {
555 let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
556 assert!(format_code_block(code_block, &Config::default()).is_none());
560 fn test_format_code_block() {
562 let code_block = "let x=3;";
563 let expected = "let x = 3;";
564 assert!(test_format_inner(format_code_block, code_block, expected));
566 // more complex code block, taken from chains.rs.
568 "let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
570 chain_indent(context, shape.add_offset(parent_rewrite.len())),
571 context.config.indent_style() == IndentStyle::Visual || is_small_parent,
573 } else if is_block_expr(context, &parent, &parent_rewrite) {
574 match context.config.indent_style() {
575 // Try to put the first child on the same line with parent's last line
576 IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
577 // The parent is a block, so align the rest of the chain with the closing
579 IndentStyle::Visual => (parent_shape, false),
583 chain_indent(context, shape.add_offset(parent_rewrite.len())),
589 "let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
591 chain_indent(context, shape.add_offset(parent_rewrite.len())),
592 context.config.indent_style() == IndentStyle::Visual || is_small_parent,
594 } else if is_block_expr(context, &parent, &parent_rewrite) {
595 match context.config.indent_style() {
596 // Try to put the first child on the same line with parent's last line
597 IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
598 // The parent is a block, so align the rest of the chain with the closing
600 IndentStyle::Visual => (parent_shape, false),
604 chain_indent(context, shape.add_offset(parent_rewrite.len())),
608 assert!(test_format_inner(format_code_block, code_block, expected));