Skip to content

Commit 861a4e0

Browse files
cargo clippy --fix
1 parent ee329d3 commit 861a4e0

37 files changed

+263
-300
lines changed

src/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl Rewrite for ast::MetaItemInner {
260260
fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
261261
// Look at before and after comment and see if there are any empty lines.
262262
let comment_begin = comment.find('/');
263-
let len = comment_begin.unwrap_or_else(|| comment.len());
263+
let len = comment_begin.unwrap_or(comment.len());
264264
let mlb = count_newlines(&comment[..len]) > 1;
265265
let mla = if comment_begin.is_none() {
266266
mlb

src/bin/main.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,7 @@ impl CliOptions for GetOptsOptions {
746746
fn version(&self) -> Option<Version> {
747747
self.inline_config
748748
.get("version")
749-
.map(|version| Version::from_str(version).ok())
750-
.flatten()
749+
.and_then(|version| Version::from_str(version).ok())
751750
}
752751
}
753752

@@ -818,7 +817,7 @@ mod test {
818817
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
819818
let config = get_config(None, Some(options));
820819
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
821-
assert_eq!(config.overflow_delimited_expr(), true);
820+
assert!(config.overflow_delimited_expr());
822821
}
823822

824823
#[nightly_only_test]
@@ -828,7 +827,7 @@ mod test {
828827
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
829828
let config = get_config(config_file, Some(options));
830829
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
831-
assert_eq!(config.overflow_delimited_expr(), true);
830+
assert!(config.overflow_delimited_expr());
832831
}
833832

834833
#[nightly_only_test]
@@ -873,7 +872,7 @@ mod test {
873872
]);
874873
let config = get_config(None, Some(options));
875874
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
876-
assert_eq!(config.overflow_delimited_expr(), true);
875+
assert!(config.overflow_delimited_expr());
877876
}
878877

879878
#[nightly_only_test]
@@ -939,7 +938,7 @@ mod test {
939938
options.style_edition = Some(StyleEdition::Edition2024);
940939
let config = get_config(None, Some(options));
941940
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
942-
assert_eq!(config.overflow_delimited_expr(), true);
941+
assert!(config.overflow_delimited_expr());
943942
}
944943

945944
#[nightly_only_test]
@@ -949,7 +948,7 @@ mod test {
949948
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
950949
let config = get_config(config_file, Some(options));
951950
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
952-
assert_eq!(config.overflow_delimited_expr(), false);
951+
assert!(!config.overflow_delimited_expr());
953952
}
954953

955954
#[nightly_only_test]
@@ -961,6 +960,6 @@ mod test {
961960
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
962961
let config = get_config(config_file, Some(options));
963962
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
964-
assert_eq!(config.overflow_delimited_expr(), false);
963+
assert!(!config.overflow_delimited_expr());
965964
}
966965
}

src/chains.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl ChainItemKind {
213213
fn is_tup_field_access(expr: &ast::Expr) -> bool {
214214
match expr.kind {
215215
ast::ExprKind::Field(_, ref field) => {
216-
field.name.as_str().chars().all(|c| c.is_digit(10))
216+
field.name.as_str().chars().all(|c| c.is_ascii_digit())
217217
}
218218
_ => false,
219219
}
@@ -288,7 +288,7 @@ impl Rewrite for ChainItem {
288288
ChainItemKind::Parent {
289289
ref expr,
290290
parens: true,
291-
} => crate::expr::rewrite_paren(context, &expr, shape, expr.span)?,
291+
} => crate::expr::rewrite_paren(context, expr, shape, expr.span)?,
292292
ChainItemKind::Parent {
293293
ref expr,
294294
parens: false,
@@ -353,7 +353,7 @@ impl ChainItem {
353353
format!("::<{}>", type_list.join(", "))
354354
};
355355
let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str);
356-
rewrite_call(context, &callee_str, &args, span, shape)
356+
rewrite_call(context, &callee_str, args, span, shape)
357357
}
358358
}
359359

@@ -843,7 +843,7 @@ impl<'a> ChainFormatterBlock<'a> {
843843
}
844844
}
845845

846-
impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
846+
impl ChainFormatter for ChainFormatterBlock<'_> {
847847
fn format_root(
848848
&mut self,
849849
parent: &ChainItem,
@@ -931,7 +931,7 @@ impl<'a> ChainFormatterVisual<'a> {
931931
}
932932
}
933933

934-
impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
934+
impl ChainFormatter for ChainFormatterVisual<'_> {
935935
fn format_root(
936936
&mut self,
937937
parent: &ChainItem,

src/closures.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn needs_block(
124124
prefix: &str,
125125
context: &RewriteContext<'_>,
126126
) -> bool {
127-
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
127+
let has_attributes = block.stmts.first().is_some_and(|first_stmt| {
128128
!get_attrs_from_stmt(first_stmt).is_empty()
129129
});
130130

@@ -434,7 +434,7 @@ pub(crate) fn rewrite_last_closure(
434434

435435
// When overflowing the closure which consists of a single control flow expression,
436436
// force to use block if its condition uses multi line.
437-
let is_multi_lined_cond = rewrite_cond(context, body, body_shape).map_or(false, |cond| {
437+
let is_multi_lined_cond = rewrite_cond(context, body, body_shape).is_some_and(|cond| {
438438
cond.contains('\n') || cond.len() > body_shape.width
439439
});
440440
if is_multi_lined_cond {

src/comment.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,11 @@ fn is_table_item(mut s: &str) -> bool {
990990
// This function may return false positive, but should get its job done in most cases (i.e.
991991
// markdown tables with two column delimiters).
992992
s = s.trim_start();
993-
return s.starts_with('|')
993+
s.starts_with('|')
994994
&& match s.rfind('|') {
995995
Some(0) | None => false,
996996
_ => true,
997-
};
997+
}
998998
}
999999

10001000
/// Given the span, rewrite the missing comment inside it if available.
@@ -1521,7 +1521,7 @@ impl<'a> LineClasses<'a> {
15211521
}
15221522
}
15231523

1524-
impl<'a> Iterator for LineClasses<'a> {
1524+
impl Iterator for LineClasses<'_> {
15251525
type Item = (FullCodeCharKind, String);
15261526

15271527
fn next(&mut self) -> Option<Self::Item> {
@@ -1797,7 +1797,7 @@ impl<'a> CommentReducer<'a> {
17971797
}
17981798
}
17991799

1800-
impl<'a> Iterator for CommentReducer<'a> {
1800+
impl Iterator for CommentReducer<'_> {
18011801
type Item = char;
18021802

18031803
fn next(&mut self) -> Option<Self::Item> {
@@ -2007,10 +2007,10 @@ mod test {
20072007

20082008
#[test]
20092009
fn test_contains_comment() {
2010-
assert_eq!(contains_comment("abc"), false);
2011-
assert_eq!(contains_comment("abc // qsdf"), true);
2012-
assert_eq!(contains_comment("abc /* kqsdf"), true);
2013-
assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
2010+
assert!(!contains_comment("abc"));
2011+
assert!(contains_comment("abc // qsdf"));
2012+
assert!(contains_comment("abc /* kqsdf"));
2013+
assert!(!contains_comment("abc \" /* */\" qsdf"));
20142014
}
20152015

20162016
#[test]

src/config/file_lines.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl fmt::Display for FileLines {
163163
Some(map) => {
164164
for (file_name, ranges) in map.iter() {
165165
write!(f, "{file_name}: ")?;
166-
write!(f, "{}\n", ranges.iter().format(", "))?;
166+
writeln!(f, "{}", ranges.iter().format(", "))?;
167167
}
168168
}
169169
};

src/config/mod.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl Config {
327327
style_edition: Option<StyleEdition>,
328328
version: Option<Version>,
329329
) -> Result<Config, Error> {
330-
let mut file = File::open(&file_path)?;
330+
let mut file = File::open(file_path)?;
331331
let mut toml = String::new();
332332
file.read_to_string(&mut toml)?;
333333
Config::from_toml_for_style_edition(&toml, file_path, edition, style_edition, version)
@@ -715,8 +715,8 @@ mod test {
715715
fn test_was_set() {
716716
let config = Config::from_toml("hard_tabs = true", Path::new("./rustfmt.toml")).unwrap();
717717

718-
assert_eq!(config.was_set().hard_tabs(), true);
719-
assert_eq!(config.was_set().verbose(), false);
718+
assert!(config.was_set().hard_tabs());
719+
assert!(!config.was_set().verbose());
720720
}
721721

722722
const PRINT_DOCS_STABLE_OPTION: &str = "stable_option <boolean> Default: false";
@@ -732,9 +732,9 @@ mod test {
732732
Config::print_docs(&mut output, false);
733733

734734
let s = str::from_utf8(&output).unwrap();
735-
assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
736-
assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), false);
737-
assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
735+
assert!(s.contains(PRINT_DOCS_STABLE_OPTION));
736+
assert!(!s.contains(PRINT_DOCS_UNSTABLE_OPTION));
737+
assert!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION));
738738
}
739739

740740
#[test]
@@ -745,9 +745,9 @@ mod test {
745745
Config::print_docs(&mut output, true);
746746

747747
let s = str::from_utf8(&output).unwrap();
748-
assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
749-
assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), true);
750-
assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
748+
assert!(s.contains(PRINT_DOCS_STABLE_OPTION));
749+
assert!(s.contains(PRINT_DOCS_UNSTABLE_OPTION));
750+
assert!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION));
751751
}
752752

753753
#[test]
@@ -966,32 +966,32 @@ make_backup = false
966966
config.set().unstable_features(true);
967967
// When we don't set the config from toml or command line options it
968968
// doesn't get marked as set by the user.
969-
assert_eq!(config.was_set().unstable_features(), false);
969+
assert!(!config.was_set().unstable_features());
970970
config.set().unstable_features(true);
971-
assert_eq!(config.unstable_features(), true);
971+
assert!(config.unstable_features());
972972
}
973973

974974
#[nightly_only_test]
975975
#[test]
976976
fn test_unstable_from_toml() {
977977
let config =
978978
Config::from_toml("unstable_features = true", Path::new("./rustfmt.toml")).unwrap();
979-
assert_eq!(config.was_set().unstable_features(), true);
980-
assert_eq!(config.unstable_features(), true);
979+
assert!(config.was_set().unstable_features());
980+
assert!(config.unstable_features());
981981
}
982982

983983
#[test]
984984
fn test_set_cli() {
985985
let mut config = Config::default();
986-
assert_eq!(config.was_set().edition(), false);
987-
assert_eq!(config.was_set_cli().edition(), false);
986+
assert!(!config.was_set().edition());
987+
assert!(!config.was_set_cli().edition());
988988
config.set().edition(Edition::Edition2021);
989-
assert_eq!(config.was_set().edition(), false);
990-
assert_eq!(config.was_set_cli().edition(), false);
989+
assert!(!config.was_set().edition());
990+
assert!(!config.was_set_cli().edition());
991991
config.set_cli().edition(Edition::Edition2021);
992-
assert_eq!(config.was_set().edition(), false);
993-
assert_eq!(config.was_set_cli().edition(), true);
994-
assert_eq!(config.was_set_cli().emit_mode(), false);
992+
assert!(!config.was_set().edition());
993+
assert!(config.was_set_cli().edition());
994+
assert!(!config.was_set_cli().emit_mode());
995995
}
996996

997997
#[cfg(test)]
@@ -1365,7 +1365,7 @@ make_backup = false
13651365
let versions = vec!["0.0.0", "0.0.1", "0.1.0"];
13661366

13671367
for version in versions {
1368-
let toml = format!("required_version=\"{}\"", version.to_string());
1368+
let toml = format!("required_version=\"{}\"", version);
13691369
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();
13701370

13711371
assert!(!config.version_meets_requirement());
@@ -1389,8 +1389,8 @@ make_backup = false
13891389
for minor in current_version.minor..0 {
13901390
let toml = format!(
13911391
"required_version=\"^{}.{}.0\"",
1392-
current_version.major.to_string(),
1393-
minor.to_string()
1392+
current_version.major,
1393+
minor
13941394
);
13951395
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();
13961396

@@ -1434,7 +1434,7 @@ make_backup = false
14341434
#[nightly_only_test]
14351435
#[test]
14361436
fn test_required_version_exact_boundary() {
1437-
let toml = format!("required_version=\"{}\"", get_current_version().to_string());
1437+
let toml = format!("required_version=\"{}\"", get_current_version());
14381438
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();
14391439

14401440
assert!(config.version_meets_requirement());
@@ -1445,7 +1445,7 @@ make_backup = false
14451445
fn test_required_version_pre_release() {
14461446
let toml = format!(
14471447
"required_version=\"^{}-alpha\"",
1448-
get_current_version().to_string()
1448+
get_current_version()
14491449
);
14501450
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();
14511451

@@ -1457,7 +1457,7 @@ make_backup = false
14571457
fn test_required_version_with_build_metadata() {
14581458
let toml = format!(
14591459
"required_version=\"{}+build.1\"",
1460-
get_current_version().to_string()
1460+
get_current_version()
14611461
);
14621462

14631463
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

src/config/options.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ pub enum ReportTactic {
170170

171171
/// What Rustfmt should emit. Mostly corresponds to the `--emit` command line
172172
/// option.
173+
#[derive(Default)]
173174
#[config_type]
174175
pub enum EmitMode {
175176
/// Emits to files.
177+
#[default]
176178
Files,
177179
/// Writes the output to stdout.
178180
Stdout,
@@ -325,11 +327,6 @@ impl ::std::str::FromStr for WidthHeuristics {
325327
}
326328
}
327329

328-
impl Default for EmitMode {
329-
fn default() -> EmitMode {
330-
EmitMode::Files
331-
}
332-
}
333330

334331
/// A set of directories, files and modules that rustfmt should ignore.
335332
#[derive(Default, Clone, Debug, PartialEq)]
@@ -440,11 +437,13 @@ pub trait CliOptions {
440437
}
441438

442439
/// The edition of the syntax and semantics of code (RFC 2052).
440+
#[derive(Default)]
443441
#[config_type]
444442
pub enum Edition {
445443
#[value = "2015"]
446444
#[doc_hint = "2015"]
447445
/// Edition 2015.
446+
#[default]
448447
Edition2015,
449448
#[value = "2018"]
450449
#[doc_hint = "2018"]
@@ -460,11 +459,6 @@ pub enum Edition {
460459
Edition2024,
461460
}
462461

463-
impl Default for Edition {
464-
fn default() -> Edition {
465-
Edition::Edition2015
466-
}
467-
}
468462

469463
impl From<Edition> for rustc_span::edition::Edition {
470464
fn from(edition: Edition) -> Self {

src/emitter/checkstyle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mod tests {
9595
let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"];
9696
let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"];
9797
let mut writer = Vec::new();
98-
let mut emitter = CheckstyleEmitter::default();
98+
let mut emitter = CheckstyleEmitter;
9999
let _ = emitter.emit_header(&mut writer);
100100
let _ = emitter
101101
.emit_formatted_file(

src/emitter/checkstyle/xml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::{self, Display};
44
/// This is needed for checkstyle output.
55
pub(super) struct XmlEscaped<'a>(pub(super) &'a str);
66

7-
impl<'a> Display for XmlEscaped<'a> {
7+
impl Display for XmlEscaped<'_> {
88
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
99
for char in self.0.chars() {
1010
match char {

0 commit comments

Comments
 (0)