diff options
| author | Pietro Albini <pietro@pietroalbini.org> | 2018-12-05 23:54:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-05 23:54:39 +0100 |
| commit | f8ee5ab8035ffab91a5b57e2f0d4822e4e7a64b9 (patch) | |
| tree | 6a66e7aa7aa28c70b50301a6a75d9335433470f8 /src/libcore | |
| parent | 0fb90f372ee1fd3645e77159e6bcf48f215f306a (diff) | |
| parent | a96430799987541b02aa9605f091dfc8368fa668 (diff) | |
| download | rust-f8ee5ab8035ffab91a5b57e2f0d4822e4e7a64b9.tar.gz rust-f8ee5ab8035ffab91a5b57e2f0d4822e4e7a64b9.zip | |
Rollup merge of #56538 - xfix:patch-13, r=bluss
Use inner iterator may_have_side_effect for Cloned Previous implementation wasn't correct, as an inner iterator could have had side effects. Noticed by @bluss in #56534.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter/mod.rs | 4 | ||||
| -rw-r--r-- | src/libcore/tests/iter.rs | 17 |
2 files changed, 20 insertions, 1 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 62e1f9fcb64..de7ab8843da 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -602,7 +602,9 @@ unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I> } #[inline] - fn may_have_side_effect() -> bool { false } + fn may_have_side_effect() -> bool { + I::may_have_side_effect() + } } #[unstable(feature = "trusted_len", issue = "37572")] diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index 495483db555..19be1a07c5b 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -1250,6 +1250,23 @@ fn test_cloned() { } #[test] +fn test_cloned_side_effects() { + let mut count = 0; + { + let iter = [1, 2, 3] + .iter() + .map(|x| { + count += 1; + x + }) + .cloned() + .zip(&[1]); + for _ in iter {} + } + assert_eq!(count, 2); +} + +#[test] fn test_double_ended_map() { let xs = [1, 2, 3, 4, 5, 6]; let mut it = xs.iter().map(|&x| x * -1); |
