Closed
Description
This code gives a warning on the latest nightly:
trait OptionExt<T> {
unsafe fn unwrap_unchecked(self) -> T;
}
impl<T> OptionExt<T> for Option<T> {
unsafe fn unwrap_unchecked(self) -> T {
self.unwrap_or_else(|| std::hint::unreachable_unchecked())
}
}
fn main() {
#[allow(unstable_name_collisions)] {
println!("{}", unsafe { Some("hello").unwrap_unchecked() });
}
}
warning: a method with this name may be added to the standard library in the future
--> src/main.rs:13:47
|
13 | println!("{}", unsafe { Some("hello").unwrap_unchecked() });
| ^^^^^^^^^^^^^^^^
|
= note: `#[warn(unstable_name_collisions)]` on by default
= warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior!
= note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919>
= help: call with fully qualified syntax `OptionExt::unwrap_unchecked(...)` to keep using the current method
= help: add `#![feature(option_result_unwrap_unchecked)]` to the crate attributes to enable `Option::<T>::unwrap_unchecked`
The warning is correct: #81383 added Option::unwrap_unchecked
so this code is a future incompatibility hazard. The bug is that #[allow(unstable_name_collisions)]
does not suppress the lint, although it works at function scope.
rustc --version --verbose
:
rustc 1.51.0-nightly (d1aed50ab 2021-01-26)
binary: rustc
commit-hash: d1aed50ab81df3140977c610c5a7d00f36dc519f
commit-date: 2021-01-26
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1