-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect warning: Unnecessary unsafe block #49112
Comments
Ah, nevermind, it occurs only when the macro is called in the context of another unsafe block, like: arr![arr![None; 128]; 16] Still, it should detect this, and not display the warning. |
This is not an enhancement, this is a regression. As mentioned by the OP, this warning didn't used to occur. |
Are there any simple workarounds for this warning? |
So the workarounds is to turn this code: unsafe { ... } // expression into this: {
#[allow(unused_unsafe)]
let tmp = unsafe { ... };
tmp
} |
same issue, not sure how to fix it in this case: .map(|_| {
if unsafe { js_sys::Math::random() < 0.5 } {
Cell::Alive
} else {
Cell::Dead
}
})
.collect(); |
Is there any actual issue being reported here? If yes, what’s the code to reproduce it and what’s the actual issue? |
I thought I'd help out by retyping (and modernizing) the code and error from the original screenshot: macro_rules! arr {
($item:expr; $n:expr) => {
{
let mut array_uninit: [_; $n] =
[::core::mem::MaybeUninit::uninit(); $n];
for x in &mut array_uninit {
unsafe { ::core::ptr::write(x.as_mut_ptr(), $item) };
}
array_uninit.map(|mu| unsafe { mu.assume_init() })
}
}
}
fn main() {
dbg!(
arr![arr![5; 3]; 2]
);
} This produces the following warnings (on stable 1.60.0): Warnings
That is, the stated problem still reproduces. However, thinking about what's going on here, this code is hazardous and should be improved: the macro is evaluating let item = $item;
unsafe { ::core::ptr::write(x.as_mut_ptr(), item) }; So, not-a-bug? Well, even with this fix, the |
I think that’s essentially “working as intended”, for now. Something like macro_rules! arr {
($item:expr; $n:expr) => {
{
let mut array_uninit: [_; $n] =
[::core::mem::MaybeUninit::uninit(); $n];
for x in &mut array_uninit {
let item = $item;
#[allow(unused_unsafe)]
unsafe { ::core::ptr::write(x.as_mut_ptr(), item) };
}
#[allow(unused_unsafe)]
array_uninit.map(|mu| unsafe { mu.assume_init() })
}
}
} should work fine. (Ignoring the soundness problem of possible And a larger |
1.Use camel case instead of underscores for type names 2.for functions and variables never used comment out 3.for field never be read or enum never be constructed add underscore 4.there is another warning which is 'unnecessary `unsafe` block' seems like a Incorrect warning: rust-lang/rust#49112
1.Use camel case instead of underscores for type names 2.for functions and variables never used comment out 3.for field never be read or enum never be constructed add underscore 4.there is another warning which is 'unnecessary `unsafe` block' seems like a Incorrect warning: rust-lang/rust#49112
1.Use camel case instead of underscores for type names 2.for functions and variables never used comment out 3.for field never be read or enum never be constructed add underscore 4.there is another warning which is 'unnecessary `unsafe` block' seems like a Incorrect warning: rust-lang/rust#49112
1.Use camel case instead of underscores for type names 2.for functions and variables never used comment out 3.for field never be read or enum never be constructed add underscore 4.there is another warning which is 'unnecessary `unsafe` block' seems like a Incorrect warning: rust-lang/rust#49112
This warning is incorrect. It says for both unsafe blocks that they are unnecessary, and when either of the unsafe keywords is removed, it gives an error (of course).
It wrongly says that the first block is nested under the second unsafe block.
Before (nightly at end of January) this warning didn't occur.
The text was updated successfully, but these errors were encountered: