Skip to content

collapsible_else_if disregard platform code in else branch #14799

@pronebird

Description

@pronebird

Summary

Without much context, I have the following code:

if connectivity.is_offline() {
    NextTunnelState::SameState(self)
} else {
    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
    Self::reset_dns(shared_state).await;

    if self.reconnect {
        NextTunnelState::NewState(ConnectingState::enter(0, self.selected_gateways, shared_state).await)
    } else {
        NextTunnelState::NewState(DisconnectedState::enter(None, shared_state).await)
    }
}

When compiling for aarch64-apple-ios, clippy suggests to collapse the else block disregarding the platform code:

warning: this `else { if .. }` block can be collapsed
   --> crates/nym-vpn-lib/src/tunnel_state_machine/states/offline_state.rs:143:24
    |
143 |                   } else {
    |  ________________________^
144 | |                     #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
145 | |                     Self::reset_dns(shared_state).await;
...   |
152 | |                 }
    | |_________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if
    = note: `#[warn(clippy::collapsible_else_if)]` on by default
help: collapse nested if block
    |
143 ~                 } else if self.reconnect {
144 +                     NextTunnelState::NewState(ConnectingState::enter(0, self.selected_gateways, shared_state).await)
145 +                 } else {
146 +                     NextTunnelState::NewState(DisconnectedState::enter(None, shared_state).await)
147 +                 }

Which would result into code duplication which I am trying to avoid. (I do not mind nested if)

Lint Name

No response

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version


Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions