Skip to content

Commit 47903db

Browse files
committed
Auto merge of #13532 - y21:issue13531, r=Alexendoo
Only emit `manual_c_str_literals` in >= Edition 2021 Fixes #13531 changelog: none
2 parents 05f3793 + 2b86ffc commit 47903db

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

clippy_lints/src/methods/manual_c_str_literals.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::{LitKind, StrStyle};
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind};
88
use rustc_lint::LateContext;
9+
use rustc_span::edition::Edition::Edition2021;
910
use rustc_span::{Span, Symbol, sym};
1011

1112
use super::MANUAL_C_STR_LITERALS;
@@ -25,6 +26,7 @@ pub(super) fn check_as_ptr<'tcx>(
2526
) {
2627
if let ExprKind::Lit(lit) = receiver.kind
2728
&& let LitKind::ByteStr(_, StrStyle::Cooked) | LitKind::Str(_, StrStyle::Cooked) = lit.node
29+
&& cx.tcx.sess.edition() >= Edition2021
2830
&& let casts_removed = peel_ptr_cast_ancestors(cx, expr)
2931
&& !get_parent_expr(cx, casts_removed).is_some_and(
3032
|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> {
6668
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>], msrv: &Msrv) {
6769
if let Some(fn_name) = is_c_str_function(cx, func)
6870
&& let [arg] = args
71+
&& cx.tcx.sess.edition() >= Edition2021
6972
&& msrv.meets(msrvs::C_STR_LITERALS)
7073
{
7174
match fn_name.as_str() {

tests/ui/manual_c_str_literals.fixed renamed to tests/ui/manual_c_str_literals.edition2021.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@revisions: edition2018 edition2021
2+
//@[edition2018] edition:2018
3+
//@[edition2021] edition:2021
14
#![warn(clippy::manual_c_str_literals)]
25
#![allow(clippy::no_effect)]
36

tests/ui/manual_c_str_literals.stderr renamed to tests/ui/manual_c_str_literals.edition2021.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: calling `CStr::new` with a byte string literal
2-
--> tests/ui/manual_c_str_literals.rs:31:5
2+
--> tests/ui/manual_c_str_literals.rs:34:5
33
|
44
LL | CStr::from_bytes_with_nul(b"foo\0");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
@@ -8,73 +8,73 @@ LL | CStr::from_bytes_with_nul(b"foo\0");
88
= help: to override `-D warnings` add `#[allow(clippy::manual_c_str_literals)]`
99

1010
error: calling `CStr::new` with a byte string literal
11-
--> tests/ui/manual_c_str_literals.rs:35:5
11+
--> tests/ui/manual_c_str_literals.rs:38:5
1212
|
1313
LL | CStr::from_bytes_with_nul(b"foo\0");
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
1515

1616
error: calling `CStr::new` with a byte string literal
17-
--> tests/ui/manual_c_str_literals.rs:36:5
17+
--> tests/ui/manual_c_str_literals.rs:39:5
1818
|
1919
LL | CStr::from_bytes_with_nul(b"foo\x00");
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
2121

2222
error: calling `CStr::new` with a byte string literal
23-
--> tests/ui/manual_c_str_literals.rs:37:5
23+
--> tests/ui/manual_c_str_literals.rs:40:5
2424
|
2525
LL | CStr::from_bytes_with_nul(b"foo\0").unwrap();
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
2727

2828
error: calling `CStr::new` with a byte string literal
29-
--> tests/ui/manual_c_str_literals.rs:38:5
29+
--> tests/ui/manual_c_str_literals.rs:41:5
3030
|
3131
LL | CStr::from_bytes_with_nul(b"foo\\0sdsd\0").unwrap();
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo\\0sdsd"`
3333

3434
error: calling `CStr::from_ptr` with a byte string literal
35-
--> tests/ui/manual_c_str_literals.rs:43:14
35+
--> tests/ui/manual_c_str_literals.rs:46:14
3636
|
3737
LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr().cast()) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
3939

4040
error: calling `CStr::from_ptr` with a byte string literal
41-
--> tests/ui/manual_c_str_literals.rs:44:14
41+
--> tests/ui/manual_c_str_literals.rs:47:14
4242
|
4343
LL | unsafe { CStr::from_ptr(b"foo\0".as_ptr() as *const _) };
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"foo"`
4545

4646
error: manually constructing a nul-terminated string
47-
--> tests/ui/manual_c_str_literals.rs:45:23
47+
--> tests/ui/manual_c_str_literals.rs:48:23
4848
|
4949
LL | let _: *const _ = b"foo\0".as_ptr();
5050
| ^^^^^^^^ help: use a `c""` literal: `c"foo"`
5151

5252
error: manually constructing a nul-terminated string
53-
--> tests/ui/manual_c_str_literals.rs:46:23
53+
--> tests/ui/manual_c_str_literals.rs:49:23
5454
|
5555
LL | let _: *const _ = "foo\0".as_ptr();
5656
| ^^^^^^^ help: use a `c""` literal: `c"foo"`
5757

5858
error: manually constructing a nul-terminated string
59-
--> tests/ui/manual_c_str_literals.rs:49:23
59+
--> tests/ui/manual_c_str_literals.rs:52:23
6060
|
6161
LL | let _: *const _ = b"foo\0".as_ptr().cast::<i8>();
6262
| ^^^^^^^^ help: use a `c""` literal: `c"foo"`
6363

6464
error: manually constructing a nul-terminated string
65-
--> tests/ui/manual_c_str_literals.rs:52:13
65+
--> tests/ui/manual_c_str_literals.rs:55:13
6666
|
6767
LL | let _ = "电脑\\\0".as_ptr();
6868
| ^^^^^^^^^^ help: use a `c""` literal: `c"电脑\\"`
6969

7070
error: manually constructing a nul-terminated string
71-
--> tests/ui/manual_c_str_literals.rs:53:13
71+
--> tests/ui/manual_c_str_literals.rs:56:13
7272
|
7373
LL | let _ = "电脑\0".as_ptr();
7474
| ^^^^^^^^ help: use a `c""` literal: `c"电脑"`
7575

7676
error: manually constructing a nul-terminated string
77-
--> tests/ui/manual_c_str_literals.rs:54:13
77+
--> tests/ui/manual_c_str_literals.rs:57:13
7878
|
7979
LL | let _ = "电脑\x00".as_ptr();
8080
| ^^^^^^^^^^ help: use a `c""` literal: `c"电脑"`

tests/ui/manual_c_str_literals.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@revisions: edition2018 edition2021
2+
//@[edition2018] edition:2018
3+
//@[edition2021] edition:2021
14
#![warn(clippy::manual_c_str_literals)]
25
#![allow(clippy::no_effect)]
36

0 commit comments

Comments
 (0)