-
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
static_mut_refs lint fires on assert_eq
#131443
Comments
Kinda. It has everything to do with the expansions, but it is dictated by So this code: static mut S: i32 = 0;
fn assert() {
unsafe { assert!(S == 0) }
}
fn assert_eq() {
unsafe { assert_eq!(S, 0) }
}
fn main() {
assert();
assert_eq();
} expands to #![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
static mut S: i32 = 0;
fn assert() {
unsafe {
if !(S == 0) {
::core::panicking::panic("assertion failed: S == 0")
}
}
}
fn assert_eq() {
unsafe {
match (&S, &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(
kind,
&*left_val,
&*right_val,
::core::option::Option::None,
);
}
}
}
}
}
fn main() {
assert();
assert_eq();
} according to We can probably just slap |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Seems reasonable to me. I guess the one potential problem is a static mut of custom type where then this will invoke |
True, I've opened #131446 for it. |
This only works for |
Code
Current output
Desired output
(none)
Rationale and extra context
The two macro invocations do the same thing, but in the first case we recognize that the reference created for
==
is a "short-lived reference" and suppress the warning. In the second case, we do show a warning -- I suspect it has to do with the formatting? Indeedprintln!("{}", S);
also triggers a warning. Maybe formatting macros where we know that the reference does not outlive the macro could be recognized by the lint?An alternative would be to suggest using
{ S }
in these cases which will also avoid the warning.Other cases
No response
Rust Version
current nightly
Anything else?
No response
The text was updated successfully, but these errors were encountered: