`map_unwrap_or` changes type inference, causes compilation error

Open
#11,480 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Search for the map_unwrap_or lint implementation and reproduce the two examples from the issue to observe the type-inference difference. Add regression coverage for the slice fallback case, then verify that the lint suggestion compiles without changing the empty slice into a zero-length array.

Written by the indexing model from the issue text.

Description

C-bug I-suggestion-causes-error
Summary

In map_or the fallback value dictates inferred type, while with unwrap_or the fallback value is coerced to map's type.

Code suggested by clippy breaks if the map returns a slice, and the fallback value is &[], because it changes meaning from an empty slice to a 0-length array.

Reproducer
   let works = Some(&[1]).map(|m| &m[..]).unwrap_or(&[]);
   let does_not_compile = Some(&[1]).map_or(&[], |m| &m[..]);
Version
rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: aarch64-apple-darwin
release: 1.71.1
LLVM version: 16.0.5
Additional Labels

@rustbot label +I-suggestion-causes-error

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

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.