1 #![feature(rustc_private)]
2 #![deny(rust_2018_idioms)]
3 #![warn(unreachable_pub)]
4 #![recursion_limit = "256"]
5 #![allow(clippy::match_like_matches_macro)]
6 #![allow(unreachable_pub)]
7
8 #[cfg(test)]
9 #[macro_use]
10 extern crate lazy_static;
11 #[macro_use]
12 extern crate log;
13
14 // N.B. these crates are loaded from the sysroot, so they need extern crate.
15 extern crate rustc_ast;
16 extern crate rustc_ast_pretty;
17 extern crate rustc_builtin_macros;
18 extern crate rustc_data_structures;
19 extern crate rustc_errors;
20 extern crate rustc_expand;
21 extern crate rustc_parse;
22 extern crate rustc_session;
23 extern crate rustc_span;
24 extern crate thin_vec;
25
26 // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
27 // files.
28 #[allow(unused_extern_crates)]
29 extern crate rustc_driver;
30
31 use std::cell::RefCell;
32 use std::collections::HashMap;
33 use std::fmt;
34 use std::io::{self, Write};
35 use std::mem;
36 use std::panic;
37 use std::path::PathBuf;
38 use std::rc::Rc;
39
40 use rustc_ast::ast;
41 use rustc_span::symbol;
42 use thiserror::Error;
43
44 use crate::comment::LineClasses;
45 use crate::emitter::Emitter;
46 use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
47 use crate::modules::ModuleResolutionError;
48 use crate::parse::parser::DirectoryOwnership;
49 use crate::shape::Indent;
50 use crate::utils::indent_next_line;
51
52 pub use crate::config::{
53 load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
54 Range, Verbosity,
55 };
56
57 pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};
58
59 pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
60
61 #[macro_use]
62 mod utils;
63
64 mod attr;
65 mod chains;
66 mod closures;
67 mod comment;
68 pub(crate) mod config;
69 mod coverage;
70 mod emitter;
71 mod expr;
72 mod format_report_formatter;
73 pub(crate) mod formatting;
74 mod ignore_path;
75 mod imports;
76 mod items;
77 mod lists;
78 mod macros;
79 mod matches;
80 mod missed_spans;
81 pub(crate) mod modules;
82 mod overflow;
83 mod pairs;
84 mod parse;
85 mod patterns;
86 mod release_channel;
87 mod reorder;
88 mod rewrite;
89 pub(crate) mod rustfmt_diff;
90 mod shape;
91 mod skip;
92 pub(crate) mod source_file;
93 pub(crate) mod source_map;
94 mod spanned;
95 mod stmt;
96 mod string;
97 #[cfg(test)]
98 mod test;
99 mod types;
100 mod vertical;
101 pub(crate) mod visitor;
102
103 /// The various errors that can occur during formatting. Note that not all of
104 /// these can currently be propagated to clients.
105 #[derive(Error, Debug)]
106 pub enum ErrorKind {
107 /// Line has exceeded character limit (found, maximum).
108 #[error(
109 "line formatted, but exceeded maximum width \
110 (maximum: {1} (see `max_width` option), found: {0})"
111 )]
112 LineOverflow(usize, usize),
113 /// Line ends in whitespace.
114 #[error("left behind trailing whitespace")]
115 TrailingWhitespace,
116 /// Used deprecated skip attribute.
117 #[error("`rustfmt_skip` is deprecated; use `rustfmt::skip`")]
118 DeprecatedAttr,
119 /// Used a rustfmt:: attribute other than skip or skip::macros.
120 #[error("invalid attribute")]
121 BadAttr,
122 /// An io error during reading or writing.
123 #[error("io error: {0}")]
124 IoError(io::Error),
125 /// Error during module resolution.
126 #[error("{0}")]
127 ModuleResolutionError(#[from] ModuleResolutionError),
128 /// Parse error occurred when parsing the input.
129 #[error("parse error")]
130 ParseError,
131 /// The user mandated a version and the current version of Rustfmt does not
132 /// satisfy that requirement.
133 #[error("version mismatch")]
134 VersionMismatch,
135 /// If we had formatted the given node, then we would have lost a comment.
136 #[error("not formatted because a comment would be lost")]
137 LostComment,
138 /// Invalid glob pattern in `ignore` configuration option.
139 #[error("Invalid glob pattern found in ignore list: {0}")]
140 InvalidGlobPattern(ignore::Error),
141 }
142
143 impl ErrorKind {
is_comment(&self) -> bool144 fn is_comment(&self) -> bool {
145 matches!(self, ErrorKind::LostComment)
146 }
147 }
148
149 impl From<io::Error> for ErrorKind {
from(e: io::Error) -> ErrorKind150 fn from(e: io::Error) -> ErrorKind {
151 ErrorKind::IoError(e)
152 }
153 }
154
155 /// Result of formatting a snippet of code along with ranges of lines that didn't get formatted,
156 /// i.e., that got returned as they were originally.
157 #[derive(Debug)]
158 struct FormattedSnippet {
159 snippet: String,
160 non_formatted_ranges: Vec<(usize, usize)>,
161 }
162
163 impl FormattedSnippet {
164 /// In case the snippet needed to be wrapped in a function, this shifts down the ranges of
165 /// non-formatted code.
unwrap_code_block(&mut self)166 fn unwrap_code_block(&mut self) {
167 self.non_formatted_ranges
168 .iter_mut()
169 .for_each(|(low, high)| {
170 *low -= 1;
171 *high -= 1;
172 });
173 }
174
175 /// Returns `true` if the line n did not get formatted.
is_line_non_formatted(&self, n: usize) -> bool176 fn is_line_non_formatted(&self, n: usize) -> bool {
177 self.non_formatted_ranges
178 .iter()
179 .any(|(low, high)| *low <= n && n <= *high)
180 }
181 }
182
183 /// Reports on any issues that occurred during a run of Rustfmt.
184 ///
185 /// Can be reported to the user using the `Display` impl on [`FormatReportFormatter`].
186 #[derive(Clone)]
187 pub struct FormatReport {
188 // Maps stringified file paths to their associated formatting errors.
189 internal: Rc<RefCell<(FormatErrorMap, ReportedErrors)>>,
190 non_formatted_ranges: Vec<(usize, usize)>,
191 }
192
193 impl FormatReport {
new() -> FormatReport194 fn new() -> FormatReport {
195 FormatReport {
196 internal: Rc::new(RefCell::new((HashMap::new(), ReportedErrors::default()))),
197 non_formatted_ranges: Vec::new(),
198 }
199 }
200
add_non_formatted_ranges(&mut self, mut ranges: Vec<(usize, usize)>)201 fn add_non_formatted_ranges(&mut self, mut ranges: Vec<(usize, usize)>) {
202 self.non_formatted_ranges.append(&mut ranges);
203 }
204
append(&self, f: FileName, mut v: Vec<FormattingError>)205 fn append(&self, f: FileName, mut v: Vec<FormattingError>) {
206 self.track_errors(&v);
207 self.internal
208 .borrow_mut()
209 .0
210 .entry(f)
211 .and_modify(|fe| fe.append(&mut v))
212 .or_insert(v);
213 }
214
track_errors(&self, new_errors: &[FormattingError])215 fn track_errors(&self, new_errors: &[FormattingError]) {
216 let errs = &mut self.internal.borrow_mut().1;
217 if !new_errors.is_empty() {
218 errs.has_formatting_errors = true;
219 }
220 if errs.has_operational_errors && errs.has_check_errors && errs.has_unformatted_code_errors
221 {
222 return;
223 }
224 for err in new_errors {
225 match err.kind {
226 ErrorKind::LineOverflow(..) => {
227 errs.has_operational_errors = true;
228 }
229 ErrorKind::TrailingWhitespace => {
230 errs.has_operational_errors = true;
231 errs.has_unformatted_code_errors = true;
232 }
233 ErrorKind::LostComment => {
234 errs.has_unformatted_code_errors = true;
235 }
236 ErrorKind::DeprecatedAttr | ErrorKind::BadAttr | ErrorKind::VersionMismatch => {
237 errs.has_check_errors = true;
238 }
239 _ => {}
240 }
241 }
242 }
243
add_diff(&mut self)244 fn add_diff(&mut self) {
245 self.internal.borrow_mut().1.has_diff = true;
246 }
247
add_macro_format_failure(&mut self)248 fn add_macro_format_failure(&mut self) {
249 self.internal.borrow_mut().1.has_macro_format_failure = true;
250 }
251
add_parsing_error(&mut self)252 fn add_parsing_error(&mut self) {
253 self.internal.borrow_mut().1.has_parsing_errors = true;
254 }
255
warning_count(&self) -> usize256 fn warning_count(&self) -> usize {
257 self.internal
258 .borrow()
259 .0
260 .iter()
261 .map(|(_, errors)| errors.len())
262 .sum()
263 }
264
265 /// Whether any warnings or errors are present in the report.
has_warnings(&self) -> bool266 pub fn has_warnings(&self) -> bool {
267 self.internal.borrow().1.has_formatting_errors
268 }
269
270 /// Print the report to a terminal using colours and potentially other
271 /// fancy output.
272 #[deprecated(note = "Use FormatReportFormatter with colors enabled instead")]
fancy_print( &self, mut t: Box<dyn term::Terminal<Output = io::Stderr>>, ) -> Result<(), term::Error>273 pub fn fancy_print(
274 &self,
275 mut t: Box<dyn term::Terminal<Output = io::Stderr>>,
276 ) -> Result<(), term::Error> {
277 writeln!(
278 t,
279 "{}",
280 FormatReportFormatterBuilder::new(self)
281 .enable_colors(true)
282 .build()
283 )?;
284 Ok(())
285 }
286 }
287
288 /// Deprecated - Use FormatReportFormatter instead
289 // https://github.com/rust-lang/rust/issues/78625
290 // https://github.com/rust-lang/rust/issues/39935
291 impl fmt::Display for FormatReport {
292 // Prints all the formatting errors.
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>293 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
294 write!(fmt, "{}", FormatReportFormatterBuilder::new(self).build())?;
295 Ok(())
296 }
297 }
298
299 /// Format the given snippet. The snippet is expected to be *complete* code.
300 /// When we cannot parse the given snippet, this function returns `None`.
format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<FormattedSnippet>301 fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<FormattedSnippet> {
302 let mut config = config.clone();
303 panic::catch_unwind(|| {
304 let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
305 config.set().emit_mode(config::EmitMode::Stdout);
306 config.set().verbose(Verbosity::Quiet);
307 config.set().hide_parse_errors(true);
308 if is_macro_def {
309 config.set().error_on_unformatted(true);
310 }
311
312 let (formatting_error, result) = {
313 let input = Input::Text(snippet.into());
314 let mut session = Session::new(config, Some(&mut out));
315 let result = session.format_input_inner(input, is_macro_def);
316 (
317 session.errors.has_macro_format_failure
318 || session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
319 || result.is_err()
320 || (is_macro_def && session.has_unformatted_code_errors()),
321 result,
322 )
323 };
324 if formatting_error {
325 None
326 } else {
327 String::from_utf8(out).ok().map(|snippet| FormattedSnippet {
328 snippet,
329 non_formatted_ranges: result.unwrap().non_formatted_ranges,
330 })
331 }
332 })
333 // Discard panics encountered while formatting the snippet
334 // The ? operator is needed to remove the extra Option
335 .ok()?
336 }
337
338 /// Format the given code block. Mainly targeted for code block in comment.
339 /// The code block may be incomplete (i.e., parser may be unable to parse it).
340 /// To avoid panic in parser, we wrap the code block with a dummy function.
341 /// The returned code block does **not** end with newline.
format_code_block( code_snippet: &str, config: &Config, is_macro_def: bool, ) -> Option<FormattedSnippet>342 fn format_code_block(
343 code_snippet: &str,
344 config: &Config,
345 is_macro_def: bool,
346 ) -> Option<FormattedSnippet> {
347 const FN_MAIN_PREFIX: &str = "fn main() {\n";
348
349 fn enclose_in_main_block(s: &str, config: &Config) -> String {
350 let indent = Indent::from_width(config, config.tab_spaces());
351 let mut result = String::with_capacity(s.len() * 2);
352 result.push_str(FN_MAIN_PREFIX);
353 let mut need_indent = true;
354 for (kind, line) in LineClasses::new(s) {
355 if need_indent {
356 result.push_str(&indent.to_string(config));
357 }
358 result.push_str(&line);
359 result.push('\n');
360 need_indent = indent_next_line(kind, &line, config);
361 }
362 result.push('}');
363 result
364 }
365
366 // Wrap the given code block with `fn main()` if it does not have one.
367 let snippet = enclose_in_main_block(code_snippet, config);
368 let mut result = String::with_capacity(snippet.len());
369 let mut is_first = true;
370
371 // While formatting the code, ignore the config's newline style setting and always use "\n"
372 // instead of "\r\n" for the newline characters. This is ok because the output here is
373 // not directly outputted by rustfmt command, but used by the comment formatter's input.
374 // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary.
375 let mut config_with_unix_newline = config.clone();
376 config_with_unix_newline
377 .set()
378 .newline_style(NewlineStyle::Unix);
379 let mut formatted = format_snippet(&snippet, &config_with_unix_newline, is_macro_def)?;
380 // Remove wrapping main block
381 formatted.unwrap_code_block();
382
383 // Trim "fn main() {" on the first line and "}" on the last line,
384 // then unindent the whole code block.
385 let block_len = formatted
386 .snippet
387 .rfind('}')
388 .unwrap_or_else(|| formatted.snippet.len());
389 let mut is_indented = true;
390 let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
391 for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
392 if !is_first {
393 result.push('\n');
394 } else {
395 is_first = false;
396 }
397 let trimmed_line = if !is_indented {
398 line
399 } else if line.len() > config.max_width() {
400 // If there are lines that are larger than max width, we cannot tell
401 // whether we have succeeded but have some comments or strings that
402 // are too long, or we have failed to format code block. We will be
403 // conservative and just return `None` in this case.
404 return None;
405 } else if line.len() > indent_str.len() {
406 // Make sure that the line has leading whitespaces.
407 if line.starts_with(indent_str.as_ref()) {
408 let offset = if config.hard_tabs() {
409 1
410 } else {
411 config.tab_spaces()
412 };
413 &line[offset..]
414 } else {
415 line
416 }
417 } else {
418 line
419 };
420 result.push_str(trimmed_line);
421 is_indented = indent_next_line(kind, line, config);
422 }
423 Some(FormattedSnippet {
424 snippet: result,
425 non_formatted_ranges: formatted.non_formatted_ranges,
426 })
427 }
428
429 /// A session is a run of rustfmt across a single or multiple inputs.
430 pub struct Session<'b, T: Write> {
431 pub config: Config,
432 pub out: Option<&'b mut T>,
433 pub(crate) errors: ReportedErrors,
434 source_file: SourceFile,
435 emitter: Box<dyn Emitter + 'b>,
436 }
437
438 impl<'b, T: Write + 'b> Session<'b, T> {
new(config: Config, mut out: Option<&'b mut T>) -> Session<'b, T>439 pub fn new(config: Config, mut out: Option<&'b mut T>) -> Session<'b, T> {
440 let emitter = create_emitter(&config);
441
442 if let Some(ref mut out) = out {
443 let _ = emitter.emit_header(out);
444 }
445
446 Session {
447 config,
448 out,
449 emitter,
450 errors: ReportedErrors::default(),
451 source_file: SourceFile::new(),
452 }
453 }
454
455 /// The main entry point for Rustfmt. Formats the given input according to the
456 /// given config. `out` is only necessary if required by the configuration.
format(&mut self, input: Input) -> Result<FormatReport, ErrorKind>457 pub fn format(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
458 self.format_input_inner(input, false)
459 }
460
override_config<F, U>(&mut self, mut config: Config, f: F) -> U where F: FnOnce(&mut Session<'b, T>) -> U,461 pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
462 where
463 F: FnOnce(&mut Session<'b, T>) -> U,
464 {
465 mem::swap(&mut config, &mut self.config);
466 let result = f(self);
467 mem::swap(&mut config, &mut self.config);
468 result
469 }
470
add_operational_error(&mut self)471 pub fn add_operational_error(&mut self) {
472 self.errors.has_operational_errors = true;
473 }
474
has_operational_errors(&self) -> bool475 pub fn has_operational_errors(&self) -> bool {
476 self.errors.has_operational_errors
477 }
478
has_parsing_errors(&self) -> bool479 pub fn has_parsing_errors(&self) -> bool {
480 self.errors.has_parsing_errors
481 }
482
has_formatting_errors(&self) -> bool483 pub fn has_formatting_errors(&self) -> bool {
484 self.errors.has_formatting_errors
485 }
486
has_check_errors(&self) -> bool487 pub fn has_check_errors(&self) -> bool {
488 self.errors.has_check_errors
489 }
490
has_diff(&self) -> bool491 pub fn has_diff(&self) -> bool {
492 self.errors.has_diff
493 }
494
has_unformatted_code_errors(&self) -> bool495 pub fn has_unformatted_code_errors(&self) -> bool {
496 self.errors.has_unformatted_code_errors
497 }
498
has_no_errors(&self) -> bool499 pub fn has_no_errors(&self) -> bool {
500 !(self.has_operational_errors()
501 || self.has_parsing_errors()
502 || self.has_formatting_errors()
503 || self.has_check_errors()
504 || self.has_diff()
505 || self.has_unformatted_code_errors()
506 || self.errors.has_macro_format_failure)
507 }
508 }
509
create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a>510 pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
511 match config.emit_mode() {
512 EmitMode::Files if config.make_backup() => {
513 Box::new(emitter::FilesWithBackupEmitter::default())
514 }
515 EmitMode::Files => Box::new(emitter::FilesEmitter::new(
516 config.print_misformatted_file_names(),
517 )),
518 EmitMode::Stdout | EmitMode::Coverage => {
519 Box::new(emitter::StdoutEmitter::new(config.verbose()))
520 }
521 EmitMode::Json => Box::new(emitter::JsonEmitter::default()),
522 EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter::default()),
523 EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter::default()),
524 EmitMode::Diff => Box::new(emitter::DiffEmitter::new(config.clone())),
525 }
526 }
527
528 impl<'b, T: Write + 'b> Drop for Session<'b, T> {
drop(&mut self)529 fn drop(&mut self) {
530 if let Some(ref mut out) = self.out {
531 let _ = self.emitter.emit_footer(out);
532 }
533 }
534 }
535
536 #[derive(Debug)]
537 pub enum Input {
538 File(PathBuf),
539 Text(String),
540 }
541
542 impl Input {
file_name(&self) -> FileName543 fn file_name(&self) -> FileName {
544 match *self {
545 Input::File(ref file) => FileName::Real(file.clone()),
546 Input::Text(..) => FileName::Stdin,
547 }
548 }
549
to_directory_ownership(&self) -> Option<DirectoryOwnership>550 fn to_directory_ownership(&self) -> Option<DirectoryOwnership> {
551 match self {
552 Input::File(ref file) => {
553 // If there exists a directory with the same name as an input,
554 // then the input should be parsed as a sub module.
555 let file_stem = file.file_stem()?;
556 if file.parent()?.to_path_buf().join(file_stem).is_dir() {
557 Some(DirectoryOwnership::Owned {
558 relative: file_stem.to_str().map(symbol::Ident::from_str),
559 })
560 } else {
561 None
562 }
563 }
564 _ => None,
565 }
566 }
567 }
568
569 #[cfg(test)]
570 mod unit_tests {
571 use super::*;
572
573 #[test]
test_no_panic_on_format_snippet_and_format_code_block()574 fn test_no_panic_on_format_snippet_and_format_code_block() {
575 // `format_snippet()` and `format_code_block()` should not panic
576 // even when we cannot parse the given snippet.
577 let snippet = "let";
578 assert!(format_snippet(snippet, &Config::default(), false).is_none());
579 assert!(format_code_block(snippet, &Config::default(), false).is_none());
580 }
581
test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool where F: Fn(&str, &Config, bool) -> Option<FormattedSnippet>,582 fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
583 where
584 F: Fn(&str, &Config, bool) -> Option<FormattedSnippet>,
585 {
586 let output = formatter(input, &Config::default(), false);
587 output.is_some() && output.unwrap().snippet == expected
588 }
589
590 #[test]
test_format_snippet()591 fn test_format_snippet() {
592 let snippet = "fn main() { println!(\"hello, world\"); }";
593 #[cfg(not(windows))]
594 let expected = "fn main() {\n \
595 println!(\"hello, world\");\n\
596 }\n";
597 #[cfg(windows)]
598 let expected = "fn main() {\r\n \
599 println!(\"hello, world\");\r\n\
600 }\r\n";
601 assert!(test_format_inner(format_snippet, snippet, expected));
602 }
603
604 #[test]
test_format_code_block_fail()605 fn test_format_code_block_fail() {
606 #[rustfmt::skip]
607 let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
608 assert!(format_code_block(code_block, &Config::default(), false).is_none());
609 }
610
611 #[test]
test_format_code_block()612 fn test_format_code_block() {
613 // simple code block
614 let code_block = "let x=3;";
615 let expected = "let x = 3;";
616 assert!(test_format_inner(format_code_block, code_block, expected));
617
618 // more complex code block, taken from chains.rs.
619 let code_block =
620 "let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
621 (
622 chain_indent(context, shape.add_offset(parent_rewrite.len())),
623 context.config.indent_style() == IndentStyle::Visual || is_small_parent,
624 )
625 } else if is_block_expr(context, &parent, &parent_rewrite) {
626 match context.config.indent_style() {
627 // Try to put the first child on the same line with parent's last line
628 IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
629 // The parent is a block, so align the rest of the chain with the closing
630 // brace.
631 IndentStyle::Visual => (parent_shape, false),
632 }
633 } else {
634 (
635 chain_indent(context, shape.add_offset(parent_rewrite.len())),
636 false,
637 )
638 };
639 ";
640 let expected =
641 "let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
642 (
643 chain_indent(context, shape.add_offset(parent_rewrite.len())),
644 context.config.indent_style() == IndentStyle::Visual || is_small_parent,
645 )
646 } else if is_block_expr(context, &parent, &parent_rewrite) {
647 match context.config.indent_style() {
648 // Try to put the first child on the same line with parent's last line
649 IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
650 // The parent is a block, so align the rest of the chain with the closing
651 // brace.
652 IndentStyle::Visual => (parent_shape, false),
653 }
654 } else {
655 (
656 chain_indent(context, shape.add_offset(parent_rewrite.len())),
657 false,
658 )
659 };";
660 assert!(test_format_inner(format_code_block, code_block, expected));
661 }
662 }
663