Skip to content

Commit 3d30aec

Browse files
committed
clippy: cargo-miri
1 parent 151b6b1 commit 3d30aec

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

cargo-miri/bin.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq)]
2+
13
mod version;
24

35
use std::env;
@@ -96,6 +98,9 @@ fn show_version() {
9698
// Only use `option_env` on vergen variables to ensure the build succeeds
9799
// when vergen failed to find the git info.
98100
if let Some(sha) = option_env!("VERGEN_GIT_SHA_SHORT") {
101+
// This `unwrap` can never fail because if VERGEN_GIT_SHA_SHORT exists, then so does
102+
// VERGEN_GIT_COMMIT_DATE.
103+
#[allow(clippy::option_env_unwrap)]
99104
write!(&mut version, " ({} {})", sha, option_env!("VERGEN_GIT_COMMIT_DATE").unwrap())
100105
.unwrap();
101106
}
@@ -135,16 +140,14 @@ impl<I: Iterator<Item = String>> Iterator for ArgSplitFlagValue<'_, I> {
135140

136141
fn next(&mut self) -> Option<Self::Item> {
137142
let arg = self.args.next()?;
138-
if arg.starts_with(self.name) {
143+
if let Some(suffix) = arg.strip_prefix(self.name) {
139144
// Strip leading `name`.
140-
let suffix = &arg[self.name.len()..];
141145
if suffix.is_empty() {
142146
// This argument is exactly `name`; the next one is the value.
143147
return self.args.next().map(Ok);
144-
} else if suffix.starts_with('=') {
148+
} else if let Some(suffix) = suffix.strip_prefix('=') {
145149
// This argument is `name=value`; get the value.
146-
// Strip leading `=`.
147-
return Some(Ok(suffix[1..].to_owned()));
150+
return Some(Ok(suffix.to_owned()));
148151
}
149152
}
150153
Some(Err(arg))
@@ -255,7 +258,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
255258
let line = out
256259
.stderr
257260
.lines()
258-
.nth(0)
261+
.next()
259262
.expect("malformed `xargo --version` output: not at least one line")
260263
.expect("malformed `xargo --version` output: error reading first line");
261264
let (name, version) = {
@@ -285,7 +288,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
285288
.expect("malformed `xargo --version` output: not a patch version piece")
286289
.parse()
287290
.expect("malformed `xargo --version` output: patch version is not an integer");
288-
if !version_pieces.next().is_none() {
291+
if version_pieces.next().is_some() {
289292
panic!("malformed `xargo --version` output: more than three pieces in version");
290293
}
291294
Some((major, minor, patch))
@@ -311,7 +314,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
311314
println!("Running `{:?}` to {}.", cmd, text);
312315
}
313316

314-
if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
317+
if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
315318
show_error(format!("failed to {}", text));
316319
}
317320
}
@@ -499,10 +502,11 @@ fn get_cargo_metadata() -> Metadata {
499502
for arg in ArgSplitFlagValue::new(
500503
env::args().skip(3), // skip the program name, "miri" and "run" / "test"
501504
config_flag,
502-
) {
503-
if let Ok(config) = arg {
504-
cmd.arg(config_flag).arg(config);
505-
}
505+
)
506+
// Only look at `Ok`
507+
.flatten()
508+
{
509+
cmd.arg(config_flag).arg(arg);
506510
}
507511
let mut child = cmd
508512
.stdin(process::Stdio::null())
@@ -524,11 +528,11 @@ fn get_cargo_metadata() -> Metadata {
524528
/// Additionally, somewhere between cargo metadata and TyCtxt, '-' gets replaced with '_' so we
525529
/// make that same transformation here.
526530
fn local_crates(metadata: &Metadata) -> String {
527-
assert!(metadata.workspace_members.len() > 0);
531+
assert!(!metadata.workspace_members.is_empty());
528532
let mut local_crates = String::new();
529533
for member in &metadata.workspace_members {
530-
let name = member.split(" ").nth(0).unwrap();
531-
let name = name.replace("-", "_");
534+
let name = member.split(' ').next().unwrap();
535+
let name = name.replace('-', "_");
532536
local_crates.push_str(&name);
533537
local_crates.push(',');
534538
}
@@ -708,7 +712,7 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
708712
get_arg_flag_value("--crate-name").unwrap(),
709713
// This is technically a `-C` flag but the prefix seems unique enough...
710714
// (and cargo passes this before the filename so it should be unique)
711-
get_arg_flag_value("extra-filename").unwrap_or(String::new()),
715+
get_arg_flag_value("extra-filename").unwrap_or_default(),
712716
suffix,
713717
));
714718
path
@@ -808,11 +812,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
808812
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
809813
let emit_flag = "--emit";
810814
while let Some(arg) = args.next() {
811-
if arg.starts_with(emit_flag) {
815+
if let Some(val) = arg.strip_prefix(emit_flag) {
812816
// Patch this argument. First, extract its value.
813-
let val = &arg[emit_flag.len()..];
814-
assert!(val.starts_with("="), "`cargo` should pass `--emit=X` as one argument");
815-
let val = &val[1..];
817+
let val =
818+
val.strip_prefix('=').expect("`cargo` should pass `--emit=X` as one argument");
816819
let mut val: Vec<_> = val.split(',').collect();
817820
// Now make sure "link" is not in there, but "metadata" is.
818821
if let Some(i) = val.iter().position(|&s| s == "link") {
@@ -937,12 +940,10 @@ fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
937940
while let Some(arg) = args.next() {
938941
if arg == "--extern" {
939942
forward_patched_extern_arg(&mut args, &mut cmd);
940-
} else if arg.starts_with(error_format_flag) {
941-
let suffix = &arg[error_format_flag.len()..];
943+
} else if let Some(suffix) = arg.strip_prefix(error_format_flag) {
942944
assert!(suffix.starts_with('='));
943945
// Drop this argument.
944-
} else if arg.starts_with(json_flag) {
945-
let suffix = &arg[json_flag.len()..];
946+
} else if let Some(suffix) = arg.strip_prefix(json_flag) {
946947
assert!(suffix.starts_with('='));
947948
// Drop this argument.
948949
} else {

0 commit comments

Comments
 (0)