Skip to content

unnecessary parentheses warning triggers for necessary parentheses around yield expression #74883

Closed
@Nemo157

Description

@Nemo157

I tried this code:

#![feature(generator_trait)]
#![feature(generators)]

use std::ops::Generator;

fn main() {
    let mut x = |_| {
        while let Some(val) = (yield) {
            dbg!(val);
        }
    };
    dbg!(std::pin::Pin::new(&mut x).resume(Some(5)));
    dbg!(std::pin::Pin::new(&mut x).resume(Some(6)));
    dbg!(std::pin::Pin::new(&mut x).resume(None));
}

I expected to see this happen: no warning

Instead, this happened:

warning: unnecessary parentheses around `let` scrutinee expression
 --> src/main.rs:8:31
  |
8 |         while let Some(val) = (yield) {
  |                               ^^^^^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

if I follow the suggestion, then compilation fails:

error: expected `{`, found `}`
  --> src/main.rs:11:5
   |
11 |     };
   |     ^ expected `{`

(yield takes an optional expression, not wrapping it in parentheses means the while let block becomes that expression, and the while let is then missing a block).

Meta

  • clippy 0.0.212 (2020-07-27 76e8333)
  • `rustc 1.47.0-nightly (2020-07-27 76e8333)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-coroutines`#![feature(coroutines)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions