From 7d0afaec5ad5b1c4e80d359bd070edba9f60f396 Mon Sep 17 00:00:00 2001 From: Roxane Date: Thu, 12 Aug 2021 21:29:04 -0400 Subject: Add missing multi variant cases --- compiler/rustc_typeck/src/expr_use_visitor.rs | 3 ++ .../closures/2229_closure_analysis/issue-87988.rs | 19 ++++++++++ .../2229_closure_analysis/match-multi-variant.rs | 44 ++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/test/ui/closures/2229_closure_analysis/issue-87988.rs create mode 100644 src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index 1d7852d964c..ac26380582a 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -265,6 +265,9 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { if def.variants.len() > 1 { needs_to_be_read = true; } + } else { + // If it is not ty::Adt, then it is a MultiVariant + needs_to_be_read = true; } } PatKind::Lit(_) | PatKind::Range(..) => { diff --git a/src/test/ui/closures/2229_closure_analysis/issue-87988.rs b/src/test/ui/closures/2229_closure_analysis/issue-87988.rs new file mode 100644 index 00000000000..27e7fabf11a --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/issue-87988.rs @@ -0,0 +1,19 @@ +// run-pass +// edition:2021 + +const LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: i32 = 0x01; +const LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: i32 = 0x02; + +pub fn hotplug_callback(event: i32) { + let _ = || { + match event { + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED => (), + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT => (), + _ => (), + }; + }; +} + +fn main() { + hotplug_callback(1); +} diff --git a/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs b/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs new file mode 100644 index 00000000000..78ae5aff0ab --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs @@ -0,0 +1,44 @@ +// run-pass +// edition:2021 + +const PATTERN_REF: &str = "Hello World"; +const NUMBER: i32 = 30; +const NUMBER_POINTER: *const i32 = &NUMBER; + +pub fn multi_variant_ref(event: &str) { + let _ = || { + match event { + PATTERN_REF => (), + _ => (), + }; + }; +} + +pub fn multi_variant_str(event: String) { + let _ = || { + match event.as_str() { + "hello" => (), + _ => (), + }; + }; +} + +pub fn multi_variant_raw_ptr(event: *const i32) { + let _ = || { + match event { + NUMBER_POINTER => (), + _ => (), + }; + }; +} + +pub fn multi_variant_char(event: char) { + let _ = || { + match event { + 'a' => (), + _ => (), + }; + }; +} + +fn main() {} -- cgit 1.4.1-3-g733a5 From 2e61659bd1f3998520abc77f5459c529083a7a3d Mon Sep 17 00:00:00 2001 From: Roxane Date: Fri, 13 Aug 2021 20:48:59 -0400 Subject: Update comment and fix fmt --- compiler/rustc_typeck/src/expr_use_visitor.rs | 2 +- .../2229_closure_analysis/match-edge-cases.rs | 44 ++++++++++++++++++++++ .../2229_closure_analysis/match-multi-variant.rs | 44 ---------------------- 3 files changed, 45 insertions(+), 45 deletions(-) create mode 100644 src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs delete mode 100644 src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index ac26380582a..1bdad508daa 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -266,7 +266,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { needs_to_be_read = true; } } else { - // If it is not ty::Adt, then it is a MultiVariant + // If it is not ty::Adt, then it should be read needs_to_be_read = true; } } diff --git a/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs b/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs new file mode 100644 index 00000000000..914ebbe26a5 --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs @@ -0,0 +1,44 @@ +// run-pass +// edition:2021 + +const PATTERN_REF: &str = "Hello World"; +const NUMBER: i32 = 30; +const NUMBER_POINTER: *const i32 = &NUMBER; + +pub fn edge_case_ref(event: &str) { + let _ = || { + match event { + PATTERN_REF => (), + _ => (), + }; + }; +} + +pub fn edge_case_str(event: String) { + let _ = || { + match event.as_str() { + "hello" => (), + _ => (), + }; + }; +} + +pub fn edge_case_raw_ptr(event: *const i32) { + let _ = || { + match event { + NUMBER_POINTER => (), + _ => (), + }; + }; +} + +pub fn edge_case_char(event: char) { + let _ = || { + match event { + 'a' => (), + _ => (), + }; + }; +} + +fn main() {} diff --git a/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs b/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs deleted file mode 100644 index 78ae5aff0ab..00000000000 --- a/src/test/ui/closures/2229_closure_analysis/match-multi-variant.rs +++ /dev/null @@ -1,44 +0,0 @@ -// run-pass -// edition:2021 - -const PATTERN_REF: &str = "Hello World"; -const NUMBER: i32 = 30; -const NUMBER_POINTER: *const i32 = &NUMBER; - -pub fn multi_variant_ref(event: &str) { - let _ = || { - match event { - PATTERN_REF => (), - _ => (), - }; - }; -} - -pub fn multi_variant_str(event: String) { - let _ = || { - match event.as_str() { - "hello" => (), - _ => (), - }; - }; -} - -pub fn multi_variant_raw_ptr(event: *const i32) { - let _ = || { - match event { - NUMBER_POINTER => (), - _ => (), - }; - }; -} - -pub fn multi_variant_char(event: char) { - let _ = || { - match event { - 'a' => (), - _ => (), - }; - }; -} - -fn main() {} -- cgit 1.4.1-3-g733a5