Skip to content

needless-for-each does not add braces before unsafe block #12514

Closed
@JumperBot

Description

@JumperBot

Summary

Necessary curly braces not inserted before an unsafe block inside a for-each.
-D clippy::needless-for-each implied by -D clippy::pedantic

Reproducer

I tried this code:

// Where `array` is an `&mut Vec<T>`:
array.iter().for_each(|val| unsafe {
    // code goes here...
});

I expected to see this happen:

/// Notice that it automatically adds the necessary curly braces  : )
help: try
   |
11 ~         for val in array.iter() { unsafe {
12 +             // code goes here...
13 +         }}
   |

Instead, this happened:

help: try
   |
11 ~         for val in array.iter() unsafe {
12 +             // code goes here...
13 +         }
   |
/// This is the error before the attempt-to-replace
The following errors were reported:
error: expected `{`, found keyword `unsafe`
  --> ...
   |
11 |         for val in array.iter() unsafe {
   |                                 ^^^^^^ expected `{`
   |
help: try placing this code inside a block
   |
11 ~         for val in array.iter() { unsafe {
12 |             // code goes here...
13 ~         } }
   |

Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-pc-windows-gnu
release: 1.76.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +l-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions