rust-lang / rust-lang/rust-clippy

`manual_map` removes log statements

Open
#16,193 1 comment 0 reactions 1 assignee View on GitHub

@willwang-io is already working on this.

Since Sep 5, 2026.

C-bug I-false-positive I-suggestion-causes-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Take this code:

fn issue16193(opt: Option<u32>) -> Option<u32> {
    if let Some(n) = opt {
        log::trace!("this will expand to nothing");
        Some(n + 1)
    } else {
        None
    }
}

Here, log::trace! (usually) expands to nothing. That trips up manual_map's get_some_expr, as it accepts, among other things, blocks which consist of just the final expression, and thus in particular:

{
    log::trace!("this will expand to nothing");
    Some(n + 1)
}

Judging by this comment:
https://github.com/rust-lang/rust-clippy/blob/6885741e18c780c4082c3b57a7743f42f4325bc6/clippy_lints/src/matches/manual_map.rs#L90

accepting blocks with statements is, at least currently, unintended.

Possible solution

Check that the block actually doesn't contain anything between the start of the block and the final expression. This could be done using span_contains_non_whitespace, but one would need to take care to trim the span of the block from the start, as it contains not only the opening brace, but also unsafe (if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided))

Lint Name

manual_map

Reproducer

I tried this code:

fn issue16193(opt: Option<u32>) -> Option<u32> {
    macro_rules! ignore {
        ($anything:tt) => {};
    }

    if let Some(n) = opt {
        ignore!("this will expand to nothing");
        Some(n + 1)
    } else {
        None
    }
}

I saw this happen:

warning: manual implementation of `Option::map`
  --> src/main.rs:9:5
   |
 9 | /     if let Some(n) = opt {
10 | |         ignore!("this will expand to nothing");
11 | |         Some(n + 1)
12 | |     } else {
13 | |         None
14 | |     }
   | |_____^ help: try: `opt.map(|n| n + 1)`
   |

I expected to see this happen:

  • Ideally, a suggestion containing the ignore!() call
  • Failing that, no suggestion at all.
Version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-unknown-linux-gnu
release: 1.91.1
LLVM version: 21.1.2
Additional Labels

@rustbot label I-suggestion-causes-bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.