Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e1c6ead
move pattern migration setup/emitting to a separate module
dianne Feb 9, 2025
f1c287f
move pattern migration internals to the `migration` module
dianne Feb 9, 2025
7edd034
Use io::const_error! when possible over io::Error::new
thaliaarchi Feb 11, 2025
9e390b2
Fix &&str and trailing commas in io::const_error!
thaliaarchi Feb 11, 2025
b621a48
bootstrap: add more tracing to compiler/std/llvm flows
jieyouxu Feb 15, 2025
7b11816
bootstrap: take `target` by value in `is_builder_target`
jieyouxu Feb 15, 2025
05ba1a4
rustc-dev-guide: document `COMPILER` and `COMPILER_FOR` tracing targets
jieyouxu Feb 15, 2025
f53d0f5
invalid_from_utf8[_unchecked]: also lint inherent methods
GrigorenkoPV Feb 15, 2025
77571a5
clippy: string_from_utf8_as_bytes: also detect inherent `from_utf8`
GrigorenkoPV Feb 15, 2025
8a02724
Fix const items not being allowed to be called `r#move` or `r#static`
Noratrieb Feb 16, 2025
b023671
Add `pattern_complexity_limit` to `Limits`.
nnethercote Feb 6, 2025
30b8c84
Merge `get_limit` and `get_limit_size`.
nnethercote Feb 7, 2025
13280ee
Improve comments about limits.
nnethercote Feb 7, 2025
223c95f
Move `rustc_middle::limits` to `rustc_interface`.
nnethercote Feb 7, 2025
7a8c0fc
Rename `pattern_complexity` attr as `pattern_complexity_limit`.
nnethercote Feb 7, 2025
7ff6790
Avoid unnecessary use of the `Map` trait.
nnethercote Feb 3, 2025
b51ab7d
Clarify `impl Map for !`.
nnethercote Feb 3, 2025
6021ba0
Rename `rustc_middle/src/hir/map/mod.rs` as `map.rs`.
nnethercote Feb 2, 2025
cd1d84c
Remove unused `Map::hir_node_by_def_id` method.
nnethercote Feb 2, 2025
f86f7ad
Move some `Map` methods onto `TyCtxt`.
nnethercote Feb 2, 2025
661f99b
Overhaul the `intravisit::Map` trait.
nnethercote Feb 3, 2025
f666361
Remove `TyCtxt::hir_krate`.
nnethercote Feb 17, 2025
f3a4f1a
Rollup merge of #136466 - nnethercote:start-removing-Map, r=cjgillot
matthiaskrgr Feb 17, 2025
0c051c8
Rollup merge of #136671 - nnethercote:middle-limits, r=Nadrieril
matthiaskrgr Feb 17, 2025
767ec0a
Rollup merge of #136817 - dianne:clean-and-comment-pat-migration, r=N…
matthiaskrgr Feb 17, 2025
c04801d
Rollup merge of #136844 - thaliaarchi:const-io-error, r=ChrisDenton
matthiaskrgr Feb 17, 2025
5dd48bc
Rollup merge of #137080 - jieyouxu:more-tracing, r=onur-ozkan
matthiaskrgr Feb 17, 2025
86f3d52
Rollup merge of #137101 - GrigorenkoPV:str-inherent-lint, r=Urgau
matthiaskrgr Feb 17, 2025
f071099
Rollup merge of #137140 - Noratrieb:const-move, r=jieyouxu,compiler-e…
matthiaskrgr Feb 17, 2025
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
Prev Previous commit
Next Next commit
invalid_from_utf8[_unchecked]: also lint inherent methods
  • Loading branch information
GrigorenkoPV committed Feb 16, 2025
commit f53d0f502dba3b378d8baef159b97e685ba40f33
4 changes: 2 additions & 2 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ lint_invalid_crate_type_value = invalid `crate_type` value
.suggestion = did you mean

# FIXME: we should ordinalize $valid_up_to when we add support for doing so
lint_invalid_from_utf8_checked = calls to `{$method}` with a invalid literal always return an error
lint_invalid_from_utf8_checked = calls to `{$method}` with an invalid literal always return an error
.label = the literal was valid UTF-8 up to the {$valid_up_to} bytes

# FIXME: we should ordinalize $valid_up_to when we add support for doing so
lint_invalid_from_utf8_unchecked = calls to `{$method}` with a invalid literal are undefined behavior
lint_invalid_from_utf8_unchecked = calls to `{$method}` with an invalid literal are undefined behavior
.label = the literal was valid UTF-8 up to the {$valid_up_to} bytes

lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be directly compared to itself
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_lint/src/invalid_from_utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
sym::str_from_utf8_mut,
sym::str_from_utf8_unchecked,
sym::str_from_utf8_unchecked_mut,
sym::str_inherent_from_utf8,
sym::str_inherent_from_utf8_mut,
sym::str_inherent_from_utf8_unchecked,
sym::str_inherent_from_utf8_unchecked_mut,
]
.contains(&diag_item)
{
let lint = |label, utf8_error: Utf8Error| {
let method = diag_item.as_str().strip_prefix("str_").unwrap();
let method = format!("std::str::{method}");
let method = if let Some(method) = method.strip_prefix("inherent_") {
format!("str::{method}")
} else {
format!("std::str::{method}")
};
let valid_up_to = utf8_error.valid_up_to();
let is_unchecked_variant = diag_item.as_str().contains("unchecked");

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,10 @@ symbols! {
str_from_utf8_mut,
str_from_utf8_unchecked,
str_from_utf8_unchecked_mut,
str_inherent_from_utf8,
str_inherent_from_utf8_mut,
str_inherent_from_utf8_unchecked,
str_inherent_from_utf8_unchecked_mut,
str_len,
str_split_whitespace,
str_starts_with,
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl str {
/// assert_eq!("💖", sparkle_heart);
/// ```
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
#[rustc_diagnostic_item = "str_inherent_from_utf8"]
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
converts::from_utf8(v)
}
Expand Down Expand Up @@ -274,6 +275,7 @@ impl str {
/// errors that can be returned.
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
#[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")]
#[rustc_diagnostic_item = "str_inherent_from_utf8_mut"]
pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
converts::from_utf8_mut(v)
}
Expand Down Expand Up @@ -306,6 +308,7 @@ impl str {
#[inline]
#[must_use]
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
#[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked"]
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
// SAFETY: converts::from_utf8_unchecked has the same safety requirements as this function.
unsafe { converts::from_utf8_unchecked(v) }
Expand All @@ -331,6 +334,7 @@ impl str {
#[inline]
#[must_use]
#[unstable(feature = "inherent_str_constructors", issue = "131114")]
#[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked_mut"]
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
// SAFETY: converts::from_utf8_unchecked_mut has the same safety requirements as this function.
unsafe { converts::from_utf8_unchecked_mut(v) }
Expand Down
53 changes: 52 additions & 1 deletion tests/ui/lint/invalid_from_utf8.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//@ check-pass

#![feature(concat_bytes)]

#![feature(inherent_str_constructors)]
#![warn(invalid_from_utf8_unchecked)]
#![warn(invalid_from_utf8)]

pub fn from_utf8_unchecked_mut() {
// Valid
unsafe {
std::str::from_utf8_unchecked_mut(&mut [99, 108, 105, 112, 112, 121]);
str::from_utf8_unchecked_mut(&mut [99, 108, 105, 112, 112, 121]);
std::str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);

let x = 0xA0;
std::str::from_utf8_unchecked_mut(&mut [0xC0, x]);
Expand All @@ -19,101 +21,150 @@ pub fn from_utf8_unchecked_mut() {
unsafe {
std::str::from_utf8_unchecked_mut(&mut [99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `std::str::from_utf8_unchecked_mut`
str::from_utf8_unchecked_mut(&mut [99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `str::from_utf8_unchecked_mut`
std::str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `std::str::from_utf8_unchecked_mut`
str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `str::from_utf8_unchecked_mut`
}
}

pub fn from_utf8_unchecked() {
// Valid
unsafe {
std::str::from_utf8_unchecked(&[99, 108, 105, 112, 112, 121]);
str::from_utf8_unchecked(&[99, 108, 105, 112, 112, 121]);
std::str::from_utf8_unchecked(&[b'c', b'l', b'i', b'p', b'p', b'y']);
str::from_utf8_unchecked(&[b'c', b'l', b'i', b'p', b'p', b'y']);
std::str::from_utf8_unchecked(b"clippy");
str::from_utf8_unchecked(b"clippy");

let x = 0xA0;
std::str::from_utf8_unchecked(&[0xC0, x]);
str::from_utf8_unchecked(&[0xC0, x]);
}

// Invalid
unsafe {
std::str::from_utf8_unchecked(&[99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `std::str::from_utf8_unchecked`
str::from_utf8_unchecked(&[99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `str::from_utf8_unchecked`
std::str::from_utf8_unchecked(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `std::str::from_utf8_unchecked`
str::from_utf8_unchecked(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `str::from_utf8_unchecked`
std::str::from_utf8_unchecked(b"cl\x82ippy");
//~^ WARN calls to `std::str::from_utf8_unchecked`
str::from_utf8_unchecked(b"cl\x82ippy");
//~^ WARN calls to `str::from_utf8_unchecked`
std::str::from_utf8_unchecked(concat_bytes!(b"cl", b"\x82ippy"));
//~^ WARN calls to `std::str::from_utf8_unchecked`
str::from_utf8_unchecked(concat_bytes!(b"cl", b"\x82ippy"));
//~^ WARN calls to `str::from_utf8_unchecked`
}
}

pub fn from_utf8_mut() {
// Valid
{
std::str::from_utf8_mut(&mut [99, 108, 105, 112, 112, 121]);
str::from_utf8_mut(&mut [99, 108, 105, 112, 112, 121]);
std::str::from_utf8_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
str::from_utf8_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);

let x = 0xa0;
std::str::from_utf8_mut(&mut [0xc0, x]);
str::from_utf8_mut(&mut [0xc0, x]);
}

// Invalid
{
std::str::from_utf8_mut(&mut [99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `std::str::from_utf8_mut`
str::from_utf8_mut(&mut [99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `str::from_utf8_mut`
std::str::from_utf8_mut(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `std::str::from_utf8_mut`
str::from_utf8_mut(&mut [b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `str::from_utf8_mut`
}
}

pub fn from_utf8() {
// Valid
{
std::str::from_utf8(&[99, 108, 105, 112, 112, 121]);
str::from_utf8(&[99, 108, 105, 112, 112, 121]);
std::str::from_utf8(&[b'c', b'l', b'i', b'p', b'p', b'y']);
str::from_utf8(&[b'c', b'l', b'i', b'p', b'p', b'y']);
std::str::from_utf8(b"clippy");
str::from_utf8(b"clippy");

let x = 0xA0;
std::str::from_utf8(&[0xC0, x]);
str::from_utf8(&[0xC0, x]);
}

// Invalid
{
std::str::from_utf8(&[99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(&[99, 108, 130, 105, 112, 112, 121]);
//~^ WARN calls to `str::from_utf8`
std::str::from_utf8(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']);
//~^ WARN calls to `str::from_utf8`
std::str::from_utf8(b"cl\x82ippy");
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(b"cl\x82ippy");
//~^ WARN calls to `str::from_utf8`
std::str::from_utf8(concat_bytes!(b"cl", b"\x82ippy"));
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(concat_bytes!(b"cl", b"\x82ippy"));
//~^ WARN calls to `str::from_utf8`
}
}

pub fn from_utf8_with_indirections() {
let mut a = [99, 108, 130, 105, 112, 112, 121];
std::str::from_utf8_mut(&mut a);
//~^ WARN calls to `std::str::from_utf8_mut`
str::from_utf8_mut(&mut a);
//~^ WARN calls to `str::from_utf8_mut`
let mut b = &mut a;
let mut c = b;
std::str::from_utf8_mut(c);
//~^ WARN calls to `std::str::from_utf8_mut`
str::from_utf8_mut(c);
//~^ WARN calls to `str::from_utf8_mut`
let mut c = &[99, 108, 130, 105, 112, 112, 121];
std::str::from_utf8(c);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(c);
//~^ WARN calls to `str::from_utf8`
const INVALID_1: [u8; 7] = [99, 108, 130, 105, 112, 112, 121];
std::str::from_utf8(&INVALID_1);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(&INVALID_1);
//~^ WARN calls to `str::from_utf8`
static INVALID_2: [u8; 7] = [99, 108, 130, 105, 112, 112, 121];
std::str::from_utf8(&INVALID_2);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(&INVALID_2);
//~^ WARN calls to `str::from_utf8`
const INVALID_3: &'static [u8; 7] = &[99, 108, 130, 105, 112, 112, 121];
std::str::from_utf8(INVALID_3);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(INVALID_3);
//~^ WARN calls to `str::from_utf8`
const INVALID_4: &'static [u8; 7] = { &[99, 108, 130, 105, 112, 112, 121] };
std::str::from_utf8(INVALID_4);
//~^ WARN calls to `std::str::from_utf8`
str::from_utf8(INVALID_4);
//~^ WARN calls to `str::from_utf8`
}

fn main() {}
Loading