Skip to content

Commit 159a1d2

Browse files
GuillaumeGomezgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#133701 - kornelski:c-str, r=workingjubilee
Use c"lit" for CStrings without unwrap I've reviewed uses of `CString::new("lit")`. Some could be changed to `c"lit"`. Some could be changed to `c"lit".to_owned()`, avoiding an `unwrap()`. Many `CString` documentation examples could be simplified. I deliberately haven't changed all the examples to use the exact same expression, so that they can demonstrate many ways of creating `CString`s. I've left UI tests mostly unchanged, because `c""` requires edition 2021, but most UI tests use 2015, and I didn't want to accidentally change what the tests are testing.
2 parents 012faa4 + 73ec1e9 commit 159a1d2

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

alloc/src/ffi/c_str.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl CString {
384384
/// fn some_extern_function(s: *mut c_char);
385385
/// }
386386
///
387-
/// let c_string = CString::new("Hello!").expect("CString::new failed");
387+
/// let c_string = CString::from(c"Hello!");
388388
/// let raw = c_string.into_raw();
389389
/// unsafe {
390390
/// some_extern_function(raw);
@@ -429,7 +429,7 @@ impl CString {
429429
/// ```
430430
/// use std::ffi::CString;
431431
///
432-
/// let c_string = CString::new("foo").expect("CString::new failed");
432+
/// let c_string = CString::from(c"foo");
433433
///
434434
/// let ptr = c_string.into_raw();
435435
///
@@ -487,7 +487,7 @@ impl CString {
487487
/// ```
488488
/// use std::ffi::CString;
489489
///
490-
/// let c_string = CString::new("foo").expect("CString::new failed");
490+
/// let c_string = CString::from(c"foo");
491491
/// let bytes = c_string.into_bytes();
492492
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
493493
/// ```
@@ -508,7 +508,7 @@ impl CString {
508508
/// ```
509509
/// use std::ffi::CString;
510510
///
511-
/// let c_string = CString::new("foo").expect("CString::new failed");
511+
/// let c_string = CString::from(c"foo");
512512
/// let bytes = c_string.into_bytes_with_nul();
513513
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
514514
/// ```
@@ -530,7 +530,7 @@ impl CString {
530530
/// ```
531531
/// use std::ffi::CString;
532532
///
533-
/// let c_string = CString::new("foo").expect("CString::new failed");
533+
/// let c_string = CString::from(c"foo");
534534
/// let bytes = c_string.as_bytes();
535535
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
536536
/// ```
@@ -550,7 +550,7 @@ impl CString {
550550
/// ```
551551
/// use std::ffi::CString;
552552
///
553-
/// let c_string = CString::new("foo").expect("CString::new failed");
553+
/// let c_string = CString::from(c"foo");
554554
/// let bytes = c_string.as_bytes_with_nul();
555555
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
556556
/// ```
@@ -568,7 +568,7 @@ impl CString {
568568
/// ```
569569
/// use std::ffi::{CString, CStr};
570570
///
571-
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
571+
/// let c_string = CString::from(c"foo");
572572
/// let cstr = c_string.as_c_str();
573573
/// assert_eq!(cstr,
574574
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
@@ -586,12 +586,9 @@ impl CString {
586586
/// # Examples
587587
///
588588
/// ```
589-
/// use std::ffi::{CString, CStr};
590-
///
591-
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
589+
/// let c_string = c"foo".to_owned();
592590
/// let boxed = c_string.into_boxed_c_str();
593-
/// assert_eq!(&*boxed,
594-
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
591+
/// assert_eq!(boxed.to_bytes_with_nul(), b"foo\0");
595592
/// ```
596593
#[must_use = "`self` will be dropped if the result is not used"]
597594
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
@@ -658,7 +655,7 @@ impl CString {
658655
/// assert_eq!(
659656
/// CString::from_vec_with_nul(b"abc\0".to_vec())
660657
/// .expect("CString::from_vec_with_nul failed"),
661-
/// CString::new(b"abc".to_vec()).expect("CString::new failed")
658+
/// c"abc".to_owned()
662659
/// );
663660
/// ```
664661
///
@@ -1168,11 +1165,12 @@ impl CStr {
11681165
/// # Examples
11691166
///
11701167
/// ```
1171-
/// use std::ffi::CString;
1168+
/// use std::ffi::{CStr, CString};
11721169
///
1173-
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
1174-
/// let boxed = c_string.into_boxed_c_str();
1175-
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
1170+
/// let boxed: Box<CStr> = Box::from(c"foo");
1171+
/// let c_string: CString = c"foo".to_owned();
1172+
///
1173+
/// assert_eq!(boxed.into_c_string(), c_string);
11761174
/// ```
11771175
#[rustc_allow_incoherent_impl]
11781176
#[must_use = "`self` will be dropped if the result is not used"]

alloc/src/ffi/c_str/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn boxed_default() {
159159

160160
#[test]
161161
fn test_c_str_clone_into() {
162-
let mut c_string = CString::new("lorem").unwrap();
162+
let mut c_string = c"lorem".to_owned();
163163
let c_ptr = c_string.as_ptr();
164164
let c_str = CStr::from_bytes_with_nul(b"ipsum\0").unwrap();
165165
c_str.clone_into(&mut c_string);

alloc/src/rc/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ fn test_unsized() {
349349
#[test]
350350
fn test_maybe_thin_unsized() {
351351
// If/when custom thin DSTs exist, this test should be updated to use one
352-
use std::ffi::{CStr, CString};
352+
use std::ffi::CStr;
353353

354-
let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
354+
let x: Rc<CStr> = Rc::from(c"swordfish");
355355
assert_eq!(format!("{x:?}"), "\"swordfish\"");
356356
let y: Weak<CStr> = Rc::downgrade(&x);
357357
drop(x);

alloc/src/sync/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ fn test_unsized() {
412412
#[test]
413413
fn test_maybe_thin_unsized() {
414414
// If/when custom thin DSTs exist, this test should be updated to use one
415-
use std::ffi::{CStr, CString};
415+
use std::ffi::CStr;
416416

417-
let x: Arc<CStr> = Arc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
417+
let x: Arc<CStr> = Arc::from(c"swordfish");
418418
assert_eq!(format!("{x:?}"), "\"swordfish\"");
419419
let y: Weak<CStr> = Arc::downgrade(&x);
420420
drop(x);

std/src/sys/pal/unix/process/process_common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Command {
393393
fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
394394
CString::new(s.as_bytes()).unwrap_or_else(|_e| {
395395
*saw_nul = true;
396-
CString::new("<string-with-nul>").unwrap()
396+
c"<string-with-nul>".to_owned()
397397
})
398398
}
399399

0 commit comments

Comments
 (0)