Skip to content

Commit 993782c

Browse files
committed
Merge branch 'master' into stdin_emit
2 parents e84a378 + e0077aa commit 993782c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1698
-865
lines changed

Cargo.lock

Lines changed: 79 additions & 117 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "rustfmt-nightly"
4-
version = "1.4.9"
4+
version = "1.4.10"
55
authors = ["Nicholas Cameron <[email protected]>", "The Rustfmt developers"]
66
description = "Tool to find and fix Rust formatting issues"
77
repository = "https://github.com/rust-lang/rustfmt"
@@ -56,6 +56,7 @@ ignore = "0.4.6"
5656
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
5757
structopt = "0.3"
5858
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
59+
lazy_static = "1.0.0"
5960

6061
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6162
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
@@ -74,5 +75,3 @@ version = "610.0.0"
7475
package = "rustc-ap-syntax_pos"
7576
version = "610.0.0"
7677

77-
[dev-dependencies]
78-
lazy_static = "1.0.0"

ci/integration.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set -ex
1515
# it again.
1616
#
1717
#which cargo-fmt || cargo install --force
18-
cargo install --path . --force
18+
cargo install --path . --force --locked
1919

2020
echo "Integration tests for: ${INTEGRATION}"
2121
cargo fmt -- --version

src/cargo-fmt/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub struct Opts {
5656
/// Format all packages (only usable in workspaces)
5757
#[structopt(long = "all")]
5858
format_all: bool,
59+
60+
/// Run rustfmt in check mode
61+
#[structopt(long = "check")]
62+
check: bool,
5963
}
6064

6165
fn main() {
@@ -104,6 +108,12 @@ fn execute() -> i32 {
104108

105109
let strategy = CargoFmtStrategy::from_opts(&opts);
106110
let mut rustfmt_args = opts.rustfmt_options;
111+
if opts.check {
112+
let check_flag = String::from("--check");
113+
if !rustfmt_args.contains(&check_flag) {
114+
rustfmt_args.push(check_flag);
115+
}
116+
}
107117
if let Some(message_format) = opts.message_format {
108118
if let Err(msg) = convert_message_format_to_rustfmt_args(&message_format, &mut rustfmt_args)
109119
{
@@ -544,6 +554,7 @@ mod cargo_fmt_tests {
544554
assert_eq!(false, o.quiet);
545555
assert_eq!(false, o.verbose);
546556
assert_eq!(false, o.version);
557+
assert_eq!(false, o.check);
547558
assert_eq!(empty, o.packages);
548559
assert_eq!(empty, o.rustfmt_options);
549560
assert_eq!(false, o.format_all);
@@ -562,13 +573,15 @@ mod cargo_fmt_tests {
562573
"p2",
563574
"--message-format",
564575
"short",
576+
"--check",
565577
"--",
566578
"--edition",
567579
"2018",
568580
]);
569581
assert_eq!(true, o.quiet);
570582
assert_eq!(false, o.verbose);
571583
assert_eq!(false, o.version);
584+
assert_eq!(true, o.check);
572585
assert_eq!(vec!["p1", "p2"], o.packages);
573586
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
574587
assert_eq!(false, o.format_all);
@@ -597,12 +610,12 @@ mod cargo_fmt_tests {
597610
fn mandatory_separator() {
598611
assert!(
599612
Opts::clap()
600-
.get_matches_from_safe(&["test", "--check"])
613+
.get_matches_from_safe(&["test", "--emit"])
601614
.is_err()
602615
);
603616
assert!(
604617
!Opts::clap()
605-
.get_matches_from_safe(&["test", "--", "--check"])
618+
.get_matches_from_safe(&["test", "--", "--emit"])
606619
.is_err()
607620
);
608621
}

src/closures.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use syntax::{ast, ptr};
33

44
use crate::attr::get_attrs_from_stmt;
55
use crate::config::lists::*;
6-
use crate::config::Version;
6+
use crate::config::{IndentStyle, SeparatorTactic, Version};
77
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
88
use crate::items::{span_hi_for_param, span_lo_for_param};
99
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
@@ -43,7 +43,7 @@ pub(crate) fn rewrite_closure(
4343

4444
if let ast::ExprKind::Block(ref block, _) = body.kind {
4545
// The body of the closure is an empty block.
46-
if block.stmts.is_empty() && !block_contains_comment(block, context.source_map) {
46+
if block.stmts.is_empty() && !block_contains_comment(context, block) {
4747
return body
4848
.rewrite(context, shape)
4949
.map(|s| format!("{} {}", prefix, s));
@@ -112,7 +112,7 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
112112
is_unsafe_block(block)
113113
|| block.stmts.len() > 1
114114
|| has_attributes
115-
|| block_contains_comment(block, context.source_map)
115+
|| block_contains_comment(context, block)
116116
|| prefix.contains('\n')
117117
}
118118

@@ -238,9 +238,16 @@ fn rewrite_closure_fn_decl(
238238
.shrink_left(is_async.len() + mover.len() + immovable.len())?
239239
.sub_width(4)?;
240240

241+
let indent_style = context.config.indent_style();
242+
241243
// 1 = |
242244
let param_offset = nested_shape.indent + 1;
243-
let param_shape = nested_shape.offset_left(1)?.visual_indent(0);
245+
let param_shape = match indent_style {
246+
IndentStyle::Block => {
247+
Shape::indented(shape.indent.block_indent(context.config), context.config)
248+
}
249+
IndentStyle::Visual => nested_shape.offset_left(1)?.visual_indent(0),
250+
};
244251
let ret_str = fn_decl.output.rewrite(context, param_shape)?;
245252

246253
let param_items = itemize_list(
@@ -273,10 +280,31 @@ fn rewrite_closure_fn_decl(
273280
.tactic(tactic)
274281
.preserve_newline(true);
275282
let list_str = write_list(&item_vec, &fmt)?;
276-
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str);
283+
let one_line_budget = context.budget(param_shape.indent.width());
284+
let multi_line_params = match indent_style {
285+
IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget,
286+
_ => false,
287+
};
288+
let put_params_in_block = multi_line_params && !item_vec.is_empty();
289+
let param_str = if put_params_in_block {
290+
let trailing_comma = match context.config.trailing_comma() {
291+
SeparatorTactic::Never => "",
292+
_ => ",",
293+
};
294+
format!(
295+
"{}{}{}{}",
296+
param_shape.indent.to_string_with_newline(context.config),
297+
&list_str,
298+
trailing_comma,
299+
shape.indent.to_string_with_newline(context.config)
300+
)
301+
} else {
302+
list_str
303+
};
304+
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str);
277305

278306
if !ret_str.is_empty() {
279-
if prefix.contains('\n') {
307+
if prefix.contains('\n') && !put_params_in_block {
280308
prefix.push('\n');
281309
prefix.push_str(&param_offset.to_string(context.config));
282310
} else {
@@ -304,7 +332,7 @@ pub(crate) fn rewrite_last_closure(
304332
ast::ExprKind::Block(ref block, _)
305333
if !is_unsafe_block(block)
306334
&& !context.inside_macro()
307-
&& is_simple_block(block, Some(&body.attrs), context.source_map) =>
335+
&& is_simple_block(context, block, Some(&body.attrs)) =>
308336
{
309337
stmt_expr(&block.stmts[0]).unwrap_or(body)
310338
}

src/comment.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'a> CommentStyle<'a> {
112112
}
113113
}
114114

115-
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
115+
pub(crate) fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
116116
if !normalize_comments {
117117
if orig.starts_with("/**") && !orig.starts_with("/**/") {
118118
CommentStyle::DoubleBullet
@@ -976,6 +976,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
976976

977977
pub(crate) trait FindUncommented {
978978
fn find_uncommented(&self, pat: &str) -> Option<usize>;
979+
fn find_last_uncommented(&self, pat: &str) -> Option<usize>;
979980
}
980981

981982
impl FindUncommented for str {
@@ -1001,6 +1002,19 @@ impl FindUncommented for str {
10011002
None => Some(self.len() - pat.len()),
10021003
}
10031004
}
1005+
1006+
fn find_last_uncommented(&self, pat: &str) -> Option<usize> {
1007+
if let Some(left) = self.find_uncommented(pat) {
1008+
let mut result = left;
1009+
// add 1 to use find_last_uncommented for &str after pat
1010+
while let Some(next) = self[(result + 1)..].find_last_uncommented(pat) {
1011+
result += next + 1;
1012+
}
1013+
Some(result)
1014+
} else {
1015+
None
1016+
}
1017+
}
10041018
}
10051019

10061020
// Returns the first byte position after the first comment. The given string
@@ -1556,10 +1570,10 @@ pub(crate) fn recover_comment_removed(
15561570
// We missed some comments. Warn and keep the original text.
15571571
if context.config.error_on_unformatted() {
15581572
context.report.append(
1559-
context.source_map.span_to_filename(span).into(),
1573+
context.parse_sess.span_to_filename(span),
15601574
vec![FormattingError::from_span(
15611575
span,
1562-
&context.source_map,
1576+
&context.parse_sess,
15631577
ErrorKind::LostComment,
15641578
)],
15651579
);
@@ -1882,6 +1896,16 @@ mod test {
18821896
check("\"/* abc", "abc", Some(4));
18831897
}
18841898

1899+
#[test]
1900+
fn test_find_last_uncommented() {
1901+
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1902+
assert_eq!(expected, haystack.find_last_uncommented(needle));
1903+
}
1904+
check("foo test bar test", "test", Some(13));
1905+
check("test,", "test", Some(0));
1906+
check("nothing", "test", None);
1907+
}
1908+
18851909
#[test]
18861910
fn test_filter_normal_code() {
18871911
let s = r#"

0 commit comments

Comments
 (0)