fix a couply of clippy findings (#6007)

* clippy: autofix some lint warnings
* fix a couple more clippy warnings
This commit is contained in:
Matthias Krüger 2024-01-06 23:52:26 +01:00 committed by GitHub
parent 85e21fabf4
commit 75e3172aaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 48 additions and 53 deletions

View File

@ -40,7 +40,7 @@ fn channel() -> String {
fn commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
@ -48,7 +48,7 @@ fn commit_hash() -> Option<String> {
fn commit_date() -> Option<String> {
Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.args(["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())

View File

@ -387,16 +387,11 @@ fn format_and_emit_report<T: Write>(session: &mut Session<'_, T>, input: Input)
}
fn should_print_with_colors<T: Write>(session: &mut Session<'_, T>) -> bool {
match term::stderr() {
Some(ref t)
if session.config.color().use_colored_tty()
term::stderr().is_some_and(|t| {
session.config.color().use_colored_tty()
&& t.supports_color()
&& t.supports_attr(term::Attr::Bold) =>
{
true
}
_ => false,
}
&& t.supports_attr(term::Attr::Bold)
})
}
fn print_usage_to_stdout(opts: &Options, reason: &str) {
@ -445,7 +440,7 @@ fn print_version() {
fn determine_operation(matches: &Matches) -> Result<Operation, OperationError> {
if matches.opt_present("h") {
let topic = matches.opt_str("h");
if topic == None {
if topic.is_none() {
return Ok(Operation::Help(HelpOp::None));
} else if topic == Some("config".to_owned()) {
return Ok(Operation::Help(HelpOp::Config));

View File

@ -507,7 +507,7 @@ fn run_rustfmt(
let mut command = rustfmt_command()
.stdout(stdout)
.args(files)
.args(&["--edition", edition.as_str()])
.args(["--edition", edition.as_str()])
.args(fmt_args)
.spawn()
.map_err(|e| match e.kind() {

View File

@ -20,7 +20,7 @@ fn default_options() {
#[test]
fn good_options() {
let o = Opts::parse_from(&[
let o = Opts::parse_from([
"test",
"-q",
"-p",
@ -48,7 +48,7 @@ fn good_options() {
fn unexpected_option() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "unexpected"])
.try_get_matches_from(["test", "unexpected"])
.is_err()
);
}
@ -57,7 +57,7 @@ fn unexpected_option() {
fn unexpected_flag() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "--flag"])
.try_get_matches_from(["test", "--flag"])
.is_err()
);
}
@ -66,19 +66,19 @@ fn unexpected_flag() {
fn mandatory_separator() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "--emit"])
.try_get_matches_from(["test", "--emit"])
.is_err()
);
assert!(
Opts::command()
.try_get_matches_from(&["test", "--", "--emit"])
.try_get_matches_from(["test", "--", "--emit"])
.is_ok()
);
}
#[test]
fn multiple_packages_one_by_one() {
let o = Opts::parse_from(&[
let o = Opts::parse_from([
"test",
"-p",
"package1",
@ -92,7 +92,7 @@ fn multiple_packages_one_by_one() {
#[test]
fn multiple_packages_grouped() {
let o = Opts::parse_from(&[
let o = Opts::parse_from([
"test",
"--package",
"package1",
@ -108,7 +108,7 @@ fn multiple_packages_grouped() {
fn empty_packages_1() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p"])
.try_get_matches_from(["test", "-p"])
.is_err()
);
}
@ -117,7 +117,7 @@ fn empty_packages_1() {
fn empty_packages_2() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "--", "--check"])
.try_get_matches_from(["test", "-p", "--", "--check"])
.is_err()
);
}
@ -126,7 +126,7 @@ fn empty_packages_2() {
fn empty_packages_3() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "--verbose"])
.try_get_matches_from(["test", "-p", "--verbose"])
.is_err()
);
}
@ -135,7 +135,7 @@ fn empty_packages_3() {
fn empty_packages_4() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "--check"])
.try_get_matches_from(["test", "-p", "--check"])
.is_err()
);
}

View File

@ -503,7 +503,7 @@ fn new(line: &str) -> Option<ItemizedBlock> {
let mut line_start = " ".repeat(indent);
// Markdown blockquote start with a "> "
if line.trim_start().starts_with(">") {
if line.trim_start().starts_with('>') {
// remove the original +2 indent because there might be multiple nested block quotes
// and it's easier to reason about the final indent by just taking the length
// of the new line_start. We update the indent because it effects the max width
@ -654,7 +654,7 @@ fn join_block(s: &str, sep: &str) -> String {
while let Some(line) = iter.next() {
result.push_str(line);
result.push_str(match iter.peek() {
Some(next_line) if next_line.is_empty() => sep.trim_end(),
Some(&"") => sep.trim_end(),
Some(..) => sep,
None => "",
});
@ -836,7 +836,7 @@ fn handle_line(
}
}
let is_markdown_header_doc_comment = is_doc_comment && line.starts_with("#");
let is_markdown_header_doc_comment = is_doc_comment && line.starts_with('#');
// We only want to wrap the comment if:
// 1) wrap_comments = true is configured
@ -1761,7 +1761,7 @@ fn changed_comment_content(orig: &str, new: &str) -> bool {
let code_comment_content = |code| {
let slices = UngroupedCommentCodeSlices::new(code);
slices
.filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
.filter(|(kind, _, _)| *kind == CodeCharKind::Comment)
.flat_map(|(_, _, s)| CommentReducer::new(s))
};
let res = code_comment_content(orig).ne(code_comment_content(new));

View File

@ -89,11 +89,11 @@ fn emits_empty_record_on_file_with_no_mismatches() {
#[test]
fn emits_single_xml_tree_containing_all_files() {
let bin_file = "src/bin.rs";
let bin_original = vec!["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = vec!["fn main() {", " println!(\"Hello, world!\");", "}"];
let bin_original = ["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = ["fn main() {", " println!(\"Hello, world!\");", "}"];
let lib_file = "src/lib.rs";
let lib_original = vec!["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = vec!["fn greet() {", " println!(\"Greetings!\");", "}"];
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 _ = emitter.emit_header(&mut writer);
@ -118,7 +118,7 @@ fn emits_single_xml_tree_containing_all_files() {
)
.unwrap();
let _ = emitter.emit_footer(&mut writer);
let exp_bin_xml = vec![
let exp_bin_xml = [
format!(r#"<file name="{}">"#, bin_file),
format!(
r#"<error line="2" severity="warning" message="Should be `{}`" />"#,
@ -126,7 +126,7 @@ fn emits_single_xml_tree_containing_all_files() {
),
String::from("</file>"),
];
let exp_lib_xml = vec![
let exp_lib_xml = [
format!(r#"<file name="{}">"#, lib_file),
format!(
r#"<error line="2" severity="warning" message="Should be `{}`" />"#,
@ -136,7 +136,7 @@ fn emits_single_xml_tree_containing_all_files() {
];
assert_eq!(
String::from_utf8(writer).unwrap(),
vec![
[
r#"<?xml version="1.0" encoding="utf-8"?>"#,
"\n",
r#"<checkstyle version="4.3">"#,

View File

@ -212,7 +212,7 @@ fn emits_empty_array_on_no_diffs() {
#[test]
fn emits_array_with_files_with_diffs() {
let file_name = "src/bin.rs";
let original = vec![
let original = [
"fn main() {",
"println!(\"Hello, world!\");",
"}",
@ -225,7 +225,7 @@ fn emits_array_with_files_with_diffs() {
"}",
"}",
];
let formatted = vec![
let formatted = [
"fn main() {",
" println!(\"Hello, world!\");",
"}",
@ -285,11 +285,11 @@ fn emits_array_with_files_with_diffs() {
#[test]
fn emits_valid_json_with_multiple_files() {
let bin_file = "src/bin.rs";
let bin_original = vec!["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = vec!["fn main() {", " println!(\"Hello, world!\");", "}"];
let bin_original = ["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = ["fn main() {", " println!(\"Hello, world!\");", "}"];
let lib_file = "src/lib.rs";
let lib_original = vec!["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = vec!["fn greet() {", " println!(\"Greetings!\");", "}"];
let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"];
let mut writer = Vec::new();
let mut emitter = JsonEmitter::default();
let _ = emitter.emit_header(&mut writer);

View File

@ -234,14 +234,14 @@ mod cmd_line_tests {
#[test]
fn default_options() {
let empty: Vec<String> = vec![];
let o = Opts::parse_from(&empty);
let o = Opts::parse_from(empty);
assert_eq!(DEFAULT_PATTERN, o.filter);
assert_eq!(0, o.skip_prefix);
}
#[test]
fn good_options() {
let o = Opts::parse_from(&["test", "-p", "10", "-f", r".*\.hs"]);
let o = Opts::parse_from(["test", "-p", "10", "-f", r".*\.hs"]);
assert_eq!(r".*\.hs", o.filter);
assert_eq!(10, o.skip_prefix);
}
@ -250,7 +250,7 @@ fn good_options() {
fn unexpected_option() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "unexpected"])
.try_get_matches_from(["test", "unexpected"])
.is_err()
);
}
@ -259,7 +259,7 @@ fn unexpected_option() {
fn unexpected_flag() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "--flag"])
.try_get_matches_from(["test", "--flag"])
.is_err()
);
}
@ -268,7 +268,7 @@ fn unexpected_flag() {
fn overridden_option() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "10", "-p", "20"])
.try_get_matches_from(["test", "-p", "10", "-p", "20"])
.is_err()
);
}
@ -277,7 +277,7 @@ fn overridden_option() {
fn negative_filter() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "-1"])
.try_get_matches_from(["test", "-p", "-1"])
.is_err()
);
}

View File

@ -257,8 +257,8 @@ fn warning_count(&self) -> usize {
self.internal
.borrow()
.0
.iter()
.map(|(_, errors)| errors.len())
.values()
.map(|errors| errors.len())
.sum()
}

View File

@ -389,7 +389,7 @@ fn rewrite_empty_macro_def_body(
stmts: vec![].into(),
id: rustc_ast::node_id::DUMMY_NODE_ID,
rules: ast::BlockCheckMode::Default,
span: span,
span,
tokens: None,
could_be_bare_literal: false,
};

View File

@ -33,7 +33,7 @@ pub(crate) fn write_all_files<T>(
let mut emitter = create_emitter(config);
emitter.emit_header(out)?;
for &(ref filename, ref text) in source_file {
for (filename, text) in source_file {
write_file(
None,
filename,

View File

@ -332,7 +332,7 @@ fn assert_stdin_output(
let mut session = Session::new(config, Some(&mut buf));
session.format(input).unwrap();
let errors = ReportedErrors {
has_diff: has_diff,
has_diff,
..Default::default()
};
assert_eq!(session.errors, errors);