rustfmt_nightly/
lib.rs

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