Merge pull request #2384 from topecongiro/init-shorthand
Use field initialization shorthand if possible
This commit is contained in:
commit
0294a79b5b
24 changed files with 218 additions and 107 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- Add `use_field_init_shorthand` config option.
|
||||||
- Add `reorder_modules` configuration option.
|
- Add `reorder_modules` configuration option.
|
||||||
|
|
||||||
## [0.3.6] 2018-01-18
|
## [0.3.6] 2018-01-18
|
||||||
|
|
|
@ -1781,6 +1781,48 @@ fn lorem<Ipsum: Dolor+Sit=Amet>() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `use_field_init_shorthand`
|
||||||
|
|
||||||
|
Use field initialize shorthand if possible.
|
||||||
|
|
||||||
|
- **Default value**: `false`
|
||||||
|
- **Possible values**: `true`, `false`
|
||||||
|
- **Stable**: No
|
||||||
|
|
||||||
|
#### `false` (default):
|
||||||
|
|
||||||
|
```rust
|
||||||
|
struct Foo {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
z: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 1;
|
||||||
|
let y = 2;
|
||||||
|
let z = 3;
|
||||||
|
let a = Foo { x: x, y: y, z: z };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `true`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
struct Foo {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
z: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 1;
|
||||||
|
let y = 2;
|
||||||
|
let z = 3;
|
||||||
|
let a = Foo { x, y, z };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## `use_try_shorthand`
|
## `use_try_shorthand`
|
||||||
|
|
||||||
Replace uses of the try! macro by the ? shorthand
|
Replace uses of the try! macro by the ? shorthand
|
||||||
|
|
|
@ -453,7 +453,7 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
|
||||||
|
|
||||||
return Ok(Operation::Stdin {
|
return Ok(Operation::Stdin {
|
||||||
input: buffer,
|
input: buffer,
|
||||||
config_path: config_path,
|
config_path,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,8 +469,8 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Operation::Format {
|
Ok(Operation::Format {
|
||||||
files: files,
|
files,
|
||||||
config_path: config_path,
|
config_path,
|
||||||
minimal_config_path: minimal_config_path,
|
minimal_config_path,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -254,7 +254,7 @@ fn rewrite_closure_fn_decl(
|
||||||
};
|
};
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: SeparatorTactic::Never,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
|
|
|
@ -290,11 +290,11 @@ fn rewrite_comment_inner(
|
||||||
let mut fmt = StringFormat {
|
let mut fmt = StringFormat {
|
||||||
opener: "",
|
opener: "",
|
||||||
closer: "",
|
closer: "",
|
||||||
line_start: line_start,
|
line_start,
|
||||||
line_end: "",
|
line_end: "",
|
||||||
shape: Shape::legacy(max_chars, fmt_indent),
|
shape: Shape::legacy(max_chars, fmt_indent),
|
||||||
trim_end: true,
|
trim_end: true,
|
||||||
config: config,
|
config,
|
||||||
};
|
};
|
||||||
|
|
||||||
let line_breaks = count_newlines(orig.trim_right());
|
let line_breaks = count_newlines(orig.trim_right());
|
||||||
|
@ -900,7 +900,7 @@ pub struct CommentCodeSlices<'a> {
|
||||||
impl<'a> CommentCodeSlices<'a> {
|
impl<'a> CommentCodeSlices<'a> {
|
||||||
pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
|
pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
|
||||||
CommentCodeSlices {
|
CommentCodeSlices {
|
||||||
slice: slice,
|
slice,
|
||||||
last_slice_kind: CodeCharKind::Comment,
|
last_slice_kind: CodeCharKind::Comment,
|
||||||
last_slice_end: 0,
|
last_slice_end: 0,
|
||||||
}
|
}
|
||||||
|
@ -1024,7 +1024,7 @@ impl<'a> CommentReducer<'a> {
|
||||||
let is_block = comment.starts_with("/*");
|
let is_block = comment.starts_with("/*");
|
||||||
let comment = remove_comment_header(comment);
|
let comment = remove_comment_header(comment);
|
||||||
CommentReducer {
|
CommentReducer {
|
||||||
is_block: is_block,
|
is_block,
|
||||||
at_start_line: false, // There are no supplementary '*' on the first line
|
at_start_line: false, // There are no supplementary '*' on the first line
|
||||||
iter: comment.chars(),
|
iter: comment.chars(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -674,6 +674,7 @@ create_config! {
|
||||||
condense_wildcard_suffixes: bool, false, false, "Replace strings of _ wildcards by a single .. \
|
condense_wildcard_suffixes: bool, false, false, "Replace strings of _ wildcards by a single .. \
|
||||||
in tuple patterns";
|
in tuple patterns";
|
||||||
force_explicit_abi: bool, true, true, "Always print the abi for extern items";
|
force_explicit_abi: bool, true, true, "Always print the abi for extern items";
|
||||||
|
use_field_init_shorthand: bool, false, false, "Use field initialization shorthand if possible";
|
||||||
|
|
||||||
// Control options (changes the operation of rustfmt, rather than the formatting)
|
// Control options (changes the operation of rustfmt, rather than the formatting)
|
||||||
write_mode: WriteMode, WriteMode::Overwrite, false,
|
write_mode: WriteMode, WriteMode::Overwrite, false,
|
||||||
|
|
60
src/expr.rs
60
src/expr.rs
|
@ -449,7 +449,7 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
|
||||||
let ends_with_newline = tactic.ends_with_newline(context.config.indent_style());
|
let ends_with_newline = tactic.ends_with_newline(context.config.indent_style());
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if trailing_comma {
|
trailing_separator: if trailing_comma {
|
||||||
SeparatorTactic::Always
|
SeparatorTactic::Always
|
||||||
|
@ -470,7 +470,7 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
|
||||||
},
|
},
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: nested_shape,
|
shape: nested_shape,
|
||||||
ends_with_newline: ends_with_newline,
|
ends_with_newline,
|
||||||
preserve_newline: false,
|
preserve_newline: false,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
};
|
};
|
||||||
|
@ -787,35 +787,35 @@ impl<'a> ControlFlow<'a> {
|
||||||
) -> ControlFlow<'a> {
|
) -> ControlFlow<'a> {
|
||||||
ControlFlow {
|
ControlFlow {
|
||||||
cond: Some(cond),
|
cond: Some(cond),
|
||||||
block: block,
|
block,
|
||||||
else_block: else_block,
|
else_block,
|
||||||
label: None,
|
label: None,
|
||||||
pat: pat,
|
pat,
|
||||||
keyword: "if",
|
keyword: "if",
|
||||||
matcher: match pat {
|
matcher: match pat {
|
||||||
Some(..) => "let",
|
Some(..) => "let",
|
||||||
None => "",
|
None => "",
|
||||||
},
|
},
|
||||||
connector: " =",
|
connector: " =",
|
||||||
allow_single_line: allow_single_line,
|
allow_single_line,
|
||||||
nested_if: nested_if,
|
nested_if,
|
||||||
span: span,
|
span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_loop(block: &'a ast::Block, label: Option<ast::Label>, span: Span) -> ControlFlow<'a> {
|
fn new_loop(block: &'a ast::Block, label: Option<ast::Label>, span: Span) -> ControlFlow<'a> {
|
||||||
ControlFlow {
|
ControlFlow {
|
||||||
cond: None,
|
cond: None,
|
||||||
block: block,
|
block,
|
||||||
else_block: None,
|
else_block: None,
|
||||||
label: label,
|
label,
|
||||||
pat: None,
|
pat: None,
|
||||||
keyword: "loop",
|
keyword: "loop",
|
||||||
matcher: "",
|
matcher: "",
|
||||||
connector: "",
|
connector: "",
|
||||||
allow_single_line: false,
|
allow_single_line: false,
|
||||||
nested_if: false,
|
nested_if: false,
|
||||||
span: span,
|
span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -828,10 +828,10 @@ impl<'a> ControlFlow<'a> {
|
||||||
) -> ControlFlow<'a> {
|
) -> ControlFlow<'a> {
|
||||||
ControlFlow {
|
ControlFlow {
|
||||||
cond: Some(cond),
|
cond: Some(cond),
|
||||||
block: block,
|
block,
|
||||||
else_block: None,
|
else_block: None,
|
||||||
label: label,
|
label,
|
||||||
pat: pat,
|
pat,
|
||||||
keyword: "while",
|
keyword: "while",
|
||||||
matcher: match pat {
|
matcher: match pat {
|
||||||
Some(..) => "let",
|
Some(..) => "let",
|
||||||
|
@ -840,7 +840,7 @@ impl<'a> ControlFlow<'a> {
|
||||||
connector: " =",
|
connector: " =",
|
||||||
allow_single_line: false,
|
allow_single_line: false,
|
||||||
nested_if: false,
|
nested_if: false,
|
||||||
span: span,
|
span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -853,16 +853,16 @@ impl<'a> ControlFlow<'a> {
|
||||||
) -> ControlFlow<'a> {
|
) -> ControlFlow<'a> {
|
||||||
ControlFlow {
|
ControlFlow {
|
||||||
cond: Some(cond),
|
cond: Some(cond),
|
||||||
block: block,
|
block,
|
||||||
else_block: None,
|
else_block: None,
|
||||||
label: label,
|
label,
|
||||||
pat: Some(pat),
|
pat: Some(pat),
|
||||||
keyword: "for",
|
keyword: "for",
|
||||||
matcher: "",
|
matcher: "",
|
||||||
connector: " in",
|
connector: " in",
|
||||||
allow_single_line: false,
|
allow_single_line: false,
|
||||||
nested_if: false,
|
nested_if: false,
|
||||||
span: span,
|
span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1488,7 +1488,7 @@ fn rewrite_match_pattern(
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: " |",
|
separator: " |",
|
||||||
trailing_separator: SeparatorTactic::Never,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
separator_place: context.config.binop_separator(),
|
separator_place: context.config.binop_separator(),
|
||||||
|
@ -1992,7 +1992,7 @@ where
|
||||||
);
|
);
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if force_trailing_comma {
|
trailing_separator: if force_trailing_comma {
|
||||||
SeparatorTactic::Always
|
SeparatorTactic::Always
|
||||||
|
@ -2569,9 +2569,13 @@ pub fn rewrite_field(
|
||||||
if contains_skip(&field.attrs) {
|
if contains_skip(&field.attrs) {
|
||||||
return Some(context.snippet(field.span()).to_owned());
|
return Some(context.snippet(field.span()).to_owned());
|
||||||
}
|
}
|
||||||
let name = &field.ident.node.to_string();
|
let mut attrs_str = field.attrs.rewrite(context, shape)?;
|
||||||
|
if !attrs_str.is_empty() {
|
||||||
|
attrs_str.push_str(&format!("\n{}", shape.indent.to_string(context.config)));
|
||||||
|
};
|
||||||
|
let name = field.ident.node.to_string();
|
||||||
if field.is_shorthand {
|
if field.is_shorthand {
|
||||||
Some(name.to_string())
|
Some(attrs_str + &name)
|
||||||
} else {
|
} else {
|
||||||
let mut separator = String::from(struct_lit_field_separator(context.config));
|
let mut separator = String::from(struct_lit_field_separator(context.config));
|
||||||
for _ in 0..prefix_max_width.checked_sub(name.len()).unwrap_or(0) {
|
for _ in 0..prefix_max_width.checked_sub(name.len()).unwrap_or(0) {
|
||||||
|
@ -2581,12 +2585,10 @@ pub fn rewrite_field(
|
||||||
let expr_shape = shape.offset_left(overhead)?;
|
let expr_shape = shape.offset_left(overhead)?;
|
||||||
let expr = field.expr.rewrite(context, expr_shape);
|
let expr = field.expr.rewrite(context, expr_shape);
|
||||||
|
|
||||||
let mut attrs_str = field.attrs.rewrite(context, shape)?;
|
|
||||||
if !attrs_str.is_empty() {
|
|
||||||
attrs_str.push_str(&format!("\n{}", shape.indent.to_string(context.config)));
|
|
||||||
};
|
|
||||||
|
|
||||||
match expr {
|
match expr {
|
||||||
|
Some(ref e) if e.as_str() == name && context.config.use_field_init_shorthand() => {
|
||||||
|
Some(attrs_str + &name)
|
||||||
|
}
|
||||||
Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
|
Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
|
||||||
None => {
|
None => {
|
||||||
let expr_offset = shape.indent.block_indent(context.config);
|
let expr_offset = shape.indent.block_indent(context.config);
|
||||||
|
@ -2675,11 +2677,11 @@ where
|
||||||
nested_shape.width,
|
nested_shape.width,
|
||||||
);
|
);
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: SeparatorTactic::Never,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: shape,
|
shape,
|
||||||
ends_with_newline: false,
|
ends_with_newline: false,
|
||||||
preserve_newline: false,
|
preserve_newline: false,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
|
|
|
@ -34,7 +34,7 @@ impl<'a> From<&'a LineRange> for Range {
|
||||||
|
|
||||||
impl Range {
|
impl Range {
|
||||||
pub fn new(lo: usize, hi: usize) -> Range {
|
pub fn new(lo: usize, hi: usize) -> Range {
|
||||||
Range { lo: lo, hi: hi }
|
Range { lo, hi }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_empty(self) -> bool {
|
fn is_empty(self) -> bool {
|
||||||
|
|
|
@ -299,7 +299,7 @@ fn rewrite_imports(
|
||||||
separator: "",
|
separator: "",
|
||||||
trailing_separator: SeparatorTactic::Never,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: shape,
|
shape,
|
||||||
ends_with_newline: true,
|
ends_with_newline: true,
|
||||||
preserve_newline: false,
|
preserve_newline: false,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
|
@ -552,7 +552,7 @@ fn rewrite_nested_use_tree(
|
||||||
&& tactic != DefinitiveListTactic::Horizontal;
|
&& tactic != DefinitiveListTactic::Horizontal;
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if ends_with_newline {
|
trailing_separator: if ends_with_newline {
|
||||||
context.config.trailing_comma()
|
context.config.trailing_comma()
|
||||||
|
@ -561,7 +561,7 @@ fn rewrite_nested_use_tree(
|
||||||
},
|
},
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: nested_shape,
|
shape: nested_shape,
|
||||||
ends_with_newline: ends_with_newline,
|
ends_with_newline,
|
||||||
preserve_newline: true,
|
preserve_newline: true,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
};
|
};
|
||||||
|
|
|
@ -90,8 +90,8 @@ impl BadIssueSeeker {
|
||||||
todo_idx: 0,
|
todo_idx: 0,
|
||||||
fixme_idx: 0,
|
fixme_idx: 0,
|
||||||
},
|
},
|
||||||
report_todo: report_todo,
|
report_todo,
|
||||||
report_fixme: report_fixme,
|
report_fixme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,8 +169,8 @@ impl BadIssueSeeker {
|
||||||
}
|
}
|
||||||
|
|
||||||
Seeking::Issue {
|
Seeking::Issue {
|
||||||
todo_idx: todo_idx,
|
todo_idx,
|
||||||
fixme_idx: fixme_idx,
|
fixme_idx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,10 +213,7 @@ impl BadIssueSeeker {
|
||||||
NumberPart::CloseParen => {}
|
NumberPart::CloseParen => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.state = Seeking::Number {
|
self.state = Seeking::Number { part, issue };
|
||||||
part: part,
|
|
||||||
issue: issue,
|
|
||||||
};
|
|
||||||
|
|
||||||
IssueClassification::None
|
IssueClassification::None
|
||||||
}
|
}
|
||||||
|
|
46
src/items.rs
46
src/items.rs
|
@ -138,7 +138,7 @@ impl<'a> Item<'a> {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|i| BodyElement::ForeignItem(i))
|
.map(|i| BodyElement::ForeignItem(i))
|
||||||
.collect(),
|
.collect(),
|
||||||
span: span,
|
span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,8 +169,8 @@ impl<'a> FnSig<'a> {
|
||||||
vis: ast::Visibility,
|
vis: ast::Visibility,
|
||||||
) -> FnSig<'a> {
|
) -> FnSig<'a> {
|
||||||
FnSig {
|
FnSig {
|
||||||
decl: decl,
|
decl,
|
||||||
generics: generics,
|
generics,
|
||||||
abi: abi::Abi::Rust,
|
abi: abi::Abi::Rust,
|
||||||
constness: ast::Constness::NotConst,
|
constness: ast::Constness::NotConst,
|
||||||
defaultness: ast::Defaultness::Final,
|
defaultness: ast::Defaultness::Final,
|
||||||
|
@ -189,7 +189,7 @@ impl<'a> FnSig<'a> {
|
||||||
defaultness: ast::Defaultness::Final,
|
defaultness: ast::Defaultness::Final,
|
||||||
abi: method_sig.abi,
|
abi: method_sig.abi,
|
||||||
decl: &*method_sig.decl,
|
decl: &*method_sig.decl,
|
||||||
generics: generics,
|
generics,
|
||||||
visibility: ast::Visibility::Inherited,
|
visibility: ast::Visibility::Inherited,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,12 +202,12 @@ impl<'a> FnSig<'a> {
|
||||||
) -> FnSig<'a> {
|
) -> FnSig<'a> {
|
||||||
match *fn_kind {
|
match *fn_kind {
|
||||||
visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig {
|
visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig {
|
||||||
decl: decl,
|
decl,
|
||||||
generics: generics,
|
generics,
|
||||||
abi: abi,
|
abi,
|
||||||
constness: constness.node,
|
constness: constness.node,
|
||||||
defaultness: defualtness,
|
defaultness: defualtness,
|
||||||
unsafety: unsafety,
|
unsafety,
|
||||||
visibility: visibility.clone(),
|
visibility: visibility.clone(),
|
||||||
},
|
},
|
||||||
visit::FnKind::Method(_, method_sig, vis, _) => {
|
visit::FnKind::Method(_, method_sig, vis, _) => {
|
||||||
|
@ -510,7 +510,7 @@ impl<'a> FmtVisitor<'a> {
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: self.config.trailing_comma(),
|
trailing_separator: self.config.trailing_comma(),
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: shape,
|
shape,
|
||||||
ends_with_newline: true,
|
ends_with_newline: true,
|
||||||
preserve_newline: true,
|
preserve_newline: true,
|
||||||
config: self.config,
|
config: self.config,
|
||||||
|
@ -888,10 +888,10 @@ impl<'a> StructParts<'a> {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
StructParts {
|
StructParts {
|
||||||
prefix: prefix,
|
prefix,
|
||||||
ident: item.ident,
|
ident: item.ident,
|
||||||
vis: &item.vis,
|
vis: &item.vis,
|
||||||
def: def,
|
def,
|
||||||
generics: Some(generics),
|
generics: Some(generics),
|
||||||
span: item.span,
|
span: item.span,
|
||||||
}
|
}
|
||||||
|
@ -1502,11 +1502,11 @@ impl<'a> StaticParts<'a> {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
StaticParts {
|
StaticParts {
|
||||||
prefix: prefix,
|
prefix,
|
||||||
vis: &item.vis,
|
vis: &item.vis,
|
||||||
ident: item.ident,
|
ident: item.ident,
|
||||||
ty: ty,
|
ty,
|
||||||
mutability: mutability,
|
mutability,
|
||||||
expr_opt: Some(expr),
|
expr_opt: Some(expr),
|
||||||
defaultness: None,
|
defaultness: None,
|
||||||
span: item.span,
|
span: item.span,
|
||||||
|
@ -1522,7 +1522,7 @@ impl<'a> StaticParts<'a> {
|
||||||
prefix: "const",
|
prefix: "const",
|
||||||
vis: &ast::Visibility::Inherited,
|
vis: &ast::Visibility::Inherited,
|
||||||
ident: ti.ident,
|
ident: ti.ident,
|
||||||
ty: ty,
|
ty,
|
||||||
mutability: ast::Mutability::Immutable,
|
mutability: ast::Mutability::Immutable,
|
||||||
expr_opt: expr_opt.as_ref(),
|
expr_opt: expr_opt.as_ref(),
|
||||||
defaultness: None,
|
defaultness: None,
|
||||||
|
@ -1539,7 +1539,7 @@ impl<'a> StaticParts<'a> {
|
||||||
prefix: "const",
|
prefix: "const",
|
||||||
vis: &ii.vis,
|
vis: &ii.vis,
|
||||||
ident: ii.ident,
|
ident: ii.ident,
|
||||||
ty: ty,
|
ty,
|
||||||
mutability: ast::Mutability::Immutable,
|
mutability: ast::Mutability::Immutable,
|
||||||
expr_opt: Some(expr),
|
expr_opt: Some(expr),
|
||||||
defaultness: Some(ii.defaultness),
|
defaultness: Some(ii.defaultness),
|
||||||
|
@ -1811,7 +1811,7 @@ fn rewrite_fn_base(
|
||||||
let one_line_budget = context.budget(used_width + overhead);
|
let one_line_budget = context.budget(used_width + overhead);
|
||||||
let shape = Shape {
|
let shape = Shape {
|
||||||
width: one_line_budget,
|
width: one_line_budget,
|
||||||
indent: indent,
|
indent,
|
||||||
offset: used_width,
|
offset: used_width,
|
||||||
};
|
};
|
||||||
let fd = fn_sig.decl;
|
let fd = fn_sig.decl;
|
||||||
|
@ -2078,8 +2078,8 @@ struct WhereClauseOption {
|
||||||
impl WhereClauseOption {
|
impl WhereClauseOption {
|
||||||
pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
|
pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
|
||||||
WhereClauseOption {
|
WhereClauseOption {
|
||||||
suppress_comma: suppress_comma,
|
suppress_comma,
|
||||||
snuggle: snuggle,
|
snuggle,
|
||||||
compress_where: false,
|
compress_where: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2226,7 +2226,7 @@ fn rewrite_args(
|
||||||
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
|
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if variadic {
|
trailing_separator: if variadic {
|
||||||
SeparatorTactic::Never
|
SeparatorTactic::Never
|
||||||
|
@ -2397,7 +2397,7 @@ where
|
||||||
one_line_budget,
|
one_line_budget,
|
||||||
);
|
);
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if context.config.indent_style() == IndentStyle::Visual {
|
trailing_separator: if context.config.indent_style() == IndentStyle::Visual {
|
||||||
SeparatorTactic::Never
|
SeparatorTactic::Never
|
||||||
|
@ -2405,7 +2405,7 @@ where
|
||||||
context.config.trailing_comma()
|
context.config.trailing_comma()
|
||||||
},
|
},
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: shape,
|
shape,
|
||||||
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
|
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
|
||||||
preserve_newline: true,
|
preserve_newline: true,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
|
@ -2630,7 +2630,7 @@ fn rewrite_where_clause(
|
||||||
}
|
}
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: comma_tactic,
|
trailing_separator: comma_tactic,
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
|
|
|
@ -449,7 +449,7 @@ fn format_lines(
|
||||||
line: cur_line,
|
line: cur_line,
|
||||||
kind: error_kind,
|
kind: error_kind,
|
||||||
is_comment: kind.is_comment(),
|
is_comment: kind.is_comment(),
|
||||||
is_string: is_string,
|
is_string,
|
||||||
line_buffer: line_buffer.clone(),
|
line_buffer: line_buffer.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
32
src/lists.rs
32
src/lists.rs
|
@ -690,15 +690,15 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
ListItem {
|
ListItem {
|
||||||
pre_comment: pre_comment,
|
pre_comment,
|
||||||
pre_comment_style: pre_comment_style,
|
pre_comment_style,
|
||||||
item: if self.inner.peek().is_none() && self.leave_last {
|
item: if self.inner.peek().is_none() && self.leave_last {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
(self.get_item_string)(&item)
|
(self.get_item_string)(&item)
|
||||||
},
|
},
|
||||||
post_comment: post_comment,
|
post_comment,
|
||||||
new_lines: new_lines,
|
new_lines,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -724,16 +724,16 @@ where
|
||||||
F3: Fn(&T) -> Option<String>,
|
F3: Fn(&T) -> Option<String>,
|
||||||
{
|
{
|
||||||
ListItems {
|
ListItems {
|
||||||
codemap: codemap,
|
codemap,
|
||||||
inner: inner.peekable(),
|
inner: inner.peekable(),
|
||||||
get_lo: get_lo,
|
get_lo,
|
||||||
get_hi: get_hi,
|
get_hi,
|
||||||
get_item_string: get_item_string,
|
get_item_string,
|
||||||
prev_span_end: prev_span_end,
|
prev_span_end,
|
||||||
next_span_start: next_span_start,
|
next_span_start,
|
||||||
terminator: terminator,
|
terminator,
|
||||||
separator: separator,
|
separator,
|
||||||
leave_last: leave_last,
|
leave_last,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -841,7 +841,7 @@ pub fn struct_lit_formatting<'a>(
|
||||||
let ends_with_newline = context.config.indent_style() != IndentStyle::Visual
|
let ends_with_newline = context.config.indent_style() != IndentStyle::Visual
|
||||||
&& tactic == DefinitiveListTactic::Vertical;
|
&& tactic == DefinitiveListTactic::Vertical;
|
||||||
ListFormatting {
|
ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if force_no_trailing_comma {
|
trailing_separator: if force_no_trailing_comma {
|
||||||
SeparatorTactic::Never
|
SeparatorTactic::Never
|
||||||
|
@ -849,8 +849,8 @@ pub fn struct_lit_formatting<'a>(
|
||||||
context.config.trailing_comma()
|
context.config.trailing_comma()
|
||||||
},
|
},
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
shape: shape,
|
shape,
|
||||||
ends_with_newline: ends_with_newline,
|
ends_with_newline,
|
||||||
preserve_newline: true,
|
preserve_newline: true,
|
||||||
config: context.config,
|
config: context.config,
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub struct Mismatch {
|
||||||
impl Mismatch {
|
impl Mismatch {
|
||||||
fn new(line_number: u32) -> Mismatch {
|
fn new(line_number: u32) -> Mismatch {
|
||||||
Mismatch {
|
Mismatch {
|
||||||
line_number: line_number,
|
line_number,
|
||||||
lines: Vec::new(),
|
lines: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
src/shape.rs
16
src/shape.rs
|
@ -29,8 +29,8 @@ const INDENT_BUFFER: &str =
|
||||||
impl Indent {
|
impl Indent {
|
||||||
pub fn new(block_indent: usize, alignment: usize) -> Indent {
|
pub fn new(block_indent: usize, alignment: usize) -> Indent {
|
||||||
Indent {
|
Indent {
|
||||||
block_indent: block_indent,
|
block_indent,
|
||||||
alignment: alignment,
|
alignment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,8 +161,8 @@ impl Shape {
|
||||||
// |<--->| width
|
// |<--->| width
|
||||||
pub fn legacy(width: usize, indent: Indent) -> Shape {
|
pub fn legacy(width: usize, indent: Indent) -> Shape {
|
||||||
Shape {
|
Shape {
|
||||||
width: width,
|
width,
|
||||||
indent: indent,
|
indent,
|
||||||
offset: indent.alignment,
|
offset: indent.alignment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,7 +170,7 @@ impl Shape {
|
||||||
pub fn indented(indent: Indent, config: &Config) -> Shape {
|
pub fn indented(indent: Indent, config: &Config) -> Shape {
|
||||||
Shape {
|
Shape {
|
||||||
width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
|
width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
|
||||||
indent: indent,
|
indent,
|
||||||
offset: indent.alignment,
|
offset: indent.alignment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,9 +187,9 @@ impl Shape {
|
||||||
|
|
||||||
pub fn offset(width: usize, indent: Indent, offset: usize) -> Shape {
|
pub fn offset(width: usize, indent: Indent, offset: usize) -> Shape {
|
||||||
Shape {
|
Shape {
|
||||||
width: width,
|
width,
|
||||||
indent: indent,
|
indent,
|
||||||
offset: offset,
|
offset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,9 +36,9 @@ impl<'a> StringFormat<'a> {
|
||||||
closer: "\"",
|
closer: "\"",
|
||||||
line_start: " ",
|
line_start: " ",
|
||||||
line_end: "\\",
|
line_end: "\\",
|
||||||
shape: shape,
|
shape,
|
||||||
trim_end: false,
|
trim_end: false,
|
||||||
config: config,
|
config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,7 +352,7 @@ where
|
||||||
);
|
);
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: if !context.use_block_indent() || variadic {
|
trailing_separator: if !context.use_block_indent() || variadic {
|
||||||
SeparatorTactic::Never
|
SeparatorTactic::Never
|
||||||
|
|
|
@ -247,7 +247,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
|
||||||
);
|
);
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: tactic,
|
tactic,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: context.config.trailing_comma(),
|
trailing_separator: context.config.trailing_comma(),
|
||||||
separator_place: SeparatorPlace::Back,
|
separator_place: SeparatorPlace::Back,
|
||||||
|
|
|
@ -591,14 +591,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||||
snippet_provider: &'a SnippetProvider,
|
snippet_provider: &'a SnippetProvider,
|
||||||
) -> FmtVisitor<'a> {
|
) -> FmtVisitor<'a> {
|
||||||
FmtVisitor {
|
FmtVisitor {
|
||||||
parse_session: parse_session,
|
parse_session,
|
||||||
codemap: parse_session.codemap(),
|
codemap: parse_session.codemap(),
|
||||||
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
|
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
|
||||||
last_pos: BytePos(0),
|
last_pos: BytePos(0),
|
||||||
block_indent: Indent::empty(),
|
block_indent: Indent::empty(),
|
||||||
config: config,
|
config,
|
||||||
is_if_else_block: false,
|
is_if_else_block: false,
|
||||||
snippet_provider: snippet_provider,
|
snippet_provider,
|
||||||
line_number: 0,
|
line_number: 0,
|
||||||
skipped_range: vec![],
|
skipped_range: vec![],
|
||||||
}
|
}
|
||||||
|
|
19
tests/source/configs/use_field_init_shorthand/false.rs
Normal file
19
tests/source/configs/use_field_init_shorthand/false.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// rustfmt-use_field_init_shorthand: false
|
||||||
|
// Use field initialization shorthand if possible.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
z: z,
|
||||||
|
};
|
||||||
|
|
||||||
|
let b = Bar {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
#[attr]
|
||||||
|
z: z,
|
||||||
|
#[rustfmt_skip]
|
||||||
|
skipped: skipped,
|
||||||
|
};
|
||||||
|
}
|
19
tests/source/configs/use_field_init_shorthand/true.rs
Normal file
19
tests/source/configs/use_field_init_shorthand/true.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// rustfmt-use_field_init_shorthand: true
|
||||||
|
// Use field initialization shorthand if possible.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
z: z,
|
||||||
|
};
|
||||||
|
|
||||||
|
let b = Bar {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
#[attr]
|
||||||
|
z: z,
|
||||||
|
#[rustfmt_skip]
|
||||||
|
skipped: skipped,
|
||||||
|
};
|
||||||
|
}
|
15
tests/target/configs/use_field_init_shorthand/false.rs
Normal file
15
tests/target/configs/use_field_init_shorthand/false.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// rustfmt-use_field_init_shorthand: false
|
||||||
|
// Use field initialization shorthand if possible.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo { x: x, y: y, z: z };
|
||||||
|
|
||||||
|
let b = Bar {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
#[attr]
|
||||||
|
z: z,
|
||||||
|
#[rustfmt_skip]
|
||||||
|
skipped: skipped,
|
||||||
|
};
|
||||||
|
}
|
15
tests/target/configs/use_field_init_shorthand/true.rs
Normal file
15
tests/target/configs/use_field_init_shorthand/true.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// rustfmt-use_field_init_shorthand: true
|
||||||
|
// Use field initialization shorthand if possible.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo { x, y, z };
|
||||||
|
|
||||||
|
let b = Bar {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
#[attr]
|
||||||
|
z,
|
||||||
|
#[rustfmt_skip]
|
||||||
|
skipped: skipped,
|
||||||
|
};
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ where
|
||||||
pub fn new(value: V) -> Self {
|
pub fn new(value: V) -> Self {
|
||||||
Test {
|
Test {
|
||||||
cloned_value: value.clone(),
|
cloned_value: value.clone(),
|
||||||
value: value,
|
value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue