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