Skip to content

Commit 0b2cfa5

Browse files
authored
Rollup merge of #111543 - Urgau:uplift_invalid_utf8_in_unchecked, r=WaffleLapkin
Uplift `clippy::invalid_utf8_in_unchecked` lint This PR aims at uplifting the `clippy::invalid_utf8_in_unchecked` lint into two lints. ## `invalid_from_utf8_unchecked` (deny-by-default) The `invalid_from_utf8_unchecked` lint checks for calls to `std::str::from_utf8_unchecked` and `std::str::from_utf8_unchecked_mut` with an invalid UTF-8 literal. ### Example ```rust unsafe { std::str::from_utf8_unchecked(b"cl\x82ippy"); } ``` ### Explanation Creating such a `str` would result in undefined behavior as per documentation for `std::str::from_utf8_unchecked` and `std::str::from_utf8_unchecked_mut`. ## `invalid_from_utf8` (warn-by-default) The `invalid_from_utf8` lint checks for calls to `std::str::from_utf8` and `std::str::from_utf8_mut` with an invalid UTF-8 literal. ### Example ```rust std::str::from_utf8(b"ru\x82st"); ``` ### Explanation Trying to create such a `str` would always return an error as per documentation for `std::str::from_utf8` and `std::str::from_utf8_mut`. ----- Mostly followed the instructions for uplifting a clippy lint described here: rust-lang/rust#99696 (review) ````@rustbot```` label: +I-lang-nominated r? compiler ----- For Clippy: changelog: Moves: Uplifted `clippy::invalid_utf8_in_unchecked` into rustc
2 parents 0f4296a + d38bebb commit 0b2cfa5

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

alloc/tests/str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(bootstrap), allow(invalid_from_utf8))]
2+
13
use std::assert_matches::assert_matches;
24
use std::borrow::Cow;
35
use std::cmp::Ordering::{Equal, Greater, Less};

core/src/str/converts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ use super::Utf8Error;
8484
#[stable(feature = "rust1", since = "1.0.0")]
8585
#[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
8686
#[rustc_allow_const_fn_unstable(str_internals)]
87+
#[rustc_diagnostic_item = "str_from_utf8"]
8788
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
8889
// FIXME: This should use `?` again, once it's `const`
8990
match run_utf8_validation(v) {
@@ -127,6 +128,7 @@ pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
127128
/// errors that can be returned.
128129
#[stable(feature = "str_mut_extras", since = "1.20.0")]
129130
#[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")]
131+
#[rustc_diagnostic_item = "str_from_utf8_mut"]
130132
pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
131133
// This should use `?` again, once it's `const`
132134
match run_utf8_validation(v) {
@@ -167,6 +169,7 @@ pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
167169
#[must_use]
168170
#[stable(feature = "rust1", since = "1.0.0")]
169171
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")]
172+
#[rustc_diagnostic_item = "str_from_utf8_unchecked"]
170173
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
171174
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
172175
// Also relies on `&str` and `&[u8]` having the same layout.
@@ -194,6 +197,7 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
194197
#[must_use]
195198
#[stable(feature = "str_mut_extras", since = "1.20.0")]
196199
#[rustc_const_unstable(feature = "const_str_from_utf8_unchecked_mut", issue = "91005")]
200+
#[rustc_diagnostic_item = "str_from_utf8_unchecked_mut"]
197201
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
198202
// SAFETY: the caller must guarantee that the bytes `v`
199203
// are valid UTF-8, thus the cast to `*mut str` is safe.

0 commit comments

Comments
 (0)