rust-lang / rust-lang/rust-clippy

generators: redundant_pattern_matching triggers on yield

Open
#10,451 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

.

Lint Name

redundant_pattern_matching

Reproducer

I tried this code:

#![feature(generator_trait)]
#![feature(generators)]
#![deny(unused_braces, unused_parens)]

use std::ops::Generator;
use std::pin::Pin;

fn main() {
    let mut x = |_| {
        while let Some(_) = (yield) {}
 
    };
    let _ = Pin::new(&mut x).resume(Some(5));
}

I saw this happen:

warning: redundant pattern matching, consider using `is_some()`
  --> issue-74883-unused-paren-baren-yield.rs:10:19
   |
10 |         while let Some(_) = (yield) {}
   |         ----------^^^^^^^---------- help: try this: `while (yield).is_some()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

Using the suggested yield.is_some() introduces a type error:

error[E0282]: type annotations needed
  --> issue-74883-unused-paren-baren-yield.rs:9:18
   |
9  |     let mut x = |_| {
   |                  ^
10 |         while (yield).is_some() {}
   |               ------- type must be known at this point
   |
help: consider giving this closure parameter an explicit type
   |
9  |     let mut x = |_: /* Type */| {
   |                   ++++++++++++

warning: unused import: `std::ops::Generator`
 --> issue-74883-unused-paren-baren-yield.rs:5:5
  |
5 | use std::ops::Generator;
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default
Version
rustc 1.69.0-nightly (44cfafe2f 2023-03-03)
binary: rustc
commit-hash: 44cfafe2fafe816395d3acc434663a45d5178c41
commit-date: 2023-03-03
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
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 redundant_pattern_matching lint and compile the provided generator reproducer with the shown nightly Rust version. Check why the lint suggests is_some() for (yield) even though that suggestion causes a type inference error; done means the lint no longer emits an invalid suggestion for this case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.