From 87010206adc0123277d7e355893e83551a28814f Mon Sep 17 00:00:00 2001 From: Roxane Date: Sun, 26 Sep 2021 14:48:56 -0400 Subject: [PATCH 1/2] 2229: Consume IfLet expr --- compiler/rustc_typeck/src/expr_use_visitor.rs | 2 ++ .../2229_closure_analysis/issue-88118-2.rs | 21 +++++++++++++++++++ .../issue-88118-2.stderr | 19 +++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs create mode 100644 src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index ba70006fe96b3..b5c4d6ac26167 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -619,6 +619,8 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { if let Some(hir::Guard::If(ref e)) = arm.guard { self.consume_expr(e) + } else if let Some(hir::Guard::IfLet(_, ref e)) = arm.guard { + self.consume_expr(e) } self.consume_expr(&arm.body); diff --git a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs new file mode 100644 index 0000000000000..a4cbbc1d25a8e --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs @@ -0,0 +1,21 @@ +// edition:2021 +#![feature(if_let_guard)] + +fn print_error_count(registry: &Registry) { + |x: &Registry| { + match &x { + Registry if let _ = registry.try_find_description() => { } + //~^ WARNING: irrefutable `if let` guard pattern + _ => {} + } + }; +} + +struct Registry; +impl Registry { + pub fn try_find_description(&self) { + unimplemented!() + } +} + +fn main() {} diff --git a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr new file mode 100644 index 0000000000000..d51f3f0eb7b7d --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr @@ -0,0 +1,19 @@ +warning: irrefutable `if let` guard pattern + --> $DIR/issue-88118-2.rs:7:29 + | +LL | Registry if let _ = registry.try_find_description() => { } + | ^ + | + = note: `#[warn(irrefutable_let_patterns)]` on by default + = note: this pattern will always match, so the guard is useless + = help: consider removing the guard and adding a `let` inside the match arm + +error[E0507]: cannot move out of `*registry` which is behind a shared reference + --> $DIR/issue-88118-2.rs:7:33 + | +LL | Registry if let _ = registry.try_find_description() => { } + | ^^^^^^^^ move occurs because `*registry` has type `Registry`, which does not implement the `Copy` trait + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0507`. From d0e2b607de9b3b4e7e6495206a21847201248144 Mon Sep 17 00:00:00 2001 From: Roxane Date: Sun, 26 Sep 2021 16:17:08 -0400 Subject: [PATCH 2/2] Fix test --- .../closures/2229_closure_analysis/issue-88118-2.rs | 3 +++ .../2229_closure_analysis/issue-88118-2.stderr | 11 ++--------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs index a4cbbc1d25a8e..0cfb1a55bf27f 100644 --- a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs +++ b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.rs @@ -1,5 +1,8 @@ // edition:2021 +// run-pass #![feature(if_let_guard)] +#[allow(unused_must_use)] +#[allow(dead_code)] fn print_error_count(registry: &Registry) { |x: &Registry| { diff --git a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr index d51f3f0eb7b7d..15689023d818a 100644 --- a/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr +++ b/src/test/ui/closures/2229_closure_analysis/issue-88118-2.stderr @@ -1,5 +1,5 @@ warning: irrefutable `if let` guard pattern - --> $DIR/issue-88118-2.rs:7:29 + --> $DIR/issue-88118-2.rs:10:29 | LL | Registry if let _ = registry.try_find_description() => { } | ^ @@ -8,12 +8,5 @@ LL | Registry if let _ = registry.try_find_description() => { } = note: this pattern will always match, so the guard is useless = help: consider removing the guard and adding a `let` inside the match arm -error[E0507]: cannot move out of `*registry` which is behind a shared reference - --> $DIR/issue-88118-2.rs:7:33 - | -LL | Registry if let _ = registry.try_find_description() => { } - | ^^^^^^^^ move occurs because `*registry` has type `Registry`, which does not implement the `Copy` trait - -error: aborting due to previous error; 1 warning emitted +warning: 1 warning emitted -For more information about this error, try `rustc --explain E0507`.