Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clippy_lints/src/methods/manual_c_str_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_ast::{LitKind, StrStyle};
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;
Expand All @@ -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()),
Expand Down Expand Up @@ -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) {
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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
#![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)]

Expand Down
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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::<i8>();
| ^^^^^^^^ 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"电脑"`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/manual_c_str_literals.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
#![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)]

Expand Down