Skip to content

Fix master #4057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions rustfmt-core/rustfmt-bin/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ fn format(opt: Opt) -> Result<i32> {
return format_string(buf, opt);
}

if let Some(file) = opt.files.iter().find(|f| !f.exists()) {
return Err(format_err!(
"Error: file `{}` does not exist",
file.display()
));
}
if let Some(dir) = opt.files.iter().find(|f| f.is_dir()) {
return Err(format_err!("Error: `{}` is a directory", dir.display()));
}

let (config, config_path) = load_config(None, Some(&opt))?;

if config.verbose() == Verbosity::Verbose {
Expand All @@ -489,30 +499,22 @@ fn format(opt: Opt) -> Result<i32> {
for pair in FileConfigPairIter::new(&opt, config_path.is_some()) {
let file = pair.file;

if !file.exists() {
eprintln!("Error: file `{}` does not exist", file.display());
session.add_operational_error();
} else if file.is_dir() {
eprintln!("Error: `{}` is a directory", file.display());
session.add_operational_error();
} else {
if let FileConfig::Local(local_config, config_path) = pair.config {
if let Some(path) = config_path {
if local_config.verbose() == Verbosity::Verbose {
println!(
"Using rustfmt config file {} for {}",
path.display(),
file.display()
);
}
if let FileConfig::Local(local_config, config_path) = pair.config {
if let Some(path) = config_path {
if local_config.verbose() == Verbosity::Verbose {
println!(
"Using rustfmt config file {} for {}",
path.display(),
file.display()
);
}

session.override_config(local_config, |sess| {
format_and_emit_report(sess, Input::File(file.to_path_buf()))
});
} else {
format_and_emit_report(&mut session, Input::File(file.to_path_buf()));
}

session.override_config(local_config, |sess| {
format_and_emit_report(sess, Input::File(file.to_path_buf()))
});
} else {
format_and_emit_report(&mut session, Input::File(file.to_path_buf()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Format attributes and meta items.

use rustc_span::{BytePos, DUMMY_SP, Span, symbol::sym};
use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I had a feeling I was adding some misformatted things when bumping the rustc-ap crates 😞. Glad to have the tests running again!

use syntax::ast;

use self::doc_comment::DocCommentFormatter;
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;

use rustc_span::{BytePos, DUMMY_SP, source_map, Span, symbol::sym};
use rustc_span::{source_map, symbol::sym, BytePos, Span, DUMMY_SP};
use syntax::ast::{self, UseTreeKind};

use crate::comment::combine_strs_with_missing_comments;
Expand Down
18 changes: 11 additions & 7 deletions rustfmt-core/rustfmt-lib/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Cow;
use std::cmp::{max, min, Ordering};

use regex::Regex;
use rustc_span::{BytePos, DUMMY_SP, source_map, Span, symbol};
use rustc_span::{source_map, symbol, BytePos, Span, DUMMY_SP};
use syntax::visit;
use syntax::{ast, ptr};

Expand Down Expand Up @@ -287,8 +287,8 @@ impl<'a> FnSig<'a> {
defaultness,
unsafety: fn_sig.header.unsafety,
visibility: vis.clone(),
}
}
},
},
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -744,7 +744,13 @@ pub(crate) fn format_impl(
item: &ast::Item,
offset: Indent,
) -> Option<String> {
if let ast::ItemKind::Impl { ref generics, ref self_ty, ref items, ..} = item.kind {
if let ast::ItemKind::Impl {
ref generics,
ref self_ty,
ref items,
..
} = item.kind
{
let mut result = String::with_capacity(128);
let ref_and_type = format_impl_ref_and_type(context, item, offset)?;
let sep = offset.to_string_with_newline(context.config);
Expand Down Expand Up @@ -1760,9 +1766,7 @@ impl<'a> StaticParts<'a> {
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
let (prefix, ty, mutability, expr) = match item.kind {
ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
ast::ItemKind::Const(ref ty, ref expr) => {
("const", ty, ast::Mutability::Not, expr)
}
ast::ItemKind::Const(ref ty, ref expr) => ("const", ty, ast::Mutability::Not, expr),
_ => unreachable!(),
};
StaticParts {
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::panic::{catch_unwind, AssertUnwindSafe};

use rustc_ast_pretty::pprust;
use rustc_parse::{new_parser_from_tts, parser::Parser};
use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, symbol::kw};
use rustc_span::{symbol::kw, BytePos, Span, Symbol, DUMMY_SP};
use syntax::token::{BinOpToken, DelimToken, Token, TokenKind};
use syntax::tokenstream::{Cursor, TokenStream, TokenTree};
use syntax::{ast, ptr};
Expand Down
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-lib/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ impl Rewrite for Pat {
shape,
SeparatorPlace::Front,
)
},
}
(_, _) => unimplemented!(),
}
},
PatKind::Ref(ref pat, mutability) => {
let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, shape)
Expand Down
8 changes: 3 additions & 5 deletions rustfmt-core/rustfmt-lib/src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::cmp::{Ord, Ordering};

use rustc_span::{Span, symbol::sym};
use rustc_span::{symbol::sym, Span};
use syntax::{ast, attr};

use crate::config::Config;
Expand All @@ -31,10 +31,8 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
// `extern crate foo as bar;`
// ^^^ Comparing this.
let a_orig_name =
a_name.map_or_else(|| a.ident.as_str(), rustc_span::Symbol::as_str);
let b_orig_name =
b_name.map_or_else(|| b.ident.as_str(), rustc_span::Symbol::as_str);
let a_orig_name = a_name.map_or_else(|| a.ident.as_str(), rustc_span::Symbol::as_str);
let b_orig_name = b_name.map_or_else(|| b.ident.as_str(), rustc_span::Symbol::as_str);
let result = a_orig_name.cmp(&b_orig_name);
if result != Ordering::Equal {
return result;
Expand Down
3 changes: 2 additions & 1 deletion rustfmt-core/rustfmt-lib/src/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl SkipContext {
syntax::ast::AttrKind::Normal(ref attr_item) => {
if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) {
get_skip_names(&mut self.macros, attr)
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes) {
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes)
{
get_skip_names(&mut self.attributes, attr)
}
}
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/syntux/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};

use rustc_errors::{Diagnostic, PResult};
use rustc_parse::{new_sub_parser_from_file, parser::Parser as RawParser};
use rustc_span::{DUMMY_SP, Span, symbol::kw};
use rustc_span::{symbol::kw, Span, DUMMY_SP};
use syntax::ast;
use syntax::token::{DelimToken, TokenKind};

Expand Down
7 changes: 5 additions & 2 deletions rustfmt-core/rustfmt-lib/src/syntux/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use rustc_data_structures::sync::{Lrc, Send};
use rustc_errors::emitter::{Emitter, EmitterWriter};
use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel};
use rustc_session::parse::ParseSess as RawParseSess;
use rustc_span::{BytePos, source_map::{FilePathMapping, SourceMap}, Span};
use rustc_span::{
source_map::{FilePathMapping, SourceMap},
BytePos, Span,
};
use syntax::ast;

use crate::config::file_lines::LineRange;
Expand Down Expand Up @@ -274,8 +277,8 @@ mod tests {
use crate::config::IgnoreList;
use crate::is_nightly_channel;
use crate::utils::mk_sp;
use rustc_span::{FileName as SourceMapFileName, MultiSpan, DUMMY_SP};
use std::path::PathBuf;
use rustc_span::{DUMMY_SP, MultiSpan, FileName as SourceMapFileName};

struct TestEmitter {
num_emitted_errors: Rc<RefCell<u32>>,
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter::ExactSizeIterator;
use std::ops::Deref;

use rustc_span::{BytePos, Span, symbol::kw};
use rustc_span::{symbol::kw, BytePos, Span};
use syntax::ast::{self, FunctionRetTy, Mutability};

use crate::config::lists::*;
Expand Down
5 changes: 3 additions & 2 deletions rustfmt-core/rustfmt-lib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use rustc_ast_pretty::pprust;
use rustc_span::{BytePos, ExpnId, Span, sym, Symbol, SyntaxContext};
use rustc_span::{sym, BytePos, ExpnId, Span, Symbol, SyntaxContext};
use rustc_target::spec::abi;
use syntax::ast::{
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility,
Expand Down Expand Up @@ -255,7 +255,8 @@ fn is_skip(meta_item: &MetaItem) -> bool {
match meta_item.kind {
MetaItemKind::Word => {
let path_str = pprust::path_to_string(&meta_item.path);
path_str == &*skip_annotation().as_str() || path_str == &*depr_skip_annotation().as_str()
path_str == &*skip_annotation().as_str()
|| path_str == &*depr_skip_annotation().as_str()
}
MetaItemKind::List(ref l) => {
meta_item.check_name(sym::cfg_attr) && l.len() == 2 && is_skip_nested(&l[1])
Expand Down
8 changes: 4 additions & 4 deletions rustfmt-core/rustfmt-lib/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::{Cell, RefCell};
use std::rc::Rc;

use rustc_span::{BytePos, Pos, Span};
use syntax::{ast, visit, token::DelimToken};
use syntax::{ast, token::DelimToken, visit};

use crate::attr::*;
use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
Expand All @@ -14,7 +14,7 @@ use crate::items::{
rewrite_opaque_impl_type, rewrite_opaque_type, rewrite_type_alias, FnBraceStyle, FnSig,
StaticParts, StructParts,
};
use crate::macros::{rewrite_macro, rewrite_macro_def, MacroPosition, macro_style};
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::{Indent, Shape};
use crate::skip::{is_skip_attr, SkipContext};
Expand Down Expand Up @@ -448,7 +448,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
if should_visit_node_again {
match item.kind {
ast::ItemKind::Use(ref tree) => self.format_import(item, tree),
ast::ItemKind::Impl {..} => {
ast::ItemKind::Impl { .. } => {
let block_indent = self.block_indent;
let rw = self.with_context(|ctx| format_impl(&ctx, item, block_indent));
self.push_rewrite(item.span, rw);
Expand Down Expand Up @@ -664,7 +664,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.block_indent,
),
None => rewrite_associated(),
}
},
};
self.push_rewrite(ii.span, rewrite);
}
Expand Down