static_mut_refs lint fires on `assert_eq`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
static mut S: i32 = 0;
fn main() {
unsafe {
assert!(S == 0);
assert_eq!(S, 0);
}
}
Current output
warning: creating a shared reference to mutable static is discouraged
--> src/main.rs:6:20
|
6 | assert_eq!(S, 0);
| ^ shared reference to mutable static
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
= note: `#[warn(static_mut_refs)]` on by default
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? Indeed println!("{}", 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the src/main.rs reproducer and the static_mut_refs lint behavior for assert_eq! and println!; compare them with the assert!(S == 0) case. Done means equivalent formatting macro uses no longer emit the warning, while the existing static_mut_refs diagnostic remains for cases where the reference can outlive the macro.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100