Skip to content

cargo clippy --fix suggesting incorrect code #16654

@EmIsGreat

Description

@EmIsGreat

Summary

I was trying to run cargo clippy --fix and it failed due to it generating incorrect code. The message told me to create an issue so here I am.

/Documents/Stuff/Dev/Ensemble-Emulator/core copilot/prep..-publication >1 !1 > cargo clippy -p monsoon-cli --fix                                                                                                           node system 21:50:32
    Checking monsoon-cli v0.1.0 (/home/emily/Documents/Stuff/Dev/Ensemble-Emulator/cli)
warning: failed to automatically apply fixes suggested by rustc to crate `monsoon_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/cli/execution.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
   --> cli/src/cli/execution.rs:121:22
    |
119 | /             if let Ok(cond) = cond {
120 | |                 res.push(cond)
121 | |             } else { cond? }
    | |______________________^^^^^_- expected this to be `()`
    |                        |
    |                        expected `()`, found `StopCondition`
    |
help: consider using a semicolon here
    |
121 |             } else { cond?; }
    |                           +
help: consider using a semicolon here
    |
121 |             } else { cond? };
    |                             +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: this block may be rewritten with the `?` operator
   --> cli/src/cli/execution.rs:121:20
    |
121 |               } else if let Err(cond) = cond {
    |  ____________________^
122 | |                 return Err(cond)
123 | |             }
    | |_____________^ help: replace it with: `{ cond? }`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
    = note: `#[warn(clippy::question_mark)]` on by default

warning: `monsoon-cli` (lib) generated 1 warning (run `cargo clippy --fix --lib -p monsoon-cli -- --no-deps` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.86s

The code responsible for this behavior is this:

/// A stop condition that can be checked during execution
#[derive(Debug, Clone)]
pub enum StopCondition {
    /// Stop after N master cycles
    Cycles(u128),
    /// Stop after N frames
    Frames(u64),
    /// Stop when PC reaches address (breakpoint)
    PcEquals(u16),
    /// Stop when opcode is executed
    Opcode(u8),
    /// Stop when memory address equals value
    MemoryEquals {
        addr: u16,
        value: u8,
        and: Option<Box<StopCondition>>,
    },
    /// Stop when memory address does not equal value
    MemoryNotEquals {
        addr: u16,
        value: u8,
        and: Option<Box<StopCondition>>,
    },
    /// Stop on HLT instruction
    OnHalt,
    /// Breakpoint at address (alias for PcEquals, kept for backward compatibility)
    Breakpoint(u16),
    /// Watch memory address for access
    MemoryWatch {
        addr: u16,
        access_type: MemoryAccessType,
    },
}

impl StopCondition {
    /// Parse a memory condition string like "0x6000==0x80" or "0x6000!=0x00"
    pub fn parse_memory_condition(vec: &Vec<String>) -> Result<Vec<StopCondition>, String> {
        let mut res = Vec::new();
        for s in vec {
            let cond: Result<StopCondition, String> = Self::parse_single_condition(s);

            if let Ok(cond) = cond {
                res.push(cond)
            } else if let Err(cond) = cond {
                return Err(cond);
            }
        }

        Ok(res)
    }

    pub fn parse_single_condition(s: &String) -> Result<Self, String> {
        if let Some((cond1, cond2)) = s.split_once("&&") {
            let cond1 = Self::parse_single_condition(&cond1.to_string());
            let cond2 = Self::parse_single_condition(&cond2.to_string());

            if let (Ok(cond1), Ok(cond2)) = (cond1, cond2) {
                match cond1 {
                    StopCondition::MemoryEquals {
                        addr,
                        value,
                        ..
                    } => {
                        return Ok(StopCondition::MemoryEquals {
                            addr,
                            value,
                            and: Some(Box::new(cond2)),
                        });
                    }
                    StopCondition::MemoryNotEquals {
                        addr,
                        value,
                        ..
                    } => {
                        return Ok(StopCondition::MemoryNotEquals {
                            addr,
                            value,
                            and: Some(Box::new(cond2)),
                        });
                    }
                    _ => {}
                }
            }
        }

        if let Some((addr_str, val_str)) = s.split_once("==") {
            let addr = parse_hex_u16(addr_str.trim())?;
            let value = parse_hex_u8(val_str.trim())?;
            Ok(StopCondition::MemoryEquals {
                addr,
                value,
                and: None,
            })
        } else if let Some((addr_str, val_str)) = s.split_once("!=") {
            let addr = parse_hex_u16(addr_str.trim())?;
            let value = parse_hex_u8(val_str.trim())?;
            Ok(StopCondition::MemoryNotEquals {
                addr,
                value,
                and: None,
            })
        } else {
            Err(format!(
                "Invalid memory condition '{}'. Expected format: ADDR==VALUE or ADDR!=VALUE",
                s
            ))
        }
    }
}

Version

rustc 1.95.0-nightly (3a70d0349 2026-02-27)
binary: rustc
commit-hash: 3a70d0349fa378a10c3748f1a48742e61505020f
commit-date: 2026-02-27
host: x86_64-unknown-linux-gnu
release: 1.95.0-nightly
LLVM version: 22.1.0

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions