diff --git a/compiler/rustc_lint/src/methods.rs b/compiler/rustc_lint/src/methods.rs index 1e67e9dd7deb..f1e44fbe2a25 100644 --- a/compiler/rustc_lint/src/methods.rs +++ b/compiler/rustc_lint/src/methods.rs @@ -10,7 +10,7 @@ use rustc_span::{ declare_lint! { pub TEMPORARY_CSTRING_AS_PTR, - Deny, + Warn, "detects getting the inner pointer of a temporary `CString`" } diff --git a/library/std/src/ffi/c_str.rs b/library/std/src/ffi/c_str.rs index 53966396e110..a9ab0d1d83bc 100644 --- a/library/std/src/ffi/c_str.rs +++ b/library/std/src/ffi/c_str.rs @@ -1265,7 +1265,7 @@ impl CStr { /// behavior when `ptr` is used inside the `unsafe` block: /// /// ```no_run - /// # #![allow(unused_must_use)] #![allow(temporary_cstring_as_ptr)] + /// # #![allow(unused_must_use, temporary_cstring_as_ptr)] /// use std::ffi::CString; /// /// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr(); diff --git a/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs b/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs index 54f57e1dd771..73a9d6677c75 100644 --- a/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs +++ b/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs @@ -1,4 +1,5 @@ // ignore-tidy-linelength +#![deny(temporary_cstring_as_ptr)] use std::ffi::CString; diff --git a/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr index 9ceb71ba8d9e..ee06bfc2fadb 100644 --- a/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr +++ b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr @@ -1,12 +1,16 @@ error: getting the inner pointer of a temporary `CString` - --> $DIR/lint-temporary-cstring-as-ptr.rs:6:48 + --> $DIR/lint-temporary-cstring-as-ptr.rs:7:48 | LL | let s = CString::new("some text").unwrap().as_ptr(); | ---------------------------------- ^^^^^^ this pointer will be invalid | | | this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime | - = note: `#[deny(temporary_cstring_as_ptr)]` on by default +note: the lint level is defined here + --> $DIR/lint-temporary-cstring-as-ptr.rs:2:9 + | +LL | #![deny(temporary_cstring_as_ptr)] + | ^^^^^^^^^^^^^^^^^^^^^^^^ = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` is deallocated because nothing is referencing it as far as the type system is concerned error: aborting due to previous error