Skip to content

Commit 5bce431

Browse files
committed
invalid_from_utf8[_unchecked]: also lint inherent methods
1 parent 608e228 commit 5bce431

File tree

7 files changed

+419
-220
lines changed

7 files changed

+419
-220
lines changed

compiler/rustc_lint/src/invalid_from_utf8.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,20 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
7070
sym::str_from_utf8_mut,
7171
sym::str_from_utf8_unchecked,
7272
sym::str_from_utf8_unchecked_mut,
73+
sym::str_inherent_from_utf8,
74+
sym::str_inherent_from_utf8_mut,
75+
sym::str_inherent_from_utf8_unchecked,
76+
sym::str_inherent_from_utf8_unchecked_mut,
7377
]
7478
.contains(&diag_item)
7579
{
7680
let lint = |label, utf8_error: Utf8Error| {
7781
let method = diag_item.as_str().strip_prefix("str_").unwrap();
78-
let method = format!("std::str::{method}");
82+
let method = if let Some(method) = method.strip_prefix("inherent_") {
83+
format!("str::{method}")
84+
} else {
85+
format!("std::str::{method}")
86+
};
7987
let valid_up_to = utf8_error.valid_up_to();
8088
let is_unchecked_variant = diag_item.as_str().contains("unchecked");
8189

compiler/rustc_span/src/symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,10 @@ symbols! {
19731973
str_from_utf8_mut,
19741974
str_from_utf8_unchecked,
19751975
str_from_utf8_unchecked_mut,
1976+
str_inherent_from_utf8,
1977+
str_inherent_from_utf8_mut,
1978+
str_inherent_from_utf8_unchecked,
1979+
str_inherent_from_utf8_unchecked_mut,
19761980
str_len,
19771981
str_split_whitespace,
19781982
str_starts_with,

library/core/src/str/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ impl str {
238238
/// assert_eq!("💖", sparkle_heart);
239239
/// ```
240240
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
241+
#[rustc_diagnostic_item = "str_inherent_from_utf8"]
241242
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
242243
converts::from_utf8(v)
243244
}
@@ -274,6 +275,7 @@ impl str {
274275
/// errors that can be returned.
275276
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
276277
#[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")]
278+
#[rustc_diagnostic_item = "str_inherent_from_utf8_mut"]
277279
pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
278280
converts::from_utf8_mut(v)
279281
}
@@ -306,6 +308,7 @@ impl str {
306308
#[inline]
307309
#[must_use]
308310
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
311+
#[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked"]
309312
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
310313
// SAFETY: converts::from_utf8_unchecked has the same safety requirements as this function.
311314
unsafe { converts::from_utf8_unchecked(v) }
@@ -331,6 +334,7 @@ impl str {
331334
#[inline]
332335
#[must_use]
333336
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
337+
#[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked_mut"]
334338
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
335339
// SAFETY: converts::from_utf8_unchecked_mut has the same safety requirements as this function.
336340
unsafe { converts::from_utf8_unchecked_mut(v) }
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
warning: calls to `str::from_utf8_unchecked_mut` with a invalid literal are undefined behavior
2+
--> $DIR/invalid_from_utf8.rs:36:9
3+
|
4+
LL | call!(from_utf8_unchecked_mut)(&mut [99, 108, 130, 105, 112, 112, 121]);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^
6+
| |
7+
| the literal was valid UTF-8 up to the 2 bytes
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/invalid_from_utf8.rs:7:9
11+
|
12+
LL | #![warn(invalid_from_utf8_unchecked)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
warning: calls to `str::from_utf8_unchecked_mut` with a invalid literal are undefined behavior
16+
--> $DIR/invalid_from_utf8.rs:38:9
17+
|
18+
LL | call!(from_utf8_unchecked_mut)(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------------------^
20+
| |
21+
| the literal was valid UTF-8 up to the 2 bytes
22+
23+
warning: calls to `str::from_utf8_unchecked` with a invalid literal are undefined behavior
24+
--> $DIR/invalid_from_utf8.rs:56:9
25+
|
26+
LL | call!(from_utf8_unchecked)(&[99, 108, 130, 105, 112, 112, 121]);
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^
28+
| |
29+
| the literal was valid UTF-8 up to the 2 bytes
30+
31+
warning: calls to `str::from_utf8_unchecked` with a invalid literal are undefined behavior
32+
--> $DIR/invalid_from_utf8.rs:58:9
33+
|
34+
LL | call!(from_utf8_unchecked)(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------------------^
36+
| |
37+
| the literal was valid UTF-8 up to the 2 bytes
38+
39+
warning: calls to `str::from_utf8_unchecked` with a invalid literal are undefined behavior
40+
--> $DIR/invalid_from_utf8.rs:60:9
41+
|
42+
LL | call!(from_utf8_unchecked)(b"cl\x82ippy");
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------^
44+
| |
45+
| the literal was valid UTF-8 up to the 2 bytes
46+
47+
warning: calls to `str::from_utf8_unchecked` with a invalid literal are undefined behavior
48+
--> $DIR/invalid_from_utf8.rs:62:9
49+
|
50+
LL | call!(from_utf8_unchecked)(concat_bytes!(b"cl", b"\x82ippy"));
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^
52+
| |
53+
| the literal was valid UTF-8 up to the 2 bytes
54+
55+
warning: calls to `str::from_utf8_mut` with a invalid literal always return an error
56+
--> $DIR/invalid_from_utf8.rs:79:9
57+
|
58+
LL | call!(from_utf8_mut)(&mut [99, 108, 130, 105, 112, 112, 121]);
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^
60+
| |
61+
| the literal was valid UTF-8 up to the 2 bytes
62+
|
63+
note: the lint level is defined here
64+
--> $DIR/invalid_from_utf8.rs:8:9
65+
|
66+
LL | #![warn(invalid_from_utf8)]
67+
| ^^^^^^^^^^^^^^^^^
68+
69+
warning: calls to `str::from_utf8_mut` with a invalid literal always return an error
70+
--> $DIR/invalid_from_utf8.rs:81:9
71+
|
72+
LL | call!(from_utf8_mut)(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------------------^
74+
| |
75+
| the literal was valid UTF-8 up to the 2 bytes
76+
77+
warning: calls to `str::from_utf8` with a invalid literal always return an error
78+
--> $DIR/invalid_from_utf8.rs:99:9
79+
|
80+
LL | call!(from_utf8)(&[99, 108, 130, 105, 112, 112, 121]);
81+
| ^^^^^^^^^^^^^^^^^^----------------------------------^
82+
| |
83+
| the literal was valid UTF-8 up to the 2 bytes
84+
85+
warning: calls to `str::from_utf8` with a invalid literal always return an error
86+
--> $DIR/invalid_from_utf8.rs:101:9
87+
|
88+
LL | call!(from_utf8)(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
89+
| ^^^^^^^^^^^^^^^^^^---------------------------------------------^
90+
| |
91+
| the literal was valid UTF-8 up to the 2 bytes
92+
93+
warning: calls to `str::from_utf8` with a invalid literal always return an error
94+
--> $DIR/invalid_from_utf8.rs:103:9
95+
|
96+
LL | call!(from_utf8)(b"cl\x82ippy");
97+
| ^^^^^^^^^^^^^^^^^-------------^
98+
| |
99+
| the literal was valid UTF-8 up to the 2 bytes
100+
101+
warning: calls to `str::from_utf8` with a invalid literal always return an error
102+
--> $DIR/invalid_from_utf8.rs:105:9
103+
|
104+
LL | call!(from_utf8)(concat_bytes!(b"cl", b"\x82ippy"));
105+
| ^^^^^^^^^^^^^^^^^---------------------------------^
106+
| |
107+
| the literal was valid UTF-8 up to the 2 bytes
108+
109+
warning: calls to `str::from_utf8_mut` with a invalid literal always return an error
110+
--> $DIR/invalid_from_utf8.rs:112:5
111+
|
112+
LL | let mut a = [99, 108, 130, 105, 112, 112, 121];
113+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
114+
LL | call!(from_utf8_mut)(&mut a);
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
117+
warning: calls to `str::from_utf8_mut` with a invalid literal always return an error
118+
--> $DIR/invalid_from_utf8.rs:116:5
119+
|
120+
LL | let mut a = [99, 108, 130, 105, 112, 112, 121];
121+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
122+
...
123+
LL | call!(from_utf8_mut)(c);
124+
| ^^^^^^^^^^^^^^^^^^^^^^^
125+
126+
warning: calls to `str::from_utf8` with a invalid literal always return an error
127+
--> $DIR/invalid_from_utf8.rs:119:5
128+
|
129+
LL | let mut c = &[99, 108, 130, 105, 112, 112, 121];
130+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
131+
LL | call!(from_utf8)(c);
132+
| ^^^^^^^^^^^^^^^^^^^
133+
134+
warning: calls to `str::from_utf8` with a invalid literal always return an error
135+
--> $DIR/invalid_from_utf8.rs:122:5
136+
|
137+
LL | const INVALID_1: [u8; 7] = [99, 108, 130, 105, 112, 112, 121];
138+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
139+
LL | call!(from_utf8)(&INVALID_1);
140+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141+
142+
warning: calls to `str::from_utf8` with a invalid literal always return an error
143+
--> $DIR/invalid_from_utf8.rs:125:5
144+
|
145+
LL | static INVALID_2: [u8; 7] = [99, 108, 130, 105, 112, 112, 121];
146+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
147+
LL | call!(from_utf8)(&INVALID_2);
148+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
149+
150+
warning: calls to `str::from_utf8` with a invalid literal always return an error
151+
--> $DIR/invalid_from_utf8.rs:128:5
152+
|
153+
LL | const INVALID_3: &'static [u8; 7] = &[99, 108, 130, 105, 112, 112, 121];
154+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
155+
LL | call!(from_utf8)(INVALID_3);
156+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
157+
158+
warning: calls to `str::from_utf8` with a invalid literal always return an error
159+
--> $DIR/invalid_from_utf8.rs:131:5
160+
|
161+
LL | const INVALID_4: &'static [u8; 7] = { &[99, 108, 130, 105, 112, 112, 121] };
162+
| ---------------------------------- the literal was valid UTF-8 up to the 2 bytes
163+
LL | call!(from_utf8)(INVALID_4);
164+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
165+
166+
warning: 19 warnings emitted
167+

0 commit comments

Comments
 (0)