diff --git a/clippy_lints/src/methods/manual_c_str_literals.rs b/clippy_lints/src/methods/manual_c_str_literals.rs index 96af9db1af7..f991e6bc900 100644 --- a/clippy_lints/src/methods/manual_c_str_literals.rs +++ b/clippy_lints/src/methods/manual_c_str_literals.rs @@ -6,6 +6,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind}; use rustc_lint::LateContext; +use rustc_span::edition::Edition::Edition2021; use rustc_span::{Span, Symbol, sym}; use super::MANUAL_C_STR_LITERALS; @@ -25,6 +26,7 @@ pub(super) fn check_as_ptr<'tcx>( ) { if let ExprKind::Lit(lit) = receiver.kind && let LitKind::ByteStr(_, StrStyle::Cooked) | LitKind::Str(_, StrStyle::Cooked) = lit.node + && cx.tcx.sess.edition() >= Edition2021 && let casts_removed = peel_ptr_cast_ancestors(cx, expr) && !get_parent_expr(cx, casts_removed).is_some_and( |parent| matches!(parent.kind, ExprKind::Call(func, _) if is_c_str_function(cx, func).is_some()), @@ -66,6 +68,7 @@ fn is_c_str_function(cx: &LateContext<'_>, func: &Expr<'_>) -> Option { pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>], msrv: &Msrv) { if let Some(fn_name) = is_c_str_function(cx, func) && let [arg] = args + && cx.tcx.sess.edition() >= Edition2021 && msrv.meets(msrvs::C_STR_LITERALS) { match fn_name.as_str() { diff --git a/tests/ui/manual_c_str_literals.fixed b/tests/ui/manual_c_str_literals.edition2021.fixed similarity index 93% rename from tests/ui/manual_c_str_literals.fixed rename to tests/ui/manual_c_str_literals.edition2021.fixed index a24d7088c88..391c63bb4b8 100644 --- a/tests/ui/manual_c_str_literals.fixed +++ b/tests/ui/manual_c_str_literals.edition2021.fixed @@ -1,3 +1,6 @@ +//@revisions: edition2018 edition2021 +//@[edition2018] edition:2018 +//@[edition2021] edition:2021 #![warn(clippy::manual_c_str_literals)] #![allow(clippy::no_effect)] diff --git a/tests/ui/manual_c_str_literals.stderr b/tests/ui/manual_c_str_literals.edition2021.stderr similarity index 84% rename from tests/ui/manual_c_str_literals.stderr rename to tests/ui/manual_c_str_literals.edition2021.stderr index 9c70bddb81c..beab29ccdda 100644 --- a/tests/ui/manual_c_str_literals.stderr +++ b/tests/ui/manual_c_str_literals.edition2021.stderr @@ -1,5 +1,5 @@ error: calling `CStr::new` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:31:5 + --> tests/ui/manual_c_str_literals.rs:34:5 | LL | CStr::from_bytes_with_nul(b"foo\0"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` @@ -8,73 +8,73 @@ LL | CStr::from_bytes_with_nul(b"foo\0"); = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]` error: calling `CStr::new` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:35:5 + --> tests/ui/manual_c_str_literals.rs:38:5 | LL | CStr::from_bytes_with_nul(b"foo\0"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` error: calling `CStr::new` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:36:5 + --> tests/ui/manual_c_str_literals.rs:39:5 | LL | CStr::from_bytes_with_nul(b"foo\x00"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` error: calling `CStr::new` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:37:5 + --> tests/ui/manual_c_str_literals.rs:40:5 | LL | CStr::from_bytes_with_nul(b"foo\0").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` error: calling `CStr::new` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:38:5 + --> tests/ui/manual_c_str_literals.rs:41:5 | LL | CStr::from_bytes_with_nul(b"foo\\0sdsd\0").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo\\0sdsd"` error: calling `CStr::from_ptr` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:43:14 + --> tests/ui/manual_c_str_literals.rs:46:14 | LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr().cast()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` error: calling `CStr::from_ptr` with a byte string literal - --> tests/ui/manual_c_str_literals.rs:44:14 + --> tests/ui/manual_c_str_literals.rs:47:14 | LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr() as *const _) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:45:23 + --> tests/ui/manual_c_str_literals.rs:48:23 | LL | let _: *const _ = b"foo\0".as_ptr(); | ^^^^^^^^ help: use a `c""` literal: `c"foo"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:46:23 + --> tests/ui/manual_c_str_literals.rs:49:23 | LL | let _: *const _ = "foo\0".as_ptr(); | ^^^^^^^ help: use a `c""` literal: `c"foo"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:49:23 + --> tests/ui/manual_c_str_literals.rs:52:23 | LL | let _: *const _ = b"foo\0".as_ptr().cast::(); | ^^^^^^^^ help: use a `c""` literal: `c"foo"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:52:13 + --> tests/ui/manual_c_str_literals.rs:55:13 | LL | let _ = "电脑\\\0".as_ptr(); | ^^^^^^^^^^ help: use a `c""` literal: `c"电脑\\"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:53:13 + --> tests/ui/manual_c_str_literals.rs:56:13 | LL | let _ = "电脑\0".as_ptr(); | ^^^^^^^^ help: use a `c""` literal: `c"电脑"` error: manually constructing a nul-terminated string - --> tests/ui/manual_c_str_literals.rs:54:13 + --> tests/ui/manual_c_str_literals.rs:57:13 | LL | let _ = "电脑\x00".as_ptr(); | ^^^^^^^^^^ help: use a `c""` literal: `c"电脑"` diff --git a/tests/ui/manual_c_str_literals.rs b/tests/ui/manual_c_str_literals.rs index 0a007786720..39b62258077 100644 --- a/tests/ui/manual_c_str_literals.rs +++ b/tests/ui/manual_c_str_literals.rs @@ -1,3 +1,6 @@ +//@revisions: edition2018 edition2021 +//@[edition2018] edition:2018 +//@[edition2021] edition:2021 #![warn(clippy::manual_c_str_literals)] #![allow(clippy::no_effect)]