fix &str type check in from_str_radix_10 (#15410)

minor fix in `from_str_radix_10` lint, `is_type_diagnostic_item` only
checks `Adt`, use `.is_str()` instead

changelog: [`from_str_radix_10`]: properly lint references to `&str` as
well
This commit is contained in:
Samuel Tardieu 2025-08-09 22:30:18 +00:00 committed by GitHub
commit e6b63d15fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 3 deletions

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
use clippy_utils::ty::is_type_lang_item;
use clippy_utils::{is_in_const_context, is_integer_literal, sym};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem, PrimTy, QPath, TyKind, def};
@ -89,5 +89,5 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
/// Checks if a Ty is `String` or `&str`
fn is_ty_stringish(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
is_type_lang_item(cx, ty, LangItem::String) || is_type_diagnostic_item(cx, ty, sym::str)
is_type_lang_item(cx, ty, LangItem::String) || ty.peel_refs().is_str()
}

View file

@ -74,3 +74,13 @@ fn issue_12731() {
let _ = u32::from_str_radix("123", 10);
}
}
fn fix_str_ref_check() {
#![allow(clippy::needless_borrow)]
let s = "1";
let _ = s.parse::<u32>().unwrap();
//~^ from_str_radix_10
let s_ref = &s;
let _ = s_ref.parse::<u32>().unwrap();
//~^ from_str_radix_10
}

View file

@ -74,3 +74,13 @@ fn issue_12731() {
let _ = u32::from_str_radix("123", 10);
}
}
fn fix_str_ref_check() {
#![allow(clippy::needless_borrow)]
let s = "1";
let _ = u32::from_str_radix(&s, 10).unwrap();
//~^ from_str_radix_10
let s_ref = &s;
let _ = u32::from_str_radix(&s_ref, 10).unwrap();
//~^ from_str_radix_10
}

View file

@ -49,5 +49,17 @@ error: this call to `from_str_radix` can be replaced with a call to `str::parse`
LL | i32::from_str_radix(&stringier, 10)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `stringier.parse::<i32>()`
error: aborting due to 8 previous errors
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
--> tests/ui/from_str_radix_10.rs:81:13
|
LL | let _ = u32::from_str_radix(&s, 10).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.parse::<u32>()`
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
--> tests/ui/from_str_radix_10.rs:84:13
|
LL | let _ = u32::from_str_radix(&s_ref, 10).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s_ref.parse::<u32>()`
error: aborting due to 10 previous errors