Skip to content

Commit 8bf82ae

Browse files
Merge remote-tracking branch 'upstream/master' into subtree-sync-2021-12-19
2 parents 122e1c3 + 57ac92b commit 8bf82ae

20 files changed

+161
-46
lines changed

src/bin/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let exit_code = match execute(&opts) {
2727
Ok(code) => code,
2828
Err(e) => {
29-
eprintln!("{}", e.to_string());
29+
eprintln!("{}", e);
3030
1
3131
}
3232
};

src/expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,9 +2003,7 @@ fn choose_rhs<R: Rewrite>(
20032003
has_rhs_comment: bool,
20042004
) -> Option<String> {
20052005
match orig_rhs {
2006-
Some(ref new_str) if new_str.is_empty() => {
2007-
return Some(String::new());
2008-
}
2006+
Some(ref new_str) if new_str.is_empty() => Some(String::new()),
20092007
Some(ref new_str)
20102008
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
20112009
{

src/formatting.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::{self, Write};
55
use std::time::{Duration, Instant};
66

77
use rustc_ast::ast;
8+
use rustc_ast::AstLike;
89
use rustc_span::Span;
910

1011
use self::newline_style::apply_newline_style;
@@ -15,7 +16,7 @@ use crate::issues::BadIssueSeeker;
1516
use crate::modules::Module;
1617
use crate::syntux::parser::{DirectoryOwnership, Parser, ParserError};
1718
use crate::syntux::session::ParseSess;
18-
use crate::utils::count_newlines;
19+
use crate::utils::{contains_skip, count_newlines};
1920
use crate::visitor::FmtVisitor;
2021
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
2122

@@ -58,6 +59,39 @@ impl<'b, T: Write + 'b> Session<'b, T> {
5859
}
5960
}
6061

62+
/// Determine if a module should be skipped. True if the module should be skipped, false otherwise.
63+
fn should_skip_module<T: FormatHandler>(
64+
config: &Config,
65+
context: &FormatContext<'_, T>,
66+
input_is_stdin: bool,
67+
main_file: &FileName,
68+
path: &FileName,
69+
module: &Module<'_>,
70+
) -> bool {
71+
if contains_skip(module.attrs()) {
72+
return true;
73+
}
74+
75+
if config.skip_children() && path != main_file {
76+
return true;
77+
}
78+
79+
if !input_is_stdin && context.ignore_file(path) {
80+
return true;
81+
}
82+
83+
if !config.format_generated_files() {
84+
let source_file = context.parse_session.span_to_file_contents(module.span);
85+
let src = source_file.src.as_ref().expect("SourceFile without src");
86+
87+
if is_generated_file(src) {
88+
return true;
89+
}
90+
}
91+
92+
false
93+
}
94+
6195
// Format an entire crate (or subset of the module tree).
6296
fn format_project<T: FormatHandler>(
6397
input: Input,
@@ -97,23 +131,19 @@ fn format_project<T: FormatHandler>(
97131
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
98132
!input_is_stdin && !config.skip_children(),
99133
)
100-
.visit_crate(&krate)?;
134+
.visit_crate(&krate)?
135+
.into_iter()
136+
.filter(|(path, module)| {
137+
!should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
138+
})
139+
.collect::<Vec<_>>();
101140

102141
timer = timer.done_parsing();
103142

104143
// Suppress error output if we have to do any further parsing.
105144
context.parse_session.set_silent_emitter();
106145

107146
for (path, module) in files {
108-
let source_file = context.parse_session.span_to_file_contents(module.span);
109-
let src = source_file.src.as_ref().expect("SourceFile without src");
110-
111-
let should_ignore = (!input_is_stdin && context.ignore_file(&path))
112-
|| (!config.format_generated_files() && is_generated_file(src));
113-
114-
if (config.skip_children() && path != main_file) || should_ignore {
115-
continue;
116-
}
117147
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
118148
context.format_file(path, &module, is_macro_def)?;
119149
}

src/items.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,15 +1535,15 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
15351535
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
15361536
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
15371537
match (visitor_kind, &op_ty) {
1538-
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(ref op_bounds)) => {
1538+
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(op_bounds)) => {
15391539
let op = OpaqueType { bounds: op_bounds };
15401540
rewrite_ty(rw_info, Some(bounds), Some(&op), vis)
15411541
}
15421542
(Item(_) | AssocTraitItem(_) | ForeignItem(_), None) => {
15431543
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
15441544
}
15451545
(AssocImplItem(_), _) => {
1546-
let result = if let Some(ref op_bounds) = op_ty {
1546+
let result = if let Some(op_bounds) = op_ty {
15471547
let op = OpaqueType { bounds: op_bounds };
15481548
rewrite_ty(rw_info, Some(bounds), Some(&op), &DEFAULT_VISIBILITY)
15491549
} else {
@@ -3124,7 +3124,7 @@ impl Rewrite for ast::ForeignItem {
31243124
let inner_attrs = inner_attributes(&self.attrs);
31253125
let fn_ctxt = visit::FnCtxt::Foreign;
31263126
visitor.visit_fn(
3127-
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
3127+
visit::FnKind::Fn(fn_ctxt, self.ident, sig, &self.vis, Some(body)),
31283128
generics,
31293129
&sig.decl,
31303130
self.span,
@@ -3137,7 +3137,7 @@ impl Rewrite for ast::ForeignItem {
31373137
context,
31383138
shape.indent,
31393139
self.ident,
3140-
&FnSig::from_method_sig(&sig, generics, &self.vis),
3140+
&FnSig::from_method_sig(sig, generics, &self.vis),
31413141
span,
31423142
FnBraceStyle::None,
31433143
)
@@ -3166,7 +3166,7 @@ impl Rewrite for ast::ForeignItem {
31663166
.map(|s| s + ";")
31673167
}
31683168
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
3169-
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);
3169+
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);
31703170
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
31713171
}
31723172
ast::ForeignItemKind::MacCall(ref mac) => {

src/lists.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,8 @@ where
448448
true
449449
} else if starts_with_newline(comment) {
450450
false
451-
} else if comment.trim().contains('\n') || comment.trim().len() > width {
452-
true
453451
} else {
454-
false
452+
comment.trim().contains('\n') || comment.trim().len() > width
455453
};
456454

457455
rewrite_comment(

src/patterns.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,20 @@ fn rewrite_struct_pat(
318318
let mut fields_str = write_list(&item_vec, &fmt)?;
319319
let one_line_width = h_shape.map_or(0, |shape| shape.width);
320320

321+
let has_trailing_comma = fmt.needs_trailing_separator();
322+
321323
if ellipsis {
322324
if fields_str.contains('\n') || fields_str.len() > one_line_width {
323325
// Add a missing trailing comma.
324-
if context.config.trailing_comma() == SeparatorTactic::Never {
326+
if !has_trailing_comma {
325327
fields_str.push(',');
326328
}
327329
fields_str.push('\n');
328330
fields_str.push_str(&nested_shape.indent.to_string(context.config));
329331
} else {
330332
if !fields_str.is_empty() {
331333
// there are preceding struct fields being matched on
332-
if tactic == DefinitiveListTactic::Vertical {
333-
// if the tactic is Vertical, write_list already added a trailing ,
334+
if has_trailing_comma {
334335
fields_str.push(' ');
335336
} else {
336337
fields_str.push_str(", ");

src/test/configuration_snippet.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,7 @@ impl ConfigCodeBlock {
110110
assert!(self.code_block.is_some() && self.code_block_start.is_some());
111111

112112
// See if code block begins with #![rustfmt::skip].
113-
let fmt_skip = self
114-
.code_block
115-
.as_ref()
116-
.unwrap()
117-
.lines()
118-
.nth(0)
119-
.unwrap_or("")
120-
== "#![rustfmt::skip]";
113+
let fmt_skip = self.fmt_skip();
121114

122115
if self.config_name.is_none() && !fmt_skip {
123116
write_message(&format!(
@@ -138,6 +131,17 @@ impl ConfigCodeBlock {
138131
true
139132
}
140133

134+
/// True if the code block starts with #![rustfmt::skip]
135+
fn fmt_skip(&self) -> bool {
136+
self.code_block
137+
.as_ref()
138+
.unwrap()
139+
.lines()
140+
.nth(0)
141+
.unwrap_or("")
142+
== "#![rustfmt::skip]"
143+
}
144+
141145
fn has_parsing_errors<T: Write>(&self, session: &Session<'_, T>) -> bool {
142146
if session.has_parsing_errors() {
143147
write_message(&format!(
@@ -251,6 +255,7 @@ fn configuration_snippet_tests() {
251255
let blocks = get_code_blocks();
252256
let failures = blocks
253257
.iter()
258+
.filter(|block| !block.fmt_skip())
254259
.map(ConfigCodeBlock::formatted_is_idempotent)
255260
.fold(0, |acc, r| acc + (!r as u32));
256261

src/test/mod_resolver.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ fn out_of_line_nested_inline_within_out_of_line() {
4141
],
4242
);
4343
}
44+
45+
#[test]
46+
fn skip_out_of_line_nested_inline_within_out_of_line() {
47+
// See also https://github.com/rust-lang/rustfmt/issues/5065
48+
verify_mod_resolution(
49+
"tests/mod-resolver/skip-files-issue-5065/main.rs",
50+
&["tests/mod-resolver/skip-files-issue-5065/one.rs"],
51+
);
52+
}

src/visitor.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
552552
_ => visit::FnCtxt::Foreign,
553553
};
554554
self.visit_fn(
555-
visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)),
555+
visit::FnKind::Fn(fn_ctxt, item.ident, sig, &item.vis, Some(body)),
556556
generics,
557557
&sig.decl,
558558
item.span,
@@ -562,14 +562,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
562562
} else {
563563
let indent = self.block_indent;
564564
let rewrite = self.rewrite_required_fn(
565-
indent, item.ident, &sig, &item.vis, generics, item.span,
565+
indent, item.ident, sig, &item.vis, generics, item.span,
566566
);
567567
self.push_rewrite(item.span, rewrite);
568568
}
569569
}
570570
ast::ItemKind::TyAlias(ref ty_alias) => {
571571
use ItemVisitorKind::Item;
572-
self.visit_ty_alias_kind(ty_alias, &Item(&item), item.span);
572+
self.visit_ty_alias_kind(ty_alias, &Item(item), item.span);
573573
}
574574
ast::ItemKind::GlobalAsm(..) => {
575575
let snippet = Some(self.snippet(item.span).to_owned());
@@ -619,17 +619,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
619619
skip_out_of_file_lines_range_visitor!(self, ai.span);
620620

621621
if self.visit_attrs(&ai.attrs, ast::AttrStyle::Outer) {
622-
self.push_skipped_with_span(&ai.attrs.as_slice(), skip_span, skip_span);
622+
self.push_skipped_with_span(ai.attrs.as_slice(), skip_span, skip_span);
623623
return;
624624
}
625625

626626
// TODO(calebcartwright): consider enabling box_patterns feature gate
627627
match (&ai.kind, visitor_kind) {
628628
(ast::AssocItemKind::Const(..), AssocTraitItem(_)) => {
629-
self.visit_static(&StaticParts::from_trait_item(&ai))
629+
self.visit_static(&StaticParts::from_trait_item(ai))
630630
}
631631
(ast::AssocItemKind::Const(..), AssocImplItem(_)) => {
632-
self.visit_static(&StaticParts::from_impl_item(&ai))
632+
self.visit_static(&StaticParts::from_impl_item(ai))
633633
}
634634
(ast::AssocItemKind::Fn(ref fn_kind), _) => {
635635
let ast::Fn {
@@ -948,12 +948,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
948948

949949
pub(crate) fn format_separate_mod(&mut self, m: &Module<'_>, end_pos: BytePos) {
950950
self.block_indent = Indent::empty();
951-
if self.visit_attrs(m.attrs(), ast::AttrStyle::Inner) {
952-
self.push_skipped_with_span(m.attrs(), m.span, m.span);
953-
} else {
954-
self.walk_mod_items(&m.items);
955-
self.format_missing_with_indent(end_pos);
956-
}
951+
let skipped = self.visit_attrs(m.attrs(), ast::AttrStyle::Inner);
952+
assert!(
953+
!skipped,
954+
"Skipping module must be handled before reaching this line."
955+
);
956+
self.walk_mod_items(&m.items);
957+
self.format_missing_with_indent(end_pos);
957958
}
958959

959960
pub(crate) fn skip_empty_lines(&mut self, end_pos: BytePos) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![rustfmt::skip]
2+
3+
mod bar {
4+
5+
mod baz;}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn baz() { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![rustfmt::skip]
2+
3+
mod foo;
4+
mod one;
5+
6+
fn main() {println!("Hello, world!");
7+
}
8+
9+
// trailing commet
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct One { value: String }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Always
2+
// rustfmt-struct_lit_single_line: false
3+
// rustfmt-struct_lit_width: 0
4+
5+
fn main() {
6+
let Foo {
7+
a,
8+
..
9+
} = b;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Never
2+
// rustfmt-struct_lit_single_line: false
3+
// rustfmt-struct_lit_width: 0
4+
5+
fn main() {
6+
let Foo {
7+
a,
8+
..
9+
} = b;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Always
2+
// rustfmt-struct_lit_single_line: false
3+
4+
// There is an issue with how this is formatted.
5+
// formatting should look like ./multi_line_struct_trailing_comma_always_struct_lit_width_0.rs
6+
fn main() {
7+
let Foo {
8+
a, ..
9+
} = b;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Never
2+
// rustfmt-struct_lit_single_line: false
3+
4+
// There is an issue with how this is formatted.
5+
// formatting should look like ./multi_line_struct_trailing_comma_never_struct_lit_width_0.rs
6+
fn main() {
7+
let Foo {
8+
a, ..
9+
} = b;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-trailing_comma: Always
2+
3+
fn main() {
4+
let Foo { a, .. } = b;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-trailing_comma: Never
2+
3+
fn main() {
4+
let Foo { a, .. } = b;
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![rustfmt::skip]
2+
3+
fn main() {
4+
println!("Hello, world!");
5+
}
6+
7+
// Trailing Comment

0 commit comments

Comments
 (0)