Skip to content

Commit e6b63d1

Browse files
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
2 parents f3ced4d + 7a11381 commit e6b63d1

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

clippy_lints/src/from_str_radix_10.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::sugg::Sugg;
3-
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
3+
use clippy_utils::ty::is_type_lang_item;
44
use clippy_utils::{is_in_const_context, is_integer_literal, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem, PrimTy, QPath, TyKind, def};
@@ -89,5 +89,5 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
8989

9090
/// Checks if a Ty is `String` or `&str`
9191
fn is_ty_stringish(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
92-
is_type_lang_item(cx, ty, LangItem::String) || is_type_diagnostic_item(cx, ty, sym::str)
92+
is_type_lang_item(cx, ty, LangItem::String) || ty.peel_refs().is_str()
9393
}

tests/ui/from_str_radix_10.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ fn issue_12731() {
7474
let _ = u32::from_str_radix("123", 10);
7575
}
7676
}
77+
78+
fn fix_str_ref_check() {
79+
#![allow(clippy::needless_borrow)]
80+
let s = "1";
81+
let _ = s.parse::<u32>().unwrap();
82+
//~^ from_str_radix_10
83+
let s_ref = &s;
84+
let _ = s_ref.parse::<u32>().unwrap();
85+
//~^ from_str_radix_10
86+
}

tests/ui/from_str_radix_10.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ fn issue_12731() {
7474
let _ = u32::from_str_radix("123", 10);
7575
}
7676
}
77+
78+
fn fix_str_ref_check() {
79+
#![allow(clippy::needless_borrow)]
80+
let s = "1";
81+
let _ = u32::from_str_radix(&s, 10).unwrap();
82+
//~^ from_str_radix_10
83+
let s_ref = &s;
84+
let _ = u32::from_str_radix(&s_ref, 10).unwrap();
85+
//~^ from_str_radix_10
86+
}

tests/ui/from_str_radix_10.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,17 @@ error: this call to `from_str_radix` can be replaced with a call to `str::parse`
4949
LL | i32::from_str_radix(&stringier, 10)?;
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `stringier.parse::<i32>()`
5151

52-
error: aborting due to 8 previous errors
52+
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
53+
--> tests/ui/from_str_radix_10.rs:81:13
54+
|
55+
LL | let _ = u32::from_str_radix(&s, 10).unwrap();
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.parse::<u32>()`
57+
58+
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
59+
--> tests/ui/from_str_radix_10.rs:84:13
60+
|
61+
LL | let _ = u32::from_str_radix(&s_ref, 10).unwrap();
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s_ref.parse::<u32>()`
63+
64+
error: aborting due to 10 previous errors
5365

0 commit comments

Comments
 (0)