Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a couply of clippy findings #6007

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ 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())
}

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())
Expand Down
17 changes: 6 additions & 11 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
&& t.supports_color()
&& t.supports_attr(term::Attr::Bold) =>
{
true
}
_ => false,
}
term::stderr().is_some_and(|t| {
session.config.color().use_colored_tty()
&& t.supports_color()
&& t.supports_attr(term::Attr::Bold)
})
}

fn print_usage_to_stdout(opts: &Options, reason: &str) {
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 11 additions & 11 deletions src/cargo-fmt/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn default_options() {

#[test]
fn good_options() {
let o = Opts::parse_from(&[
let o = Opts::parse_from([
"test",
"-q",
"-p",
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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()
);
}
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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()
);
}
Expand All @@ -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()
);
}
Expand All @@ -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()
);
}
Expand All @@ -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()
);
}
8 changes: 4 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl 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
Expand Down Expand Up @@ -654,7 +654,7 @@ impl<'a> CommentRewrite<'a> {
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 => "",
});
Expand Down Expand Up @@ -836,7 +836,7 @@ impl<'a> CommentRewrite<'a> {
}
}

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
Expand Down Expand Up @@ -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));
Expand Down
14 changes: 7 additions & 7 deletions src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ mod tests {
#[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);
Expand All @@ -118,15 +118,15 @@ mod tests {
)
.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 `{}`" />"#,
XmlEscaped(r#" println!("Hello, world!");"#),
),
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 `{}`" />"#,
Expand All @@ -136,7 +136,7 @@ mod tests {
];
assert_eq!(
String::from_utf8(writer).unwrap(),
vec![
[
r#"<?xml version="1.0" encoding="utf-8"?>"#,
"\n",
r#"<checkstyle version="4.3">"#,
Expand Down
12 changes: 6 additions & 6 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod tests {
#[test]
fn emits_array_with_files_with_diffs() {
let file_name = "src/bin.rs";
let original = vec![
let original = [
"fn main() {",
"println!(\"Hello, world!\");",
"}",
Expand All @@ -225,7 +225,7 @@ mod tests {
"}",
"}",
];
let formatted = vec![
let formatted = [
"fn main() {",
" println!(\"Hello, world!\");",
"}",
Expand Down Expand Up @@ -285,11 +285,11 @@ mod tests {
#[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);
Expand Down
12 changes: 6 additions & 6 deletions src/format-diff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -250,7 +250,7 @@ mod cmd_line_tests {
fn unexpected_option() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "unexpected"])
.try_get_matches_from(["test", "unexpected"])
.is_err()
);
}
Expand All @@ -259,7 +259,7 @@ mod cmd_line_tests {
fn unexpected_flag() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "--flag"])
.try_get_matches_from(["test", "--flag"])
.is_err()
);
}
Expand All @@ -268,7 +268,7 @@ mod cmd_line_tests {
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()
);
}
Expand All @@ -277,7 +277,7 @@ mod cmd_line_tests {
fn negative_filter() {
assert!(
Opts::command()
.try_get_matches_from(&["test", "-p", "-1"])
.try_get_matches_from(["test", "-p", "-1"])
.is_err()
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ impl FormatReport {
self.internal
.borrow()
.0
.iter()
.map(|(_, errors)| errors.len())
.values()
.map(|errors| errors.len())
.sum()
}

Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion src/source_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
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,
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading