Skip to content

generators: redundant_pattern_matching triggers on yield  #10451

@matthiaskrgr

Description

@matthiaskrgr

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

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-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions