Files
rust/tests/ui/manual_c_str_literals.edition2021.fixed

78 lines
2.0 KiB
Rust
Raw Normal View History

//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
2025-02-11 17:57:08 +01:00
//@[edition2018] check-pass
2023-12-03 23:41:01 +01:00
#![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)]
use std::ffi::CStr;
macro_rules! cstr {
($s:literal) => {
CStr::from_bytes_with_nul(concat!($s, "\0").as_bytes()).unwrap()
};
}
macro_rules! macro_returns_c_str {
() => {
CStr::from_bytes_with_nul(b"foo\0").unwrap();
};
}
macro_rules! macro_returns_byte_string {
() => {
b"foo\0"
};
}
#[clippy::msrv = "1.76.0"]
fn pre_stabilization() {
CStr::from_bytes_with_nul(b"foo\0");
}
#[clippy::msrv = "1.77.0"]
fn post_stabilization() {
c"foo";
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
}
fn main() {
c"foo";
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
c"foo";
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
c"foo";
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
c"foo\\0sdsd";
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
CStr::from_bytes_with_nul(br"foo\\0sdsd\0").unwrap();
CStr::from_bytes_with_nul(br"foo\x00").unwrap();
CStr::from_bytes_with_nul(br##"foo#a\0"##).unwrap();
unsafe { c"foo" };
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
unsafe { c"foo" };
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _: *const _ = c"foo".as_ptr();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _: *const _ = c"foo".as_ptr();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _: *const _ = "foo".as_ptr(); // not a C-string
let _: *const _ = "".as_ptr();
let _: *const _ = c"foo".as_ptr().cast::<i8>();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _ = "电脑".as_ptr();
let _ = "电脑\\".as_ptr();
let _ = c"电脑\\".as_ptr();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _ = c"电脑".as_ptr();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
let _ = c"电脑".as_ptr();
2025-02-11 17:57:08 +01:00
//~[edition2021]^ manual_c_str_literals
2023-12-03 23:41:01 +01:00
// Macro cases, don't lint:
cstr!("foo");
macro_returns_c_str!();
CStr::from_bytes_with_nul(macro_returns_byte_string!()).unwrap();
}