Skip to content

Can rust provide a warning on unreachable if patterns? #15736

Closed
@farcaller

Description

@farcaller

Here's a bug generated by a simple copy-paste mistake:

fn init_flash_access(freq: u32) {
  let num_clocks: u32 = if freq > 100_000_000 { 6 } else
                        if freq > 80_000_000 { 5 } else
                        if freq > 60_000_000 { 4 } else
                        if freq > 40_000_000 { 3 } else
                        if freq > 40_000_000 { 2 } else
                        { 1 };
  let val = (num_clocks - 1) << 12;
  reg::FLASHCFG.set_value(val);
}

This function will never return 2, as 40_000_000 in the last line should actually be 20_000_000. Is there any way rust could warn me about that or any other applicable pattern to do it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions