diff --git a/src/attr.rs b/src/attr.rs index 802dfc0485c..f05810b428d 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -262,7 +262,7 @@ impl Rewrite for ast::NestedMetaItem { fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) { // Look at before and after comment and see if there are any empty lines. let comment_begin = comment.find('/'); - let len = comment_begin.unwrap_or_else(|| comment.len()); + let len = comment_begin.unwrap_or(comment.len()); let mlb = count_newlines(&comment[..len]) > 1; let mla = if comment_begin.is_none() { mlb diff --git a/src/bin/main.rs b/src/bin/main.rs index 14299434bc7..acaf2b5fc58 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -462,16 +462,15 @@ fn print_version() { fn determine_operation(matches: &Matches) -> Result { if matches.opt_present("h") { - let topic = matches.opt_str("h"); - if topic.is_none() { + let Some(topic) = matches.opt_str("h") else { return Ok(Operation::Help(HelpOp::None)); - } else if topic == Some("config".to_owned()) { - return Ok(Operation::Help(HelpOp::Config)); - } else if topic == Some("file-lines".to_owned()) && is_nightly() { - return Ok(Operation::Help(HelpOp::FileLines)); - } else { - return Err(OperationError::UnknownHelpTopic(topic.unwrap())); - } + }; + + return match topic.as_str() { + "config" => Ok(Operation::Help(HelpOp::Config)), + "file-lines" if is_nightly() => Ok(Operation::Help(HelpOp::FileLines)), + _ => Err(OperationError::UnknownHelpTopic(topic)), + }; } let mut free_matches = matches.free.iter(); @@ -547,9 +546,11 @@ struct GetOptsOptions { impl GetOptsOptions { pub fn from_matches(matches: &Matches) -> Result { - let mut options = GetOptsOptions::default(); - options.verbose = matches.opt_present("verbose"); - options.quiet = matches.opt_present("quiet"); + let mut options = GetOptsOptions { + verbose: matches.opt_present("verbose"), + quiet: matches.opt_present("quiet"), + ..Default::default() + }; if options.verbose && options.quiet { return Err(format_err!("Can't use both `--verbose` and `--quiet`")); } @@ -750,8 +751,7 @@ impl CliOptions for GetOptsOptions { fn version(&self) -> Option { self.inline_config .get("version") - .map(|version| Version::from_str(version).ok()) - .flatten() + .and_then(|version| Version::from_str(version).ok()) } } @@ -799,8 +799,10 @@ mod test { #[nightly_only_test] #[test] fn flag_sets_style_edition_override_correctly() { - let mut options = GetOptsOptions::default(); - options.style_edition = Some(StyleEdition::Edition2024); + let options = GetOptsOptions { + style_edition: Some(StyleEdition::Edition2024), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); } @@ -808,8 +810,10 @@ mod test { #[nightly_only_test] #[test] fn edition_sets_style_edition_override_correctly() { - let mut options = GetOptsOptions::default(); - options.edition = Some(Edition::Edition2024); + let options = GetOptsOptions { + edition: Some(Edition::Edition2024), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); } @@ -817,8 +821,10 @@ mod test { #[nightly_only_test] #[test] fn version_sets_style_edition_override_correctly() { - let mut options = GetOptsOptions::default(); - options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]); + let options = GetOptsOptions { + inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); assert_eq!(config.overflow_delimited_expr(), true); @@ -837,9 +843,11 @@ mod test { #[nightly_only_test] #[test] fn style_edition_flag_has_correct_precedence_over_edition() { - let mut options = GetOptsOptions::default(); - options.style_edition = Some(StyleEdition::Edition2021); - options.edition = Some(Edition::Edition2024); + let options = GetOptsOptions { + style_edition: Some(StyleEdition::Edition2021), + edition: Some(Edition::Edition2024), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2021); } @@ -847,9 +855,11 @@ mod test { #[nightly_only_test] #[test] fn style_edition_flag_has_correct_precedence_over_version() { - let mut options = GetOptsOptions::default(); - options.style_edition = Some(StyleEdition::Edition2018); - options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]); + let options = GetOptsOptions { + style_edition: Some(StyleEdition::Edition2018), + inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2018); } @@ -857,10 +867,12 @@ mod test { #[nightly_only_test] #[test] fn style_edition_flag_has_correct_precedence_over_edition_version() { - let mut options = GetOptsOptions::default(); - options.style_edition = Some(StyleEdition::Edition2021); - options.edition = Some(Edition::Edition2018); - options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]); + let options = GetOptsOptions { + style_edition: Some(StyleEdition::Edition2021), + edition: Some(Edition::Edition2018), + inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2021); } @@ -868,12 +880,14 @@ mod test { #[nightly_only_test] #[test] fn style_edition_inline_has_correct_precedence_over_edition_version() { - let mut options = GetOptsOptions::default(); - options.edition = Some(Edition::Edition2018); - options.inline_config = HashMap::from([ - ("version".to_owned(), "One".to_owned()), - ("style_edition".to_owned(), "2024".to_owned()), - ]); + let options = GetOptsOptions { + edition: Some(Edition::Edition2018), + inline_config: HashMap::from([ + ("version".to_owned(), "One".to_owned()), + ("style_edition".to_owned(), "2024".to_owned()), + ]), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); assert_eq!(config.overflow_delimited_expr(), true); @@ -882,10 +896,12 @@ mod test { #[nightly_only_test] #[test] fn style_edition_config_file_trumps_edition_flag_version_inline() { - let mut options = GetOptsOptions::default(); + let options = GetOptsOptions { + edition: Some(Edition::Edition2018), + inline_config: HashMap::from([("version".to_owned(), "One".to_owned())]), + ..Default::default() + }; let config_file = Some(Path::new("tests/config/style-edition/just-style-edition")); - options.edition = Some(Edition::Edition2018); - options.inline_config = HashMap::from([("version".to_owned(), "One".to_owned())]); let config = get_config(config_file, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); } @@ -893,11 +909,13 @@ mod test { #[nightly_only_test] #[test] fn style_edition_config_file_trumps_edition_config_and_version_inline() { - let mut options = GetOptsOptions::default(); + let options = GetOptsOptions { + inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]), + ..Default::default() + }; let config_file = Some(Path::new( "tests/config/style-edition/style-edition-and-edition", )); - options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]); let config = get_config(config_file, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2021); assert_eq!(config.edition(), Edition::Edition2024); @@ -906,9 +924,11 @@ mod test { #[nightly_only_test] #[test] fn version_config_trumps_edition_config_and_flag() { - let mut options = GetOptsOptions::default(); + let options = GetOptsOptions { + edition: Some(Edition::Edition2018), + ..Default::default() + }; let config_file = Some(Path::new("tests/config/style-edition/version-edition")); - options.edition = Some(Edition::Edition2018); let config = get_config(config_file, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); } @@ -938,8 +958,10 @@ mod test { #[nightly_only_test] #[test] fn correct_defaults_for_style_edition_loaded() { - let mut options = GetOptsOptions::default(); - options.style_edition = Some(StyleEdition::Edition2024); + let options = GetOptsOptions { + style_edition: Some(StyleEdition::Edition2024), + ..Default::default() + }; let config = get_config(None, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); assert_eq!(config.overflow_delimited_expr(), true); @@ -958,10 +980,14 @@ mod test { #[nightly_only_test] #[test] fn style_edition_defaults_overridden_from_cli() { - let mut options = GetOptsOptions::default(); + let options = GetOptsOptions { + inline_config: HashMap::from([( + "overflow_delimited_expr".to_owned(), + "false".to_owned(), + )]), + ..Default::default() + }; let config_file = Some(Path::new("tests/config/style-edition/just-style-edition")); - options.inline_config = - HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]); let config = get_config(config_file, Some(options)); assert_eq!(config.style_edition(), StyleEdition::Edition2024); assert_eq!(config.overflow_delimited_expr(), false); diff --git a/src/chains.rs b/src/chains.rs index a3f70055425..0712d13d827 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -209,7 +209,7 @@ impl ChainItemKind { fn is_tup_field_access(expr: &ast::Expr) -> bool { match expr.kind { ast::ExprKind::Field(_, ref field) => { - field.name.to_string().chars().all(|c| c.is_digit(10)) + field.name.to_string().chars().all(|c| c.is_ascii_digit()) } _ => false, } @@ -286,7 +286,7 @@ impl Rewrite for ChainItem { ChainItemKind::Parent { ref expr, parens: true, - } => crate::expr::rewrite_paren(context, &expr, shape, expr.span)?, + } => crate::expr::rewrite_paren(context, expr, shape, expr.span)?, ChainItemKind::Parent { ref expr, parens: false, @@ -351,7 +351,7 @@ impl ChainItem { format!("::<{}>", type_list.join(", ")) }; let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str); - rewrite_call(context, &callee_str, &args, span, shape) + rewrite_call(context, &callee_str, args, span, shape) } } diff --git a/src/comment.rs b/src/comment.rs index 5bf3c1a725c..7af6a463dde 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -533,10 +533,11 @@ impl ItemizedBlock { /// Returns the block as a string, with each line trimmed at the start. fn trimmed_block_as_string(&self) -> String { - self.lines - .iter() - .map(|line| format!("{} ", line.trim_start())) - .collect::() + self.lines.iter().fold(String::new(), |mut acc, line| { + acc.push_str(line.trim_start()); + acc.push(' '); + acc + }) } /// Returns the block as a string under its original form. diff --git a/src/config/mod.rs b/src/config/mod.rs index 8a95cc55fcb..5ac08c2d724 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -288,7 +288,7 @@ impl Config { style_edition: Option, version: Option, ) -> Result { - let mut file = File::open(&file_path)?; + let mut file = File::open(file_path)?; let mut toml = String::new(); file.read_to_string(&mut toml)?; Config::from_toml_for_style_edition( @@ -900,7 +900,7 @@ make_backup = false Config::default_with_style_edition(style_edition) .all_options() .to_toml() - .unwrap(); + .unwrap() }; let edition2015 = get_edition_toml(StyleEdition::Edition2015); let edition2018 = get_edition_toml(StyleEdition::Edition2018); @@ -1048,10 +1048,10 @@ make_backup = false max_width = 100 "#; let config = Config::from_toml(toml, Path::new("")).unwrap(); - assert_eq!(config.array_width(), usize::max_value()); - assert_eq!(config.attr_fn_like_width(), usize::max_value()); - assert_eq!(config.chain_width(), usize::max_value()); - assert_eq!(config.fn_call_width(), usize::max_value()); + assert_eq!(config.array_width(), usize::MAX); + assert_eq!(config.attr_fn_like_width(), usize::MAX); + assert_eq!(config.chain_width(), usize::MAX); + assert_eq!(config.fn_call_width(), usize::MAX); assert_eq!(config.single_line_if_else_max_width(), 0); assert_eq!(config.struct_lit_width(), 0); assert_eq!(config.struct_variant_width(), 0); diff --git a/src/config/options.rs b/src/config/options.rs index b9e79b9a7de..f63c887f052 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -155,8 +155,10 @@ pub enum ReportTactic { /// What Rustfmt should emit. Mostly corresponds to the `--emit` command line /// option. +#[derive(Default)] #[config_type] pub enum EmitMode { + #[default] /// Emits to files. Files, /// Writes the output to stdout. @@ -255,12 +257,12 @@ impl WidthHeuristics { // Using this WidthHeuristics means we ignore heuristics. pub fn null() -> WidthHeuristics { WidthHeuristics { - fn_call_width: usize::max_value(), - attr_fn_like_width: usize::max_value(), + fn_call_width: usize::MAX, + attr_fn_like_width: usize::MAX, struct_lit_width: 0, struct_variant_width: 0, - array_width: usize::max_value(), - chain_width: usize::max_value(), + array_width: usize::MAX, + chain_width: usize::MAX, single_line_if_else_max_width: 0, single_line_let_else_max_width: 0, } @@ -310,12 +312,6 @@ impl ::std::str::FromStr for WidthHeuristics { } } -impl Default for EmitMode { - fn default() -> EmitMode { - EmitMode::Files - } -} - /// A set of directories, files and modules that rustfmt should ignore. #[derive(Default, Clone, Debug, PartialEq)] pub struct IgnoreList { @@ -425,8 +421,10 @@ pub trait CliOptions { } /// The edition of the syntax and semantics of code (RFC 2052). +#[derive(Default)] #[config_type] pub enum Edition { + #[default] #[value = "2015"] #[doc_hint = "2015"] /// Edition 2015. @@ -445,12 +443,6 @@ pub enum Edition { Edition2024, } -impl Default for Edition { - fn default() -> Edition { - Edition::Edition2015 - } -} - impl From for rustc_span::edition::Edition { fn from(edition: Edition) -> Self { match edition { diff --git a/src/emitter/checkstyle.rs b/src/emitter/checkstyle.rs index 9385ae59a06..0de027df364 100644 --- a/src/emitter/checkstyle.rs +++ b/src/emitter/checkstyle.rs @@ -95,7 +95,7 @@ mod tests { let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"]; let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"]; let mut writer = Vec::new(); - let mut emitter = CheckstyleEmitter::default(); + let mut emitter = CheckstyleEmitter; let _ = emitter.emit_header(&mut writer); let _ = emitter .emit_formatted_file( diff --git a/src/emitter/json.rs b/src/emitter/json.rs index 084f565804c..beb951b580e 100644 --- a/src/emitter/json.rs +++ b/src/emitter/json.rs @@ -136,7 +136,7 @@ mod tests { ], }; - let _ = emitter + emitter .add_misformatted_file(&FileName::Real(PathBuf::from(file)), vec![mismatch]) .unwrap(); @@ -181,7 +181,7 @@ mod tests { ], }; - let _ = emitter + emitter .add_misformatted_file(&FileName::Real(PathBuf::from(file)), vec![mismatch]) .unwrap(); diff --git a/src/expr.rs b/src/expr.rs index 02372e7be13..8158b8f55d1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -61,7 +61,7 @@ pub(crate) fn format_expr( ) -> Option { skip_out_of_file_lines_range!(context, expr.span); - if contains_skip(&*expr.attrs) { + if contains_skip(&expr.attrs) { return Some(context.snippet(expr.span()).to_owned()); } let shape = if expr_type == ExprType::Statement && semicolon_for_expr(context, expr) { @@ -278,7 +278,7 @@ pub(crate) fn format_expr( ) .ok(), ast::ExprKind::Index(ref expr, ref index, _) => { - rewrite_index(&**expr, &**index, context, shape) + rewrite_index(&*expr, &*index, context, shape) } ast::ExprKind::Repeat(ref expr, ref repeats) => rewrite_pair( &**expr, @@ -295,13 +295,11 @@ pub(crate) fn format_expr( ast::RangeLimits::Closed => "..=", }; - fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool { + fn needs_space_before_range(lhs: &ast::Expr) -> bool { match lhs.kind { ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit), - ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr), - ast::ExprKind::Binary(_, _, ref rhs_expr) => { - needs_space_before_range(context, rhs_expr) - } + ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(expr), + ast::ExprKind::Binary(_, _, ref rhs_expr) => needs_space_before_range(rhs_expr), _ => false, } } @@ -320,7 +318,7 @@ pub(crate) fn format_expr( format!( "{}{}{}", - lhs.map_or("", |lhs| space_if(needs_space_before_range(context, lhs))), + lhs.map_or("", |lhs| space_if(needs_space_before_range(lhs))), delim, rhs.map_or("", |rhs| space_if(needs_space_after_range(rhs))), ) @@ -916,7 +914,7 @@ impl<'a> ControlFlow<'a> { let comments_span = mk_sp(comments_lo, expr.span.lo()); return rewrite_assign_rhs_with_comments( context, - &format!("{}{}{}", matcher, pat_string, self.connector), + format!("{}{}{}", matcher, pat_string, self.connector), expr, cond_shape, &RhsAssignKind::Expr(&expr.kind, expr.span), @@ -1392,7 +1390,7 @@ pub(crate) fn is_simple_expr(expr: &ast::Expr) -> bool { | ast::ExprKind::Unary(_, ref expr) => is_simple_expr(expr), ast::ExprKind::Index(ref lhs, ref rhs, _) => is_simple_expr(lhs) && is_simple_expr(rhs), ast::ExprKind::Repeat(ref lhs, ref rhs) => { - is_simple_expr(lhs) && is_simple_expr(&*rhs.value) + is_simple_expr(lhs) && is_simple_expr(&rhs.value) } _ => false, } @@ -1672,7 +1670,7 @@ fn rewrite_struct_lit<'a>( } else { let field_iter = fields.iter().map(StructLitField::Regular).chain( match struct_rest { - ast::StructRest::Base(expr) => Some(StructLitField::Base(&**expr)), + ast::StructRest::Base(expr) => Some(StructLitField::Base(&*expr)), ast::StructRest::Rest(span) => Some(StructLitField::Rest(*span)), ast::StructRest::None => None, } @@ -2200,7 +2198,7 @@ fn choose_rhs( match (orig_rhs, new_rhs) { (Some(ref orig_rhs), Some(ref new_rhs)) - if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) => + if !filtered_str_fits(new_rhs, context.config.max_width(), new_shape) => { Some(format!("{before_space_str}{orig_rhs}")) } diff --git a/src/items.rs b/src/items.rs index 3894ee2cdf8..6822a00c23b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -322,7 +322,7 @@ impl<'a> FnSig<'a> { constness: method_sig.header.constness, defaultness: ast::Defaultness::Final, ext: method_sig.header.ext, - decl: &*method_sig.decl, + decl: &method_sig.decl, generics, visibility, } @@ -356,7 +356,7 @@ impl<'a> FnSig<'a> { fn to_str(&self, context: &RewriteContext<'_>) -> String { let mut result = String::with_capacity(128); // Vis defaultness constness unsafety abi. - result.push_str(&*format_visibility(context, self.visibility)); + result.push_str(&format_visibility(context, self.visibility)); result.push_str(format_defaultness(self.defaultness)); result.push_str(format_constness(self.constness)); self.coroutine_kind @@ -1047,7 +1047,7 @@ fn format_impl_ref_and_type( IndentStyle::Visual => new_line_offset + trait_ref_overhead, IndentStyle::Block => new_line_offset, }; - result.push_str(&*self_ty.rewrite(context, Shape::legacy(budget, type_offset))?); + result.push_str(&self_ty.rewrite(context, Shape::legacy(budget, type_offset))?); Some(result) } @@ -2224,7 +2224,7 @@ impl Rewrite for ast::Param { !has_multiple_attr_lines && !has_doc_comments, )?; - if !is_empty_infer(&*self.ty, self.pat.span) { + if !is_empty_infer(&self.ty, self.pat.span) { let (before_comment, after_comment) = get_missing_param_comments(context, self.pat.span, self.ty.span, shape); result.push_str(&before_comment); @@ -2412,7 +2412,7 @@ fn rewrite_fn_base( let generics_str = rewrite_generics( context, rewrite_ident(context, ident), - &fn_sig.generics, + fn_sig.generics, shape, )?; result.push_str(&generics_str); @@ -3491,7 +3491,7 @@ pub(crate) fn rewrite_mod( attrs_shape: Shape, ) -> Option { let mut result = String::with_capacity(32); - result.push_str(&*format_visibility(context, &item.vis)); + result.push_str(&format_visibility(context, &item.vis)); result.push_str("mod "); result.push_str(rewrite_ident(context, item.ident)); result.push(';'); diff --git a/src/lib.rs b/src/lib.rs index 7dc698dd92c..fce848b81a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -389,7 +389,7 @@ fn format_code_block( let block_len = formatted .snippet .rfind('}') - .unwrap_or_else(|| formatted.snippet.len()); + .unwrap_or(formatted.snippet.len()); // It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was // formatted into the empty string, leading to the enclosing `fn main() {\n}` being formatted @@ -520,7 +520,7 @@ impl<'b, T: Write + 'b> Session<'b, T> { pub(crate) fn create_emitter<'a>(config: &Config) -> Box { match config.emit_mode() { EmitMode::Files if config.make_backup() => { - Box::new(emitter::FilesWithBackupEmitter::default()) + Box::new(emitter::FilesWithBackupEmitter) } EmitMode::Files => Box::new(emitter::FilesEmitter::new( config.print_misformatted_file_names(), @@ -529,8 +529,8 @@ pub(crate) fn create_emitter<'a>(config: &Config) -> Box { Box::new(emitter::StdoutEmitter::new(config.verbose())) } EmitMode::Json => Box::new(emitter::JsonEmitter::default()), - EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter::default()), - EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter::default()), + EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter), + EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter), EmitMode::Diff => Box::new(emitter::DiffEmitter::new(config.clone())), } } diff --git a/src/lists.rs b/src/lists.rs index f9e722130cd..d41be141fae 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -669,7 +669,7 @@ pub(crate) fn get_comment_end( if is_last { return post_snippet .find_uncommented(terminator) - .unwrap_or_else(|| post_snippet.len()); + .unwrap_or(post_snippet.len()); } let mut block_open_index = post_snippet.find("/*"); @@ -725,14 +725,12 @@ pub(crate) fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool .len_utf8(); // Everything from the separator to the next item. let test_snippet = &post_snippet[comment_end - len_last..]; - let first_newline = test_snippet - .find('\n') - .unwrap_or_else(|| test_snippet.len()); + let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); // From the end of the first line of comments. let test_snippet = &test_snippet[first_newline..]; let first = test_snippet .find(|c: char| !c.is_whitespace()) - .unwrap_or_else(|| test_snippet.len()); + .unwrap_or(test_snippet.len()); // From the end of the first line of comments to the next non-whitespace char. let test_snippet = &test_snippet[..first]; diff --git a/src/macros.rs b/src/macros.rs index 51ded869229..b5b80b92798 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -832,7 +832,7 @@ impl MacroArgParser { // Parse '*', '+' or '?. for tok in iter { - self.set_last_tok(&tok); + self.set_last_tok(tok); if first { first = false; } @@ -980,7 +980,7 @@ impl MacroArgParser { } } - self.set_last_tok(&tok); + self.set_last_tok(tok); } // We are left with some stuff in the buffer. Since there is nothing @@ -1160,9 +1160,9 @@ pub(crate) fn convert_try_mac( pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> Delimiter { let snippet = context.snippet(mac.span()); - let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value()); - let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value()); - let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value()); + let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::MAX); + let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::MAX); + let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::MAX); if paren_pos < bracket_pos && paren_pos < brace_pos { Delimiter::Parenthesis diff --git a/src/overflow.rs b/src/overflow.rs index 510ad3c642e..a1e981c3034 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -581,7 +581,7 @@ impl<'a> Context<'a> { if tactic == DefinitiveListTactic::Vertical { if let Some((all_simple, num_args_before)) = - maybe_get_args_offset(self.ident, &self.items, &self.context.config) + maybe_get_args_offset(self.ident, &self.items, self.context.config) { let one_line = all_simple && definitive_tactic( diff --git a/src/reorder.rs b/src/reorder.rs index 80d1c738994..d98c1462386 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -28,7 +28,7 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering { (&ast::ItemKind::Mod(..), &ast::ItemKind::Mod(..)) => { a.ident.as_str().cmp(b.ident.as_str()) } - (&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => { + (&ast::ItemKind::ExternCrate(a_name), ast::ItemKind::ExternCrate(ref b_name)) => { // `extern crate foo as bar;` // ^^^ Comparing this. let a_orig_name = a_name.unwrap_or(a.ident.name); @@ -274,7 +274,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let item_length = items .iter() .take_while(|ppi| { - item_kind.is_same_item_kind(&***ppi) + item_kind.is_same_item_kind(ppi) && (!in_group || { let current = self.psess.lookup_line_range(ppi.span()); let in_same_group = current.lo < last.hi + 2; diff --git a/src/rewrite.rs b/src/rewrite.rs index 8cefda1657f..97f4c208169 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -72,21 +72,21 @@ pub(crate) trait RewriteErrorExt { impl RewriteErrorExt for Option { fn max_width_error(self, width: usize, span: Span) -> Result { - self.ok_or_else(|| RewriteError::ExceedsMaxWidth { + self.ok_or(RewriteError::ExceedsMaxWidth { configured_width: width, span: span, }) } fn macro_error(self, kind: MacroErrorKind, span: Span) -> Result { - self.ok_or_else(|| RewriteError::MacroFailure { + self.ok_or(RewriteError::MacroFailure { kind: kind, span: span, }) } fn unknown_error(self) -> Result { - self.ok_or_else(|| RewriteError::Unknown) + self.ok_or(RewriteError::Unknown) } } diff --git a/src/string.rs b/src/string.rs index cb666fff695..38b09788147 100644 --- a/src/string.rs +++ b/src/string.rs @@ -84,7 +84,7 @@ pub(crate) fn rewrite_string<'a>( stripped_str .len() .checked_next_power_of_two() - .unwrap_or(usize::max_value()), + .unwrap_or(usize::MAX), ); result.push_str(fmt.opener); diff --git a/src/test/mod.rs b/src/test/mod.rs index 4d22f64f352..9e65d370b03 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -295,7 +295,7 @@ fn assert_output(source: &Path, expected_filename: &Path) { let _ = source_file::write_all_files(&source_file, &mut out, &config); let output = String::from_utf8(out).unwrap(); - let mut expected_file = fs::File::open(&expected_filename).expect("couldn't open target"); + let mut expected_file = fs::File::open(expected_filename).expect("couldn't open target"); let mut expected_text = String::new(); expected_file .read_to_string(&mut expected_text) @@ -322,7 +322,7 @@ fn assert_stdin_output( config.set().newline_style(NewlineStyle::Unix); config.set().emit_mode(emit_mode); - let mut source_file = fs::File::open(&source).expect("couldn't open source"); + let mut source_file = fs::File::open(source).expect("couldn't open source"); let mut source_text = String::new(); source_file .read_to_string(&mut source_text) @@ -341,7 +341,7 @@ fn assert_stdin_output( assert_eq!(session.errors, errors); } - let mut expected_file = fs::File::open(&expected_filename).expect("couldn't open target"); + let mut expected_file = fs::File::open(expected_filename).expect("couldn't open target"); let mut expected_text = String::new(); expected_file .read_to_string(&mut expected_text) @@ -690,7 +690,7 @@ fn print_mismatches_default_message(result: HashMap>) { for (file_name, diff) in result { let mismatch_msg_formatter = |line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num); - print_diff(diff, &mismatch_msg_formatter, &Default::default()); + print_diff(diff, mismatch_msg_formatter, &Default::default()); } if let Some(mut t) = term::stdout() { diff --git a/src/types.rs b/src/types.rs index 76eb0ea0529..59e8ca078f0 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1244,7 +1244,7 @@ pub(crate) fn can_be_overflowed_type( match ty.kind { ast::TyKind::Tup(..) => context.use_block_indent() && len == 1, ast::TyKind::Ref(_, ref mutty) | ast::TyKind::Ptr(ref mutty) => { - can_be_overflowed_type(context, &*mutty.ty, len) + can_be_overflowed_type(context, &mutty.ty, len) } _ => false, } diff --git a/src/visitor.rs b/src/visitor.rs index 9859100a038..86d1371c19f 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -922,7 +922,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { attrs: &[ast::Attribute], ) { let vis_str = utils::format_visibility(&self.get_context(), vis); - self.push_str(&*vis_str); + self.push_str(&vis_str); self.push_str(format_safety(safety)); self.push_str("mod "); // Calling `to_owned()` to work around borrow checker. diff --git a/tests/source/cfg_if/detect/cache.rs b/tests/source/cfg_if/detect/cache.rs index 92bc4b58d16..07bf185a62d 100644 --- a/tests/source/cfg_if/detect/cache.rs +++ b/tests/source/cfg_if/detect/cache.rs @@ -81,12 +81,12 @@ impl Cache { /// Creates an uninitialized cache. #[allow(clippy::declare_interior_mutable_const)] const fn uninitialized() -> Self { - Cache(AtomicU64::new(u64::max_value())) + Cache(AtomicU64::new(u64::MAX)) } /// Is the cache uninitialized? #[inline] pub(crate) fn is_uninitialized(&self) -> bool { - self.0.load(Ordering::Relaxed) == u64::max_value() + self.0.load(Ordering::Relaxed) == u64::MAX } /// Is the `bit` in the cache set? @@ -113,15 +113,12 @@ struct Cache(AtomicU32, AtomicU32); impl Cache { /// Creates an uninitialized cache. const fn uninitialized() -> Self { - Cache( - AtomicU32::new(u32::max_value()), - AtomicU32::new(u32::max_value()), - ) + Cache(AtomicU32::new(u32::MAX), AtomicU32::new(u32::MAX)) } /// Is the cache uninitialized? #[inline] pub(crate) fn is_uninitialized(&self) -> bool { - self.1.load(Ordering::Relaxed) == u32::max_value() + self.1.load(Ordering::Relaxed) == u32::MAX } /// Is the `bit` in the cache set? diff --git a/tests/target/cfg_if/detect/cache.rs b/tests/target/cfg_if/detect/cache.rs index 92bc4b58d16..07bf185a62d 100644 --- a/tests/target/cfg_if/detect/cache.rs +++ b/tests/target/cfg_if/detect/cache.rs @@ -81,12 +81,12 @@ impl Cache { /// Creates an uninitialized cache. #[allow(clippy::declare_interior_mutable_const)] const fn uninitialized() -> Self { - Cache(AtomicU64::new(u64::max_value())) + Cache(AtomicU64::new(u64::MAX)) } /// Is the cache uninitialized? #[inline] pub(crate) fn is_uninitialized(&self) -> bool { - self.0.load(Ordering::Relaxed) == u64::max_value() + self.0.load(Ordering::Relaxed) == u64::MAX } /// Is the `bit` in the cache set? @@ -113,15 +113,12 @@ struct Cache(AtomicU32, AtomicU32); impl Cache { /// Creates an uninitialized cache. const fn uninitialized() -> Self { - Cache( - AtomicU32::new(u32::max_value()), - AtomicU32::new(u32::max_value()), - ) + Cache(AtomicU32::new(u32::MAX), AtomicU32::new(u32::MAX)) } /// Is the cache uninitialized? #[inline] pub(crate) fn is_uninitialized(&self) -> bool { - self.1.load(Ordering::Relaxed) == u32::max_value() + self.1.load(Ordering::Relaxed) == u32::MAX } /// Is the `bit` in the cache set?