Skip to content

Commit ce682bd

Browse files
authored
Merge pull request #3335 from h-michael/rust-2018
Rust 2018 idioms
2 parents fca91bc + 8183b94 commit ce682bd

34 files changed

+353
-329
lines changed

src/attr.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn argument_shape(
7575
right: usize,
7676
combine: bool,
7777
shape: Shape,
78-
context: &RewriteContext,
78+
context: &RewriteContext<'_>,
7979
) -> Option<Shape> {
8080
match context.config.indent_style() {
8181
IndentStyle::Block => {
@@ -100,7 +100,7 @@ fn format_derive(
100100
derive_args: &[Span],
101101
prefix: &str,
102102
shape: Shape,
103-
context: &RewriteContext,
103+
context: &RewriteContext<'_>,
104104
) -> Option<String> {
105105
let mut result = String::with_capacity(128);
106106
result.push_str(prefix);
@@ -133,7 +133,7 @@ fn format_derive(
133133
/// Returns the first group of attributes that fills the given predicate.
134134
/// We consider two doc comments are in different group if they are separated by normal comments.
135135
fn take_while_with_pred<'a, P>(
136-
context: &RewriteContext,
136+
context: &RewriteContext<'_>,
137137
attrs: &'a [ast::Attribute],
138138
pred: P,
139139
) -> &'a [ast::Attribute]
@@ -164,7 +164,7 @@ where
164164

165165
/// Rewrite the any doc comments which come before any other attributes.
166166
fn rewrite_initial_doc_comments(
167-
context: &RewriteContext,
167+
context: &RewriteContext<'_>,
168168
attrs: &[ast::Attribute],
169169
shape: Shape,
170170
) -> Option<(usize, Option<String>)> {
@@ -193,7 +193,7 @@ fn rewrite_initial_doc_comments(
193193
}
194194

195195
impl Rewrite for ast::NestedMetaItem {
196-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
196+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
197197
match self.node {
198198
ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
199199
ast::NestedMetaItemKind::Literal(ref l) => rewrite_literal(context, l, shape),
@@ -221,7 +221,7 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
221221
}
222222

223223
impl Rewrite for ast::MetaItem {
224-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
224+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
225225
Some(match self.node {
226226
ast::MetaItemKind::Word => {
227227
rewrite_path(context, PathContext::Type, None, &self.ident, shape)?
@@ -268,7 +268,7 @@ fn format_arg_list<I, T, F1, F2, F3>(
268268
get_hi: F2,
269269
get_item_string: F3,
270270
span: Span,
271-
context: &RewriteContext,
271+
context: &RewriteContext<'_>,
272272
shape: Shape,
273273
one_line_shape: Shape,
274274
one_line_limit: Option<usize>,
@@ -318,7 +318,7 @@ where
318318
}
319319

320320
impl Rewrite for ast::Attribute {
321-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
321+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
322322
let snippet = context.snippet(self.span);
323323
if self.is_sugared_doc {
324324
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
@@ -365,7 +365,7 @@ impl Rewrite for ast::Attribute {
365365
}
366366

367367
impl<'a> Rewrite for [ast::Attribute] {
368-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
368+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
369369
if self.is_empty() {
370370
return Some(String::new());
371371
}

src/bin/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
extern crate env_logger;
11+
use env_logger;
1212
#[macro_use]
1313
extern crate failure;
14-
extern crate getopts;
15-
extern crate rustfmt_nightly as rustfmt;
14+
15+
use rustfmt_nightly as rustfmt;
1616

1717
use std::env;
1818
use std::fs::File;
@@ -296,7 +296,7 @@ fn format(
296296
Ok(exit_code)
297297
}
298298

299-
fn format_and_emit_report<T: Write>(session: &mut Session<T>, input: Input) {
299+
fn format_and_emit_report<T: Write>(session: &mut Session<'_, T>, input: Input) {
300300
match session.format(input) {
301301
Ok(report) => {
302302
if report.has_warnings() {

src/cargo-fmt/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#![cfg(not(test))]
1414
#![deny(warnings)]
1515

16-
extern crate cargo_metadata;
17-
extern crate getopts;
18-
extern crate serde_json as json;
16+
use cargo_metadata;
17+
use getopts;
1918

2019
use std::collections::{HashMap, HashSet};
2120
use std::env;

src/chains.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ use crate::utils::{
8484
trimmed_last_line_width, wrap_str,
8585
};
8686

87-
pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -> Option<String> {
87+
pub fn rewrite_chain(
88+
expr: &ast::Expr,
89+
context: &RewriteContext<'_>,
90+
shape: Shape,
91+
) -> Option<String> {
8892
let chain = Chain::from_ast(expr, context);
8993
debug!("rewrite_chain {:?} {:?}", chain, shape);
9094

@@ -128,7 +132,7 @@ enum ChainItemKind {
128132
}
129133

130134
impl ChainItemKind {
131-
fn is_block_like(&self, context: &RewriteContext, reps: &str) -> bool {
135+
fn is_block_like(&self, context: &RewriteContext<'_>, reps: &str) -> bool {
132136
match self {
133137
ChainItemKind::Parent(ref expr) => utils::is_block_expr(context, expr, reps),
134138
ChainItemKind::MethodCall(..)
@@ -147,7 +151,7 @@ impl ChainItemKind {
147151
}
148152
}
149153

150-
fn from_ast(context: &RewriteContext, expr: &ast::Expr) -> (ChainItemKind, Span) {
154+
fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, Span) {
151155
let (kind, span) = match expr.node {
152156
ast::ExprKind::MethodCall(ref segment, ref expressions) => {
153157
let types = if let Some(ref generic_args) = segment.args {
@@ -182,7 +186,7 @@ impl ChainItemKind {
182186
}
183187

184188
impl Rewrite for ChainItem {
185-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
189+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
186190
let shape = shape.sub_width(self.tries)?;
187191
let rewrite = match self.kind {
188192
ChainItemKind::Parent(ref expr) => expr.rewrite(context, shape)?,
@@ -204,7 +208,7 @@ impl Rewrite for ChainItem {
204208
}
205209

206210
impl ChainItem {
207-
fn new(context: &RewriteContext, expr: &ast::Expr, tries: usize) -> ChainItem {
211+
fn new(context: &RewriteContext<'_>, expr: &ast::Expr, tries: usize) -> ChainItem {
208212
let (kind, span) = ChainItemKind::from_ast(context, expr);
209213
ChainItem { kind, tries, span }
210214
}
@@ -229,7 +233,7 @@ impl ChainItem {
229233
types: &[ast::GenericArg],
230234
args: &[ptr::P<ast::Expr>],
231235
span: Span,
232-
context: &RewriteContext,
236+
context: &RewriteContext<'_>,
233237
shape: Shape,
234238
) -> Option<String> {
235239
let type_str = if types.is_empty() {
@@ -254,7 +258,7 @@ struct Chain {
254258
}
255259

256260
impl Chain {
257-
fn from_ast(expr: &ast::Expr, context: &RewriteContext) -> Chain {
261+
fn from_ast(expr: &ast::Expr, context: &RewriteContext<'_>) -> Chain {
258262
let subexpr_list = Self::make_subexpr_list(expr, context);
259263

260264
// Un-parse the expression tree into ChainItems
@@ -376,7 +380,7 @@ impl Chain {
376380

377381
// Returns a Vec of the prefixes of the chain.
378382
// E.g., for input `a.b.c` we return [`a.b.c`, `a.b`, 'a']
379-
fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext) -> Vec<ast::Expr> {
383+
fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext<'_>) -> Vec<ast::Expr> {
380384
let mut subexpr_list = vec![expr.clone()];
381385

382386
while let Some(subexpr) = Self::pop_expr_chain(subexpr_list.last().unwrap(), context) {
@@ -388,7 +392,7 @@ impl Chain {
388392

389393
// Returns the expression's subexpression, if it exists. When the subexpr
390394
// is a try! macro, we'll convert it to shorthand when the option is set.
391-
fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext) -> Option<ast::Expr> {
395+
fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option<ast::Expr> {
392396
match expr.node {
393397
ast::ExprKind::MethodCall(_, ref expressions) => {
394398
Some(Self::convert_try(&expressions[0], context))
@@ -400,7 +404,7 @@ impl Chain {
400404
}
401405
}
402406

403-
fn convert_try(expr: &ast::Expr, context: &RewriteContext) -> ast::Expr {
407+
fn convert_try(expr: &ast::Expr, context: &RewriteContext<'_>) -> ast::Expr {
404408
match expr.node {
405409
ast::ExprKind::Mac(ref mac) if context.config.use_try_shorthand() => {
406410
if let Some(subexpr) = convert_try_mac(mac, context) {
@@ -415,12 +419,16 @@ impl Chain {
415419
}
416420

417421
impl Rewrite for Chain {
418-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
422+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
419423
debug!("rewrite chain {:?} {:?}", self, shape);
420424

421425
let mut formatter = match context.config.indent_style() {
422-
IndentStyle::Block => Box::new(ChainFormatterBlock::new(self)) as Box<ChainFormatter>,
423-
IndentStyle::Visual => Box::new(ChainFormatterVisual::new(self)) as Box<ChainFormatter>,
426+
IndentStyle::Block => {
427+
Box::new(ChainFormatterBlock::new(self)) as Box<dyn ChainFormatter>
428+
}
429+
IndentStyle::Visual => {
430+
Box::new(ChainFormatterVisual::new(self)) as Box<dyn ChainFormatter>
431+
}
424432
};
425433

426434
formatter.format_root(&self.parent, context, shape)?;
@@ -455,18 +463,18 @@ trait ChainFormatter {
455463
fn format_root(
456464
&mut self,
457465
parent: &ChainItem,
458-
context: &RewriteContext,
466+
context: &RewriteContext<'_>,
459467
shape: Shape,
460468
) -> Option<()>;
461-
fn child_shape(&self, context: &RewriteContext, shape: Shape) -> Option<Shape>;
462-
fn format_children(&mut self, context: &RewriteContext, child_shape: Shape) -> Option<()>;
469+
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape>;
470+
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()>;
463471
fn format_last_child(
464472
&mut self,
465-
context: &RewriteContext,
473+
context: &RewriteContext<'_>,
466474
shape: Shape,
467475
child_shape: Shape,
468476
) -> Option<()>;
469-
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String>;
477+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String>;
470478
// Returns `Some` if the chain is only a root, None otherwise.
471479
fn pure_root(&mut self) -> Option<String>;
472480
}
@@ -540,7 +548,7 @@ impl<'a> ChainFormatterShared<'a> {
540548
fn format_last_child(
541549
&mut self,
542550
may_extend: bool,
543-
context: &RewriteContext,
551+
context: &RewriteContext<'_>,
544552
shape: Shape,
545553
child_shape: Shape,
546554
) -> Option<()> {
@@ -633,7 +641,7 @@ impl<'a> ChainFormatterShared<'a> {
633641
Some(())
634642
}
635643

636-
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
644+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
637645
let connector = if self.fits_single_line {
638646
// Yay, we can put everything on one line.
639647
Cow::from("")
@@ -682,7 +690,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
682690
fn format_root(
683691
&mut self,
684692
parent: &ChainItem,
685-
context: &RewriteContext,
693+
context: &RewriteContext<'_>,
686694
shape: Shape,
687695
) -> Option<()> {
688696
let mut root_rewrite: String = parent.rewrite(context, shape)?;
@@ -713,7 +721,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
713721
Some(())
714722
}
715723

716-
fn child_shape(&self, context: &RewriteContext, shape: Shape) -> Option<Shape> {
724+
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
717725
Some(
718726
if self.root_ends_with_block {
719727
shape.block_indent(0)
@@ -724,7 +732,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
724732
)
725733
}
726734

727-
fn format_children(&mut self, context: &RewriteContext, child_shape: Shape) -> Option<()> {
735+
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
728736
for item in &self.shared.children[..self.shared.children.len() - 1] {
729737
let rewrite = item.rewrite(context, child_shape)?;
730738
self.shared.rewrites.push(rewrite);
@@ -734,15 +742,15 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
734742

735743
fn format_last_child(
736744
&mut self,
737-
context: &RewriteContext,
745+
context: &RewriteContext<'_>,
738746
shape: Shape,
739747
child_shape: Shape,
740748
) -> Option<()> {
741749
self.shared
742750
.format_last_child(true, context, shape, child_shape)
743751
}
744752

745-
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
753+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
746754
self.shared.join_rewrites(context, child_shape)
747755
}
748756

@@ -771,7 +779,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
771779
fn format_root(
772780
&mut self,
773781
parent: &ChainItem,
774-
context: &RewriteContext,
782+
context: &RewriteContext<'_>,
775783
shape: Shape,
776784
) -> Option<()> {
777785
let parent_shape = shape.visual_indent(0);
@@ -811,14 +819,14 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
811819
Some(())
812820
}
813821

814-
fn child_shape(&self, context: &RewriteContext, shape: Shape) -> Option<Shape> {
822+
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
815823
shape
816824
.with_max_width(context.config)
817825
.offset_left(self.offset)
818826
.map(|s| s.visual_indent(0))
819827
}
820828

821-
fn format_children(&mut self, context: &RewriteContext, child_shape: Shape) -> Option<()> {
829+
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
822830
for item in &self.shared.children[..self.shared.children.len() - 1] {
823831
let rewrite = item.rewrite(context, child_shape)?;
824832
self.shared.rewrites.push(rewrite);
@@ -828,15 +836,15 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
828836

829837
fn format_last_child(
830838
&mut self,
831-
context: &RewriteContext,
839+
context: &RewriteContext<'_>,
832840
shape: Shape,
833841
child_shape: Shape,
834842
) -> Option<()> {
835843
self.shared
836844
.format_last_child(false, context, shape, child_shape)
837845
}
838846

839-
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
847+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
840848
self.shared.join_rewrites(context, child_shape)
841849
}
842850

0 commit comments

Comments
 (0)