rust-lang / rust-lang/rust-clippy

the advice for unused return value of `std::option::Option::<T>::map_or` is wrong

Open
#11,282 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

before:

error: unused return value of `std::option::Option::<T>::map_or` that must be used
   --> src/xxx.rs:119:9
    |
119 | /         self.m.get_mut(&key).map_or((), |series| {
120 | |             if let Some(last_elem) = series.last() {
121 | |                 if last_elem.timestamp == timestamp {
122 | |                     series.pop();
...   |
125 | |             series.push(Value { value, timestamp });
126 | |         });
    | |__________^
    |
    = note: if you don't need the returned value, use `if let` instead
    = note: `-D unused-must-use` implied by `-D warnings`
help: use `let _ = ...` to ignore the resulting value
    |
119 |         let _ = self.m.get_mut(&key).map_or((), |series| {
    |         +++++++

after:

error: this let-binding has unit value
   --> src/xxx.rs:119:9
    |
119 | /         let _ = self.m.get_mut(&key).map_or((), |series| {
120 | |             if let Some(last_elem) = series.last() {
121 | |                 if last_elem.timestamp == timestamp {
122 | |                     series.pop();
...   |
125 | |             series.push(Value { value, timestamp });
126 | |         });
    | |___________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
    = note: `-D clippy::let-unit-value` implied by `-D warnings`
help: omit the `let` binding
    |
119 ~         self.m.get_mut(&key).map_or((), |series| {
120 +             if let Some(last_elem) = series.last() {
121 +                 if last_elem.timestamp == timestamp {
122 +                     series.pop();
123 +                 }
124 +             }
125 +             series.push(Value { value, timestamp });
126 +         });
Reproducer

I tried this code:

pub struct TimeMap2 {
    m: HashMap<String, Vec<Value>>,
}

impl TimeMap2 {
    pub fn new() -> Self {
        Self { m: HashMap::new() }
    }

    pub fn set(&mut self, key: String, value: String, timestamp: i32) {
        if !self.m.contains_key(&key) {
            self.m.insert(key.clone(), Vec::new());
        }

        self.m.get_mut(&key).map_or((), |series| {
            if let Some(last_elem) = series.last() {
                if last_elem.timestamp == timestamp {
                    series.pop();
                }
            }
            series.push(Value { value, timestamp });
        });
    }

    pub fn get(&self, key: String, timestamp: i32) -> String {
        if let Some(series) = self.m.get(&key) {
            if series.is_empty() {
                return String::new();
            }
            return match series.binary_search_by_key(&timestamp, |v| v.timestamp) {
                Ok(idx) => series[idx].value.clone(),
                Err(idx) => {
                    if let Some(val) = series.get(idx - 1) {
                        return val.value.clone();
                    }
                    String::new()
                }
            };
        }
        String::new()
    }
}

I expected to see this happen:

Instead, this happened:

Version
rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.71.0
LLVM version: 16.0.5
Additional Labels

No response

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.

Research direction

Start with the reproducer and compare the two diagnostic outputs for the map_or call. Trace the lint that emits the unused-return-value suggestion and its interaction with let_unit_value; done means the advice no longer recommends a change that triggers the conflicting lint, with coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.