Auto merge of #13532 - y21:issue13531, r=Alexendoo

Only emit `manual_c_str_literals` in >= Edition 2021

Fixes #13531

changelog: none
This commit is contained in:
bors 2024-10-11 12:47:38 +00:00
commit 47903dbf97
4 changed files with 22 additions and 13 deletions

View File

@ -6,6 +6,7 @@
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind}; use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_span::edition::Edition::Edition2021;
use rustc_span::{Span, Symbol, sym}; use rustc_span::{Span, Symbol, sym};
use super::MANUAL_C_STR_LITERALS; use super::MANUAL_C_STR_LITERALS;
@ -25,6 +26,7 @@ pub(super) fn check_as_ptr<'tcx>(
) { ) {
if let ExprKind::Lit(lit) = receiver.kind if let ExprKind::Lit(lit) = receiver.kind
&& let LitKind::ByteStr(_, StrStyle::Cooked) | LitKind::Str(_, StrStyle::Cooked) = lit.node && 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) && let casts_removed = peel_ptr_cast_ancestors(cx, expr)
&& !get_parent_expr(cx, casts_removed).is_some_and( && !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()), |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<Symbol> {
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>], msrv: &Msrv) { 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) if let Some(fn_name) = is_c_str_function(cx, func)
&& let [arg] = args && let [arg] = args
&& cx.tcx.sess.edition() >= Edition2021
&& msrv.meets(msrvs::C_STR_LITERALS) && msrv.meets(msrvs::C_STR_LITERALS)
{ {
match fn_name.as_str() { match fn_name.as_str() {

View File

@ -1,3 +1,6 @@
//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
#![warn(clippy::manual_c_str_literals)] #![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]

View File

@ -1,5 +1,5 @@
error: calling `CStr::new` with a byte string literal 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"); LL | CStr::from_bytes_with_nul(b"foo\0");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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)]` = help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
error: calling `CStr::new` with a byte string literal 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"); LL | CStr::from_bytes_with_nul(b"foo\0");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: calling `CStr::new` with a byte string literal 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"); LL | CStr::from_bytes_with_nul(b"foo\x00");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: calling `CStr::new` with a byte string literal 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(); LL | CStr::from_bytes_with_nul(b"foo\0").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: calling `CStr::new` with a byte string literal 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(); LL | CStr::from_bytes_with_nul(b"foo\\0sdsd\0").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo\\0sdsd"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo\\0sdsd"`
error: calling `CStr::from_ptr` with a byte string literal 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()) }; LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr().cast()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: calling `CStr::from_ptr` with a byte string literal 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 _) }; LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr() as *const _) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: manually constructing a nul-terminated string 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(); LL | let _: *const _ = b"foo\0".as_ptr();
| ^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: manually constructing a nul-terminated string 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(); LL | let _: *const _ = "foo\0".as_ptr();
| ^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^ help: use a `c""` literal: `c"foo"`
error: manually constructing a nul-terminated string 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::<i8>(); LL | let _: *const _ = b"foo\0".as_ptr().cast::<i8>();
| ^^^^^^^^ help: use a `c""` literal: `c"foo"` | ^^^^^^^^ help: use a `c""` literal: `c"foo"`
error: manually constructing a nul-terminated string 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(); LL | let _ = "电脑\\\0".as_ptr();
| ^^^^^^^^^^ help: use a `c""` literal: `c"电脑\\"` | ^^^^^^^^^^ help: use a `c""` literal: `c"电脑\\"`
error: manually constructing a nul-terminated string 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(); LL | let _ = "电脑\0".as_ptr();
| ^^^^^^^^ help: use a `c""` literal: `c"电脑"` | ^^^^^^^^ help: use a `c""` literal: `c"电脑"`
error: manually constructing a nul-terminated string 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(); LL | let _ = "电脑\x00".as_ptr();
| ^^^^^^^^^^ help: use a `c""` literal: `c"电脑"` | ^^^^^^^^^^ help: use a `c""` literal: `c"电脑"`

View File

@ -1,3 +1,6 @@
//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
#![warn(clippy::manual_c_str_literals)] #![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]