Skip to content

Commit 456c9da

Browse files
committed
vec_deque alloctests: remove static mut
1 parent ecdf220 commit 456c9da

File tree

1 file changed

+18
-101
lines changed

1 file changed

+18
-101
lines changed

library/alloctests/tests/vec_deque.rs

Lines changed: 18 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2-
#![allow(static_mut_refs)]
3-
41
use core::num::NonZero;
52
use std::assert_matches::assert_matches;
63
use std::collections::TryReserveErrorKind::*;
@@ -14,6 +11,7 @@ use Taggy::*;
1411
use Taggypar::*;
1512

1613
use crate::hash;
14+
use crate::testing::macros::struct_with_counted_drop;
1715

1816
#[test]
1917
fn test_simple() {
@@ -719,15 +717,7 @@ fn test_show() {
719717

720718
#[test]
721719
fn test_drop() {
722-
static mut DROPS: i32 = 0;
723-
struct Elem;
724-
impl Drop for Elem {
725-
fn drop(&mut self) {
726-
unsafe {
727-
DROPS += 1;
728-
}
729-
}
730-
}
720+
struct_with_counted_drop!(Elem, DROPS);
731721

732722
let mut ring = VecDeque::new();
733723
ring.push_back(Elem);
@@ -736,20 +726,12 @@ fn test_drop() {
736726
ring.push_front(Elem);
737727
drop(ring);
738728

739-
assert_eq!(unsafe { DROPS }, 4);
729+
assert_eq!(DROPS.get(), 4);
740730
}
741731

742732
#[test]
743733
fn test_drop_with_pop() {
744-
static mut DROPS: i32 = 0;
745-
struct Elem;
746-
impl Drop for Elem {
747-
fn drop(&mut self) {
748-
unsafe {
749-
DROPS += 1;
750-
}
751-
}
752-
}
734+
struct_with_counted_drop!(Elem, DROPS);
753735

754736
let mut ring = VecDeque::new();
755737
ring.push_back(Elem);
@@ -759,54 +741,32 @@ fn test_drop_with_pop() {
759741

760742
drop(ring.pop_back());
761743
drop(ring.pop_front());
762-
assert_eq!(unsafe { DROPS }, 2);
744+
assert_eq!(DROPS.get(), 2);
763745

764746
drop(ring);
765-
assert_eq!(unsafe { DROPS }, 4);
747+
assert_eq!(DROPS.get(), 4);
766748
}
767749

768750
#[test]
769751
fn test_drop_clear() {
770-
static mut DROPS: i32 = 0;
771-
struct Elem;
772-
impl Drop for Elem {
773-
fn drop(&mut self) {
774-
unsafe {
775-
DROPS += 1;
776-
}
777-
}
778-
}
752+
struct_with_counted_drop!(Elem, DROPS);
779753

780754
let mut ring = VecDeque::new();
781755
ring.push_back(Elem);
782756
ring.push_front(Elem);
783757
ring.push_back(Elem);
784758
ring.push_front(Elem);
785759
ring.clear();
786-
assert_eq!(unsafe { DROPS }, 4);
760+
assert_eq!(DROPS.get(), 4);
787761

788762
drop(ring);
789-
assert_eq!(unsafe { DROPS }, 4);
763+
assert_eq!(DROPS.get(), 4);
790764
}
791765

792766
#[test]
793767
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
794768
fn test_drop_panic() {
795-
static mut DROPS: i32 = 0;
796-
797-
struct D(bool);
798-
799-
impl Drop for D {
800-
fn drop(&mut self) {
801-
unsafe {
802-
DROPS += 1;
803-
}
804-
805-
if self.0 {
806-
panic!("panic in `drop`");
807-
}
808-
}
809-
}
769+
struct_with_counted_drop!(D(bool), DROPS => |this: &D| if this.0 { panic!("panic in `drop`"); } );
810770

811771
let mut q = VecDeque::new();
812772
q.push_back(D(false));
@@ -820,7 +780,7 @@ fn test_drop_panic() {
820780

821781
catch_unwind(move || drop(q)).ok();
822782

823-
assert_eq!(unsafe { DROPS }, 8);
783+
assert_eq!(DROPS.get(), 8);
824784
}
825785

826786
#[test]
@@ -1655,21 +1615,7 @@ fn test_try_rfold_moves_iter() {
16551615
#[test]
16561616
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
16571617
fn truncate_leak() {
1658-
static mut DROPS: i32 = 0;
1659-
1660-
struct D(bool);
1661-
1662-
impl Drop for D {
1663-
fn drop(&mut self) {
1664-
unsafe {
1665-
DROPS += 1;
1666-
}
1667-
1668-
if self.0 {
1669-
panic!("panic in `drop`");
1670-
}
1671-
}
1672-
}
1618+
struct_with_counted_drop!(D(bool), DROPS => |this: &D| if this.0 { panic!("panic in `drop`"); } );
16731619

16741620
let mut q = VecDeque::new();
16751621
q.push_back(D(false));
@@ -1683,27 +1629,13 @@ fn truncate_leak() {
16831629

16841630
catch_unwind(AssertUnwindSafe(|| q.truncate(1))).ok();
16851631

1686-
assert_eq!(unsafe { DROPS }, 7);
1632+
assert_eq!(DROPS.get(), 7);
16871633
}
16881634

16891635
#[test]
16901636
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
16911637
fn truncate_front_leak() {
1692-
static mut DROPS: i32 = 0;
1693-
1694-
struct D(bool);
1695-
1696-
impl Drop for D {
1697-
fn drop(&mut self) {
1698-
unsafe {
1699-
DROPS += 1;
1700-
}
1701-
1702-
if self.0 {
1703-
panic!("panic in `drop`");
1704-
}
1705-
}
1706-
}
1638+
struct_with_counted_drop!(D(bool), DROPS => |this: &D| if this.0 { panic!("panic in `drop`"); } );
17071639

17081640
let mut q = VecDeque::new();
17091641
q.push_back(D(false));
@@ -1717,28 +1649,13 @@ fn truncate_front_leak() {
17171649

17181650
catch_unwind(AssertUnwindSafe(|| q.truncate_front(1))).ok();
17191651

1720-
assert_eq!(unsafe { DROPS }, 7);
1652+
assert_eq!(DROPS.get(), 7);
17211653
}
17221654

17231655
#[test]
17241656
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
17251657
fn test_drain_leak() {
1726-
static mut DROPS: i32 = 0;
1727-
1728-
#[derive(Debug, PartialEq)]
1729-
struct D(u32, bool);
1730-
1731-
impl Drop for D {
1732-
fn drop(&mut self) {
1733-
unsafe {
1734-
DROPS += 1;
1735-
}
1736-
1737-
if self.1 {
1738-
panic!("panic in `drop`");
1739-
}
1740-
}
1741-
}
1658+
struct_with_counted_drop!(D(u32, bool), DROPS => |this: &D| if this.1 { panic!("panic in `drop`"); } );
17421659

17431660
let mut v = VecDeque::new();
17441661
v.push_back(D(4, false));
@@ -1754,10 +1671,10 @@ fn test_drain_leak() {
17541671
}))
17551672
.ok();
17561673

1757-
assert_eq!(unsafe { DROPS }, 4);
1674+
assert_eq!(DROPS.get(), 4);
17581675
assert_eq!(v.len(), 3);
17591676
drop(v);
1760-
assert_eq!(unsafe { DROPS }, 7);
1677+
assert_eq!(DROPS.get(), 7);
17611678
}
17621679

17631680
#[test]

0 commit comments

Comments
 (0)