]> git.lizzy.rs Git - rust.git/commitdiff
clippy: cargo-miri
authorRalf Jung <post@ralfj.de>
Sat, 4 Jun 2022 16:11:23 +0000 (12:11 -0400)
committerRalf Jung <post@ralfj.de>
Sat, 4 Jun 2022 17:37:37 +0000 (13:37 -0400)
cargo-miri/bin.rs

index 34904279e94f1810b8c27ee9deb901b0dd2586df..ba885d307a85f630b2e465ccbc88609025a28a46 100644 (file)
@@ -1,3 +1,5 @@
+#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq)]
+
 mod version;
 
 use std::env;
@@ -96,6 +98,9 @@ fn show_version() {
     // Only use `option_env` on vergen variables to ensure the build succeeds
     // when vergen failed to find the git info.
     if let Some(sha) = option_env!("VERGEN_GIT_SHA_SHORT") {
+        // This `unwrap` can never fail because if VERGEN_GIT_SHA_SHORT exists, then so does
+        // VERGEN_GIT_COMMIT_DATE.
+        #[allow(clippy::option_env_unwrap)]
         write!(&mut version, " ({} {})", sha, option_env!("VERGEN_GIT_COMMIT_DATE").unwrap())
             .unwrap();
     }
@@ -135,16 +140,14 @@ impl<I: Iterator<Item = String>> Iterator for ArgSplitFlagValue<'_, I> {
 
     fn next(&mut self) -> Option<Self::Item> {
         let arg = self.args.next()?;
-        if arg.starts_with(self.name) {
+        if let Some(suffix) = arg.strip_prefix(self.name) {
             // Strip leading `name`.
-            let suffix = &arg[self.name.len()..];
             if suffix.is_empty() {
                 // This argument is exactly `name`; the next one is the value.
                 return self.args.next().map(Ok);
-            } else if suffix.starts_with('=') {
+            } else if let Some(suffix) = suffix.strip_prefix('=') {
                 // This argument is `name=value`; get the value.
-                // Strip leading `=`.
-                return Some(Ok(suffix[1..].to_owned()));
+                return Some(Ok(suffix.to_owned()));
             }
         }
         Some(Err(arg))
@@ -255,7 +258,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
     let line = out
         .stderr
         .lines()
-        .nth(0)
+        .next()
         .expect("malformed `xargo --version` output: not at least one line")
         .expect("malformed `xargo --version` output: error reading first line");
     let (name, version) = {
@@ -285,7 +288,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
         .expect("malformed `xargo --version` output: not a patch version piece")
         .parse()
         .expect("malformed `xargo --version` output: patch version is not an integer");
-    if !version_pieces.next().is_none() {
+    if version_pieces.next().is_some() {
         panic!("malformed `xargo --version` output: more than three pieces in version");
     }
     Some((major, minor, patch))
@@ -311,7 +314,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
         println!("Running `{:?}` to {}.", cmd, text);
     }
 
-    if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
+    if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
         show_error(format!("failed to {}", text));
     }
 }
@@ -499,10 +502,11 @@ fn get_cargo_metadata() -> Metadata {
     for arg in ArgSplitFlagValue::new(
         env::args().skip(3), // skip the program name, "miri" and "run" / "test"
         config_flag,
-    ) {
-        if let Ok(config) = arg {
-            cmd.arg(config_flag).arg(config);
-        }
+    )
+    // Only look at `Ok`
+    .flatten()
+    {
+        cmd.arg(config_flag).arg(arg);
     }
     let mut child = cmd
         .stdin(process::Stdio::null())
@@ -524,11 +528,11 @@ fn get_cargo_metadata() -> Metadata {
 /// Additionally, somewhere between cargo metadata and TyCtxt, '-' gets replaced with '_' so we
 /// make that same transformation here.
 fn local_crates(metadata: &Metadata) -> String {
-    assert!(metadata.workspace_members.len() > 0);
+    assert!(!metadata.workspace_members.is_empty());
     let mut local_crates = String::new();
     for member in &metadata.workspace_members {
-        let name = member.split(" ").nth(0).unwrap();
-        let name = name.replace("-", "_");
+        let name = member.split(' ').next().unwrap();
+        let name = name.replace('-', "_");
         local_crates.push_str(&name);
         local_crates.push(',');
     }
@@ -708,7 +712,7 @@ fn out_filename(prefix: &str, suffix: &str) -> PathBuf {
                 get_arg_flag_value("--crate-name").unwrap(),
                 // This is technically a `-C` flag but the prefix seems unique enough...
                 // (and cargo passes this before the filename so it should be unique)
-                get_arg_flag_value("extra-filename").unwrap_or(String::new()),
+                get_arg_flag_value("extra-filename").unwrap_or_default(),
                 suffix,
             ));
             path
@@ -808,11 +812,10 @@ fn out_filename(prefix: &str, suffix: &str) -> PathBuf {
         // Forward arguments, but remove "link" from "--emit" to make this a check-only build.
         let emit_flag = "--emit";
         while let Some(arg) = args.next() {
-            if arg.starts_with(emit_flag) {
+            if let Some(val) = arg.strip_prefix(emit_flag) {
                 // Patch this argument. First, extract its value.
-                let val = &arg[emit_flag.len()..];
-                assert!(val.starts_with("="), "`cargo` should pass `--emit=X` as one argument");
-                let val = &val[1..];
+                let val =
+                    val.strip_prefix('=').expect("`cargo` should pass `--emit=X` as one argument");
                 let mut val: Vec<_> = val.split(',').collect();
                 // Now make sure "link" is not in there, but "metadata" is.
                 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) {
     while let Some(arg) = args.next() {
         if arg == "--extern" {
             forward_patched_extern_arg(&mut args, &mut cmd);
-        } else if arg.starts_with(error_format_flag) {
-            let suffix = &arg[error_format_flag.len()..];
+        } else if let Some(suffix) = arg.strip_prefix(error_format_flag) {
             assert!(suffix.starts_with('='));
             // Drop this argument.
-        } else if arg.starts_with(json_flag) {
-            let suffix = &arg[json_flag.len()..];
+        } else if let Some(suffix) = arg.strip_prefix(json_flag) {
             assert!(suffix.starts_with('='));
             // Drop this argument.
         } else {