The lint is warning because you are taking a mutable reference to a const item in order to call a method that requires &mut self. That mutable reference is to a fresh temporary copy of the const value (the NonNull), not to the original const item. In your specific pattern, that borrowed temporary is only used to manufacture a &'_ mut T pointing at 0x1234, so the const itself is not being mutated. The real risk is the usual unsafe aliasing/validity obligations of turning a raw address into a mutable reference.
- The builtin lint CONST_ITEM_MUTATION warns whenever you mutate-through or take &mut of a const item, because const items are inlined: each use creates a temporary copy. The notes in the lint text are exactly what you see (“each usage of a const item creates a new temporary … the mutable reference will refer to this temporary, not the original const item”). See the lint docs in the compiler source:
- Definition and explanation: compiler/rustc_lint_defs/src/builtin.rs (CONST_ITEM_MUTATION)
- MIR pass that emits it: compiler/rustc_mir_transform/src/check_const_item_mutation.rs (it matches taking a mutable borrow of a const and, if used as a method receiver, shows the method span)
- NonNull::as_mut takes &mut self and produces &'a mut T. The &mut self you form is to a temporary NonNull copy (as the lint says), which is fine. You are not mutating the const item; you’re only using that temporary to create a reference to the pointee.
- The unsafety is elsewhere: by calling as_mut you promise that 0x1234 is properly aligned, dereferenceable, and uniquely borrowed for the duration of the &'_ mut T (no aliases). That’s the part you must uphold.