Skip to content

multiple_unsafe_ops_per_block false positive when future is awaited inside unsafe block #13879

@MolotovCherry

Description

@MolotovCherry

Summary

multiple_unsafe_ops_per_block lint complains about multiple unsafe op calls if future is awaited inside unsafe block

That is, the following code complains about 2 unsafe ops in the unsafe block containing the await, despite there being no visible unsafety in user code

let fut = foo();
unsafe { fut.await; } 

Reproducer

#![warn(clippy::multiple_unsafe_ops_per_block)]

async fn foo() {}
    
async fn bar() {
  let fut = foo();
  unsafe { fut.await; } 
}

Version

Reproduced in rust playground

clippy 0.1.83 (2024-11-26 90b35a6)
rustc 1.85.0-nightly (2024-12-24 409998c4e8cae45344fd)

Warnings

warning: this `unsafe` block contains 2 unsafe operations, expected only one
 --> src/main.rs:8:3
  |
8 |   unsafe { fut.await; } 
  |   ^^^^^^^^^^^^^^^^^^^^^
  |
note: unsafe function call occurs here
 --> src/main.rs:8:16
  |
8 |   unsafe { fut.await; } 
  |                ^^^^^
note: unsafe function call occurs here
 --> src/main.rs:8:12
  |
8 |   unsafe { fut.await; } 
  |            ^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_unsafe_ops_per_block
note: the lint level is defined here
 --> src/main.rs:1:54
  |
1 | #![allow(dead_code, clippy::let_unit_value)] #![warn(clippy::multiple_unsafe_ops_per_block)]
  |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `playground` (bin "playground") generated 1 warning

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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions