Closed
Description
I propose we add a lint that detects code like
*(0 as *const i32)
*ptr::null()
*ptr::null_mut()
Some people seem to think that this is okay in contexts like &*(0 as *const i32)
or addr_of!(*(0 as *const i32))
, but that is not the case -- *
on a NULL pointer is UB even as a place expression.
To implement this, you can use the invalid_value
lint as a template:
rust/compiler/rustc_lint/src/builtin.rs
Line 2360 in 9d8f833
check_expr
should check for Unary
expressions with a Deref
operator, and then check the operand to be either a cast of 0
to a pointer type, or a call to one of the null
methods. To detect the methods, make them "diagnostic items"; the invalid_value
lint does that e.g. for mem::zeroed
so it can again serve as a template here.
Metadata
Metadata
Assignees
Labels
Area: Lints (warnings about flaws in source code) such as unused_mut.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the language team, which will review and decide on the PR/issue.