diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-08-09 22:30:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-09 22:30:18 +0000 |
| commit | e6b63d15fa465cba7d0ca918e3aa1c27ebd64aee (patch) | |
| tree | 9a11fed0d137cef0bc246266bbedf7931163a897 | |
| parent | f3ced4d1c17995f0212cc43734cf25bd7d16c9c9 (diff) | |
| parent | 7a113811fab70d9d1932ad32ea392b9f100186ef (diff) | |
| download | rust-e6b63d15fa465cba7d0ca918e3aa1c27ebd64aee.tar.gz rust-e6b63d15fa465cba7d0ca918e3aa1c27ebd64aee.zip | |
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
| -rw-r--r-- | clippy_lints/src/from_str_radix_10.rs | 4 | ||||
| -rw-r--r-- | tests/ui/from_str_radix_10.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/from_str_radix_10.rs | 10 | ||||
| -rw-r--r-- | tests/ui/from_str_radix_10.stderr | 14 |
4 files changed, 35 insertions, 3 deletions
diff --git a/clippy_lints/src/from_str_radix_10.rs b/clippy_lints/src/from_str_radix_10.rs index b816963cc82..d5873b3f85a 100644 --- a/clippy_lints/src/from_str_radix_10.rs +++ b/clippy_lints/src/from_str_radix_10.rs @@ -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() } diff --git a/tests/ui/from_str_radix_10.fixed b/tests/ui/from_str_radix_10.fixed index 4b8fd778685..47d24167e56 100644 --- a/tests/ui/from_str_radix_10.fixed +++ b/tests/ui/from_str_radix_10.fixed @@ -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 +} diff --git a/tests/ui/from_str_radix_10.rs b/tests/ui/from_str_radix_10.rs index 89002b11a99..952e19b57a0 100644 --- a/tests/ui/from_str_radix_10.rs +++ b/tests/ui/from_str_radix_10.rs @@ -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 +} diff --git a/tests/ui/from_str_radix_10.stderr b/tests/ui/from_str_radix_10.stderr index c693e8f50ff..d4e6c3657f2 100644 --- a/tests/ui/from_str_radix_10.stderr +++ b/tests/ui/from_str_radix_10.stderr @@ -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 |
