diff --git a/library/core/src/iter/adapters/copied.rs b/library/core/src/iter/adapters/copied.rs index 62d3afb81603d..8840e48b3b4f0 100644 --- a/library/core/src/iter/adapters/copied.rs +++ b/library/core/src/iter/adapters/copied.rs @@ -43,6 +43,7 @@ where { type Item = T; + #[inline] fn next(&mut self) -> Option { self.it.next().copied() } diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 5d5e95590344a..e5e6799cae9a5 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1794,6 +1794,7 @@ impl Option<&T> { /// let copied = opt_x.copied(); /// assert_eq!(copied, Some(12)); /// ``` + #[inline] #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "copied", since = "1.35.0")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] diff --git a/tests/codegen/issue-107681-unwrap_unchecked-optimized.rs b/tests/codegen/issue-107681-unwrap_unchecked-optimized.rs new file mode 100644 index 0000000000000..ff32860454fb3 --- /dev/null +++ b/tests/codegen/issue-107681-unwrap_unchecked-optimized.rs @@ -0,0 +1,13 @@ +// compile-flags: -C opt-level=3 + +#![crate_type = "lib"] + +extern crate core; +use core::{iter::Copied, slice::Iter}; + +// Make sure that the use of unwrap_unchecked is optimized accordingly. +// i.e., there are no branch jumps. +pub unsafe fn unwrap_unchecked_optimized(x: &mut Copied>) -> u32 { + // CHECK-NOT: br + x.next().unwrap_unchecked() +}