Skip to content

Commit 2e09276

Browse files
authored
Merge branch 'master' into master
2 parents cd19e80 + 2d9bc46 commit 2e09276

39 files changed

+978
-386
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,31 @@ rustfmt-format-diff = []
3333
generic-simd = ["bytecount/generic-simd"]
3434

3535
[dependencies]
36-
itertools = "0.9"
37-
toml = "0.5"
38-
serde = { version = "1.0", features = ["derive"] }
39-
serde_json = "1.0"
40-
unicode-segmentation = "1.0.0"
41-
regex = "1.0"
42-
term = "0.6"
36+
annotate-snippets = { version = "0.9", features = ["color"] }
37+
anyhow = "1.0"
38+
bytecount = "0.6"
39+
cargo_metadata = "0.14"
40+
clap = { version = "3.1", features = ["derive"] }
41+
derive-new = "0.5"
4342
diff = "0.1"
44-
log = "0.4.14"
45-
env_logger = "0.8"
43+
dirs = "4.0"
44+
env_logger = "0.9"
4645
getopts = "0.2"
47-
derive-new = "0.5"
48-
cargo_metadata = "0.14"
49-
bytecount = "0.6"
50-
unicode-width = "0.1.5"
51-
unicode_categories = "0.1.1"
52-
dirs = "2.0.1"
53-
ignore = "0.4.17"
54-
annotate-snippets = { version = "0.8", features = ["color"] }
55-
structopt = "0.3"
56-
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
57-
lazy_static = "1.0.0"
58-
anyhow = "1.0"
46+
ignore = "0.4"
47+
itertools = "0.10"
48+
lazy_static = "1.4"
49+
log = "0.4"
50+
regex = "1.5"
51+
serde = { version = "1.0", features = ["derive"] }
52+
serde_json = "1.0"
53+
term = "0.7"
5954
thiserror = "1.0"
55+
toml = "0.5"
56+
unicode-segmentation = "1.9"
57+
unicode-width = "0.1"
58+
unicode_categories = "0.1"
59+
60+
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
6061

6162
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6263
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`

Configurations.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,13 +2200,47 @@ specific version of rustfmt is used in your CI, use this option.
22002200
- **Possible values**: any published version (e.g. `"0.3.8"`)
22012201
- **Stable**: No (tracking issue: [#3386](https://github.com/rust-lang/rustfmt/issues/3386))
22022202

2203+
## `short_array_element_width_threshold`
2204+
2205+
The width threshold for an array element to be considered "short".
2206+
2207+
The layout of an array is dependent on the length of each of its elements.
2208+
If the length of every element in an array is below this threshold (all elements are "short") then the array can be formatted in the mixed/compressed style, but if any one element has a length that exceeds this threshold then the array elements will have to be formatted vertically.
2209+
2210+
- **Default value**: `10`
2211+
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
2212+
- **Stable**: Yes
2213+
2214+
#### `10` (default):
2215+
```rust
2216+
fn main() {
2217+
pub const FORMAT_TEST: [u64; 5] = [
2218+
0x0000000000000000,
2219+
0xaaaaaaaaaaaaaaaa,
2220+
0xbbbbbbbbbbbbbbbb,
2221+
0xcccccccccccccccc,
2222+
0xdddddddddddddddd,
2223+
];
2224+
}
2225+
```
2226+
#### `20`:
2227+
```rust
2228+
fn main() {
2229+
pub const FORMAT_TEST: [u64; 5] = [
2230+
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
2231+
0xdddddddddddddddd,
2232+
];
2233+
}
2234+
```
2235+
See also [`max_width`](#max_width).
2236+
22032237
## `skip_children`
22042238

22052239
Don't reformat out of line modules
22062240

22072241
- **Default value**: `false`
22082242
- **Possible values**: `true`, `false`
2209-
- **Stable**: No (tracking issue: [#3389](https://github.com/rust-lang/rustfmt/issues/3386))
2243+
- **Stable**: No (tracking issue: [#3389](https://github.com/rust-lang/rustfmt/issues/3389))
22102244

22112245
## `single_line_if_else_max_width`
22122246

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2022-01-23"
2+
channel = "nightly-2022-03-27"
33
components = ["rustc-dev"]

src/attr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ impl Rewrite for [ast::Attribute] {
389389
let mut attrs = self;
390390
let mut result = String::new();
391391

392+
// Determine if the source text is annotated with `#[rustfmt::skip::attributes(derive)]`
393+
// or `#![rustfmt::skip::attributes(derive)]`
394+
let skip_derives = context.skip_context.skip_attribute("derive");
395+
392396
// This is not just a simple map because we need to handle doc comments
393397
// (where we take as many doc comment attributes as possible) and possibly
394398
// merging derives into a single attribute.
@@ -431,7 +435,7 @@ impl Rewrite for [ast::Attribute] {
431435
}
432436

433437
// Handle derives if we will merge them.
434-
if context.config.merge_derives() && is_derive(&attrs[0]) {
438+
if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
435439
let derives = take_while_with_pred(context, attrs, is_derive);
436440
let derive_str = format_derive(derives, shape, context)?;
437441
result.push_str(&derive_str);

src/cargo-fmt/main.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,59 @@ use std::path::{Path, PathBuf};
1515
use std::process::Command;
1616
use std::str;
1717

18-
use structopt::StructOpt;
18+
use clap::{CommandFactory, Parser};
1919

2020
#[path = "test/mod.rs"]
2121
#[cfg(test)]
2222
mod cargo_fmt_tests;
2323

24-
#[derive(StructOpt, Debug)]
25-
#[structopt(
24+
#[derive(Parser)]
25+
#[clap(
2626
bin_name = "cargo fmt",
2727
about = "This utility formats all bin and lib files of \
2828
the current crate using rustfmt."
2929
)]
3030
pub struct Opts {
3131
/// No output printed to stdout
32-
#[structopt(short = "q", long = "quiet")]
32+
#[clap(short = 'q', long = "quiet")]
3333
quiet: bool,
3434

3535
/// Use verbose output
36-
#[structopt(short = "v", long = "verbose")]
36+
#[clap(short = 'v', long = "verbose")]
3737
verbose: bool,
3838

3939
/// Print rustfmt version and exit
40-
#[structopt(long = "version")]
40+
#[clap(long = "version")]
4141
version: bool,
4242

4343
/// Specify package to format
44-
#[structopt(short = "p", long = "package", value_name = "package")]
44+
#[clap(
45+
short = 'p',
46+
long = "package",
47+
value_name = "package",
48+
multiple_values = true
49+
)]
4550
packages: Vec<String>,
4651

4752
/// Specify path to Cargo.toml
48-
#[structopt(long = "manifest-path", value_name = "manifest-path")]
53+
#[clap(long = "manifest-path", value_name = "manifest-path")]
4954
manifest_path: Option<String>,
5055

5156
/// Specify message-format: short|json|human
52-
#[structopt(long = "message-format", value_name = "message-format")]
57+
#[clap(long = "message-format", value_name = "message-format")]
5358
message_format: Option<String>,
5459

5560
/// Options passed to rustfmt
5661
// 'raw = true' to make `--` explicit.
57-
#[structopt(name = "rustfmt_options", raw(true))]
62+
#[clap(name = "rustfmt_options", raw(true))]
5863
rustfmt_options: Vec<String>,
5964

6065
/// Format all packages, and also their local path-based dependencies
61-
#[structopt(long = "all")]
66+
#[clap(long = "all")]
6267
format_all: bool,
6368

6469
/// Run rustfmt in check mode
65-
#[structopt(long = "check")]
70+
#[clap(long = "check")]
6671
check: bool,
6772
}
6873

@@ -87,7 +92,7 @@ fn execute() -> i32 {
8792
}
8893
});
8994

90-
let opts = Opts::from_iter(args);
95+
let opts = Opts::parse_from(args);
9196

9297
let verbosity = match (opts.verbose, opts.quiet) {
9398
(false, false) => Verbosity::Normal,
@@ -204,7 +209,7 @@ fn convert_message_format_to_rustfmt_args(
204209

205210
fn print_usage_to_stderr(reason: &str) {
206211
eprintln!("{}", reason);
207-
let app = Opts::clap();
212+
let app = Opts::command();
208213
app.after_help("")
209214
.write_help(&mut io::stderr())
210215
.expect("failed to write to stderr");

src/cargo-fmt/test/mod.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod targets;
66
#[test]
77
fn default_options() {
88
let empty: Vec<String> = vec![];
9-
let o = Opts::from_iter(&empty);
9+
let o = Opts::parse_from(&empty);
1010
assert_eq!(false, o.quiet);
1111
assert_eq!(false, o.verbose);
1212
assert_eq!(false, o.version);
@@ -20,7 +20,7 @@ fn default_options() {
2020

2121
#[test]
2222
fn good_options() {
23-
let o = Opts::from_iter(&[
23+
let o = Opts::parse_from(&[
2424
"test",
2525
"-q",
2626
"-p",
@@ -47,38 +47,38 @@ fn good_options() {
4747
#[test]
4848
fn unexpected_option() {
4949
assert!(
50-
Opts::clap()
51-
.get_matches_from_safe(&["test", "unexpected"])
50+
Opts::command()
51+
.try_get_matches_from(&["test", "unexpected"])
5252
.is_err()
5353
);
5454
}
5555

5656
#[test]
5757
fn unexpected_flag() {
5858
assert!(
59-
Opts::clap()
60-
.get_matches_from_safe(&["test", "--flag"])
59+
Opts::command()
60+
.try_get_matches_from(&["test", "--flag"])
6161
.is_err()
6262
);
6363
}
6464

6565
#[test]
6666
fn mandatory_separator() {
6767
assert!(
68-
Opts::clap()
69-
.get_matches_from_safe(&["test", "--emit"])
68+
Opts::command()
69+
.try_get_matches_from(&["test", "--emit"])
7070
.is_err()
7171
);
7272
assert!(
73-
!Opts::clap()
74-
.get_matches_from_safe(&["test", "--", "--emit"])
73+
!Opts::command()
74+
.try_get_matches_from(&["test", "--", "--emit"])
7575
.is_err()
7676
);
7777
}
7878

7979
#[test]
8080
fn multiple_packages_one_by_one() {
81-
let o = Opts::from_iter(&[
81+
let o = Opts::parse_from(&[
8282
"test",
8383
"-p",
8484
"package1",
@@ -92,7 +92,7 @@ fn multiple_packages_one_by_one() {
9292

9393
#[test]
9494
fn multiple_packages_grouped() {
95-
let o = Opts::from_iter(&[
95+
let o = Opts::parse_from(&[
9696
"test",
9797
"--package",
9898
"package1",
@@ -106,32 +106,36 @@ fn multiple_packages_grouped() {
106106

107107
#[test]
108108
fn empty_packages_1() {
109-
assert!(Opts::clap().get_matches_from_safe(&["test", "-p"]).is_err());
109+
assert!(
110+
Opts::command()
111+
.try_get_matches_from(&["test", "-p"])
112+
.is_err()
113+
);
110114
}
111115

112116
#[test]
113117
fn empty_packages_2() {
114118
assert!(
115-
Opts::clap()
116-
.get_matches_from_safe(&["test", "-p", "--", "--check"])
119+
Opts::command()
120+
.try_get_matches_from(&["test", "-p", "--", "--check"])
117121
.is_err()
118122
);
119123
}
120124

121125
#[test]
122126
fn empty_packages_3() {
123127
assert!(
124-
Opts::clap()
125-
.get_matches_from_safe(&["test", "-p", "--verbose"])
128+
Opts::command()
129+
.try_get_matches_from(&["test", "-p", "--verbose"])
126130
.is_err()
127131
);
128132
}
129133

130134
#[test]
131135
fn empty_packages_4() {
132136
assert!(
133-
Opts::clap()
134-
.get_matches_from_safe(&["test", "-p", "--check"])
137+
Opts::command()
138+
.try_get_matches_from(&["test", "-p", "--check"])
135139
.is_err()
136140
);
137141
}

src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ create_config! {
106106
// Misc.
107107
remove_nested_parens: bool, true, true, "Remove nested parens";
108108
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
109+
short_array_element_width_threshold: usize, 10, true,
110+
"Width threshold for an array element to be considered short";
109111
overflow_delimited_expr: bool, false, false,
110112
"Allow trailing bracket/brace delimited expressions to overflow";
111113
struct_field_align_threshold: usize, 0, false,
@@ -591,6 +593,7 @@ spaces_around_ranges = false
591593
binop_separator = "Front"
592594
remove_nested_parens = true
593595
combine_control_expr = true
596+
short_array_element_width_threshold = 10
594597
overflow_delimited_expr = false
595598
struct_field_align_threshold = 0
596599
enum_discrim_align_threshold = 0

src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ fn rewrite_struct_lit<'a>(
15331533
enum StructLitField<'a> {
15341534
Regular(&'a ast::ExprField),
15351535
Base(&'a ast::Expr),
1536-
Rest(&'a Span),
1536+
Rest(Span),
15371537
}
15381538

15391539
// 2 = " {".len()
@@ -1568,7 +1568,7 @@ fn rewrite_struct_lit<'a>(
15681568
let field_iter = fields.iter().map(StructLitField::Regular).chain(
15691569
match struct_rest {
15701570
ast::StructRest::Base(expr) => Some(StructLitField::Base(&**expr)),
1571-
ast::StructRest::Rest(span) => Some(StructLitField::Rest(span)),
1571+
ast::StructRest::Rest(span) => Some(StructLitField::Rest(*span)),
15721572
ast::StructRest::None => None,
15731573
}
15741574
.into_iter(),

0 commit comments

Comments
 (0)