Avoiding panicking using clippy - complains about tokio::select

I am writing a server and I am thinking about using some of the more restrictive clippy lints to catch possibly panicking code or lossy integer conversions.

My reasons are: On the one hand I am pretty excessive with unwrap() during debugging and initial writing and want to catch any remainder of it in the CI. On the other hand there is a lot of buffer handling in the code and I want to ensure that any programming error there does not lead to unwinding and the difficulties with panic safety.

At one point in the code base tokio is used and the tokio::select macro pulls in a panic. This obviously is complained about by clippy.

My questions are:

  • What do you think about the aim of enabling the really restrictive clippy lints, that catch most possibilities for a panic. Does it make sense? Should I worry less about panicking in a server and just catch the unwinding?
  • What should I do about tokio::select? Also I cannot just mute the warning by placing #[allow(clippy::panic)] on top of the select macro.

I mean, is the clippy lint right about it being able to panic?

Oh well, now I am starting to get how it works. I provided an else branch and the warning vanished.

But I am struggling how the docs apply to my case. The docs say:

select! panics if all branches are disabled and there is no provided else branch. A branch is disabled when the provided if precondition returns false or when the pattern does not match the result of <async expression>.

One of the places in my code looks like that:

async fn receive_signals() -> Result<()> {
    let mut sigterm = signal(SignalKind::terminate())?;
    let mut sigint = signal(SignalKind::interrupt())?;
    let mut sighup = signal(SignalKind::hangup())?;
    let mut sigquit = signal(SignalKind::quit())?;

    tokio::select! {
        _ = sigterm.recv() => Ok(()),
        _ = sigint.recv() => Ok(()),
        _ = sighup.recv() => Ok(()),
        _ = sigquit.recv() => Ok(()),
  • Yes there is no else branch
  • But I do not use preconditions
  • And in my opinion the pattern should always match

Indeed, the example you have posted cannot panic, but it sounds like clippy was not clever enough to determine that.

1 Like