Skip to content

clippy::obfuscated_if_else fix fails #11141

Closed
@diondokter

Description

@diondokter

Summary

I ran clippy fix on my code and it had to revert.

https://github.com/diondokter/nrf-modem/blob/b88efe35f6abe19515e7ae3be8ac6431d55164c9/src/gnss.rs#L217-L239

Reproducer

I tried this code:

impl From<NmeaMask> for u16 {
    fn from(mask: NmeaMask) -> Self {
        mask.gga
            .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GGA_MASK as u16)
            .unwrap_or(0)
            | mask
                .gll
                .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16)
                .unwrap_or(0)
            | mask
                .gsa
                .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GSA_MASK as u16)
                .unwrap_or(0)
            | mask
                .gsv
                .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GSV_MASK as u16)
                .unwrap_or(0)
            | mask
                .rmc
                .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_RMC_MASK as u16)
                .unwrap_or(0)
    }
}

Instead, this happened:

Clippy output
warning: failed to automatically apply fixes suggested by rustc to crate `nrf_modem`

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

  * src\gnss.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: expected identifier, found keyword `if`
   --> src\gnss.rs:220:15
    |
220 |             | if mask
    |               ^^ expected identifier, found keyword

error: expected one of `,`, `:`, or `@`, found `mask`
   --> src\gnss.rs:220:18
    |
220 |             | if mask
    |                 -^^^^ expected one of `,`, `:`, or `@`
    |                 |
    |                 help: missing `,`

error: expected `,`
   --> src\gnss.rs:221:35
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                  ---              ^^
    |                  |
    |                  while parsing the fields for this pattern

error: expected one of `,`, `:`, or `@`, found `.`
   --> src\gnss.rs:221:17
    |
220 |             | if mask
    |                      -
    |                      |
    |                      expected one of `,`, `:`, or `@`
    |                      help: missing `,`
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                 ^ unexpected token

error: expected identifier, found keyword `else`
   --> src\gnss.rs:221:75
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                                                                           ^^^^ expected identifier, found keyword
    |
help: escape `else` to use it as an identifier
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } r#else { 0 }
    |                                                                           ++

error: expected one of `,` or `:`, found keyword `else`
   --> src\gnss.rs:221:75
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                                                                          -^^^^ expected one of `,` or `:`
    |                                                                          |
    |                                                                          help: missing `,`

error[E0422]: cannot find struct, variant or union type `gll` in this scope
   --> src\gnss.rs:221:18
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                  ^^^ not found in this scope

error[E0422]: cannot find struct, variant or union type `r#else` in this scope
   --> src\gnss.rs:221:75
    |
221 |                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |                                                                           ^^^^ not found in this scope

error[E0308]: mismatched types
   --> src\gnss.rs:219:23
    |
219 |         if mask.gga { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GGA_MASK as u16 } else { 0 }
    |         --------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------
    |         |             |
    |         |             expected `()`, found `u16`
    |         expected this to be `()`
    |
help: you might have meant to return this value
    |
219 |         if mask.gga { return nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GGA_MASK as u16; } else { 0 }
    |                       ++++++                                                 +

error[E0308]: mismatched types
   --> src\gnss.rs:219:81
    |
219 |         if mask.gga { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GGA_MASK as u16 } else { 0 }
    |         ------------------------------------------------------------------------^--
    |         |                                                                       |
    |         |                                                                       expected `()`, found integer
    |         expected this to be `()`
    |
help: you might have meant to return this value
    |
219 |         if mask.gga { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GGA_MASK as u16 } else { return 0; }
    |                                                                                 ++++++  +

error: aborting due to 10 previous errors

Some errors have detailed explanations: E0308, E0422.
For more information about an error, try `rustc --explain E0308`.

This is one of the diagnostics:

warning: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..`
   --> src\gnss.rs:222:15
    |
222 |               | mask
    |  _______________^
223 | |                 .gll
224 | |                 .then_some(nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16)
225 | |                 .unwrap_or(0)
    | |_____________________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#obfuscated_if_else
help: try
    |
222 ~             | if mask
223 +                 .gll { nrfxlib_sys::NRF_MODEM_GNSS_NMEA_GLL_MASK as u16 } else { 0 }
    |

Version

rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-pc-windows-msvc
release: 1.70.0
LLVM version: 16.0.2

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions