]> git.lizzy.rs Git - rust.git/blobdiff - src/lib.rs
set of clippy changes
[rust.git] / src / lib.rs
index 4d90f5a223eb86dd73e61af9dafb59325bed2586..7ebb967a494ce9ca11cf59dcf193110fc1539d9c 100644 (file)
@@ -8,7 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![feature(tool_attributes)]
 #![feature(decl_macro)]
 #![allow(unused_attributes)]
 #![feature(type_ascription)]
@@ -49,7 +48,7 @@
 
 use comment::LineClasses;
 use failure::Fail;
-use formatting::{FormatErrorMap, FormattingError, ReportedErrors, Summary};
+use formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
 use issues::Issue;
 use shape::Indent;
 
 mod chains;
 pub(crate) mod checkstyle;
 mod closures;
-pub(crate) mod codemap;
 mod comment;
 pub(crate) mod config;
 mod expr;
-pub(crate) mod filemap;
 pub(crate) mod formatting;
 mod imports;
 mod issues;
@@ -86,6 +83,8 @@
 mod rewrite;
 pub(crate) mod rustfmt_diff;
 mod shape;
+pub(crate) mod source_file;
+pub(crate) mod source_map;
 mod spanned;
 mod string;
 #[cfg(test)]
@@ -179,6 +178,9 @@ fn append(&self, f: FileName, mut v: Vec<FormattingError>) {
 
     fn track_errors(&self, new_errors: &[FormattingError]) {
         let errs = &mut self.internal.borrow_mut().1;
+        if !new_errors.is_empty() {
+            errs.has_formatting_errors = true;
+        }
         if errs.has_operational_errors && errs.has_check_errors {
             return;
         }
@@ -199,6 +201,18 @@ fn track_errors(&self, new_errors: &[FormattingError]) {
         }
     }
 
+    fn add_diff(&mut self) {
+        self.internal.borrow_mut().1.has_diff = true;
+    }
+
+    fn add_macro_format_failure(&mut self) {
+        self.internal.borrow_mut().1.has_macro_format_failure = true;
+    }
+
+    fn add_parsing_error(&mut self) {
+        self.internal.borrow_mut().1.has_parsing_errors = true;
+    }
+
     fn warning_count(&self) -> usize {
         self.internal
             .borrow()
@@ -210,7 +224,7 @@ fn warning_count(&self) -> usize {
 
     /// Whether any warnings or errors are present in the report.
     pub fn has_warnings(&self) -> bool {
-        self.warning_count() > 0
+        self.internal.borrow().1.has_formatting_errors
     }
 
     /// Print the report to a terminal using colours and potentially other
@@ -351,7 +365,7 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<String> {
     {
         let mut session = Session::new(config, Some(&mut out));
         let result = session.format(input);
-        let formatting_error = session.summary.has_macro_formatting_failure()
+        let formatting_error = session.errors.has_macro_format_failure
             || session.out.as_ref().unwrap().is_empty() && !snippet.is_empty();
         if formatting_error || result.is_err() {
             return None;
@@ -443,7 +457,8 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String {
 pub struct Session<'b, T: Write + 'b> {
     pub config: Config,
     pub out: Option<&'b mut T>,
-    pub summary: Summary,
+    pub(crate) errors: ReportedErrors,
+    source_file: SourceFile,
 }
 
 impl<'b, T: Write + 'b> Session<'b, T> {
@@ -455,18 +470,15 @@ pub fn new(config: Config, out: Option<&'b mut T>) -> Session<'b, T> {
         Session {
             config,
             out,
-            summary: Summary::default(),
+            errors: ReportedErrors::default(),
+            source_file: SourceFile::new(),
         }
     }
 
     /// The main entry point for Rustfmt. Formats the given input according to the
     /// given config. `out` is only necessary if required by the configuration.
     pub fn format(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
-        if !self.config.version_meets_requirement() {
-            return Err(ErrorKind::VersionMismatch);
-        }
-
-        syntax::with_globals(|| self.format_input_inner(input)).map(|tup| tup.1)
+        self.format_input_inner(input)
     }
 
     pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
@@ -478,6 +490,39 @@ pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
         mem::swap(&mut config, &mut self.config);
         result
     }
+
+    pub fn add_operational_error(&mut self) {
+        self.errors.has_operational_errors = true;
+    }
+
+    pub fn has_operational_errors(&self) -> bool {
+        self.errors.has_operational_errors
+    }
+
+    pub fn has_parsing_errors(&self) -> bool {
+        self.errors.has_parsing_errors
+    }
+
+    pub fn has_formatting_errors(&self) -> bool {
+        self.errors.has_formatting_errors
+    }
+
+    pub fn has_check_errors(&self) -> bool {
+        self.errors.has_check_errors
+    }
+
+    pub fn has_diff(&self) -> bool {
+        self.errors.has_diff
+    }
+
+    pub fn has_no_errors(&self) -> bool {
+        !(self.has_operational_errors()
+            || self.has_parsing_errors()
+            || self.has_formatting_errors()
+            || self.has_check_errors()
+            || self.has_diff())
+            || self.errors.has_macro_format_failure
+    }
 }
 
 impl<'b, T: Write + 'b> Drop for Session<'b, T> {
@@ -501,6 +546,13 @@ fn is_text(&self) -> bool {
             Input::Text(_) => true,
         }
     }
+
+    fn file_name(&self) -> FileName {
+        match *self {
+            Input::File(ref file) => FileName::Real(file.clone()),
+            Input::Text(..) => FileName::Stdin,
+        }
+    }
 }
 
 #[cfg(test)]