Skip to content

fix &str type check in from_str_radix_10 #15410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions clippy_lints/src/from_str_radix_10.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
}
10 changes: 10 additions & 0 deletions tests/ui/from_str_radix_10.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions tests/ui/from_str_radix_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 13 additions & 1 deletion tests/ui/from_str_radix_10.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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