Skip to content

manual_map removes log statements #16193

@ada4a

Description

@ada4a

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:

// TODO: Allow more complex expressions.

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

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