Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option::is_none_or: unnecessary_map_or lint doesn't work on references #14023

Open
Azkellas opened this issue Jan 18, 2025 · 2 comments · May be fixed by #14024
Open

Option::is_none_or: unnecessary_map_or lint doesn't work on references #14023

Azkellas opened this issue Jan 18, 2025 · 2 comments · May be fixed by #14024
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@Azkellas
Copy link

Azkellas commented Jan 18, 2025

Summary

Using Option::map_or to emulates Option::is_none_or fails to generate a warning when dereferencing.

Lint Name

unnecessary_map_or

Reproducer

Running this code

fn test(a: Option<f32>) {
    println!("{}", a.map_or(true, |f| f < 0.0))
}

fn test_ref(a: &Option<f32>) {
    println!("{}", a.map_or(true, |f| f < 0.0))
}

fn test_ref_mut(a: &mut Option<f32>) {
    println!("{}", a.map_or(true, |f| f < 0.0))
}

fn main() {
    test(Some(1.0));
    test_ref(&Some(1.0));
    test_ref_mut(&mut Some(1.0));
}

link to the playground

I expected a warning for each case, so three in total

Instead, this happened:

Checking playground v0.0.1 (/playground)
warning: this `map_or` can be simplified
 --> src/main.rs:3:20
  |
3 |     println!("{}", a.map_or(true, |f| f < 0.0))
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use is_none_or instead: `a.is_none_or(|f| f < 0.0)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_or
  = note: `#[warn(clippy::unnecessary_map_or)]` on by default

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.47s

Version

1.86.0-nightly

(2025-01-17 6067b36314ab5eb2eb47)
@Azkellas Azkellas added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Jan 18, 2025
@samueltardieu
Copy link
Contributor

@rustbot claim

@samueltardieu
Copy link
Contributor

Thanks for the report @Azkellas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants