Quoting the docs of the lint:
What it does: This lint checks for getting the inner pointer of a temporary CString.
Why is this bad? The inner pointer of a CString is only valid as long as the CString is alive.
@akaDemik someone found in a recent discussion that there are hundreds of .unwrap().as_ptr() calls to be found on github (not all of them UB, but many).
I believe this amount of accidental UB is reason enough to add a "new" lint to rustc that prevents misusing this specific easy-to-misuse method.
We are different aka-demik's ;)
New lints pretty much require RFCs these days.
Or we could just deprecate the method (with a message warning of the potential for misuse) and provide fn with_ptr<F: FnOnce(*const c_char)>(&self, f: F) { f(self.as_ptr()); } instead. Example.
rust-lang/rfcs#1642 submitted.
Let's continue further discussion on the rfc
Most helpful comment
Or we could just deprecate the method (with a message warning of the potential for misuse) and provide
fn with_ptr<F: FnOnce(*const c_char)>(&self, f: F) { f(self.as_ptr()); }instead. Example.