1
Fork 0

Rollup merge of #137101 - GrigorenkoPV:str-inherent-lint, r=Urgau

`invalid_from_utf8[_unchecked]`: also lint inherent methods

Addressing https://github.com/rust-lang/rust/issues/131114#issuecomment-2646663535

Also corrected a typo: "_an_ invalid literal", not "_a_ invalid literal".
This commit is contained in:
Matthias Krüger 2025-02-17 06:37:38 +01:00 committed by GitHub
commit 86f3d525e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 274 additions and 47 deletions

View file

@ -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");