Skip to content

Commit d36fe85

Browse files
Made parens addition smarter and added tests with bless
1 parent 642efab commit d36fe85

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

clippy_lints/src/from_str_radix_10.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66

77
use crate::utils::span_lint_and_sugg;
8+
use crate::utils::sugg::Sugg;
89

910
declare_clippy_lint! {
1011
/// **What it does:**
@@ -58,14 +59,20 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
5859
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
5960

6061
then {
61-
let orig_string = crate::utils::snippet(cx, arguments[0].span, "string");
62+
let sugg = Sugg::hir_with_applicability(
63+
cx,
64+
&arguments[0],
65+
"<string>",
66+
&mut Applicability::MachineApplicable
67+
).maybe_par();
68+
6269
span_lint_and_sugg(
6370
cx,
6471
FROM_STR_RADIX_10,
6572
exp.span,
6673
"this call to `from_str_radix` can be replaced with a call to `str::parse`",
6774
"try",
68-
format!("({}).parse()", orig_string),
75+
format!("{}.parse::<{}>()", sugg, prim_ty.name_str()),
6976
Applicability::MaybeIncorrect
7077
);
7178
}

tests/ui/from_str_radix_10.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@ fn from_str_radix(_: &str, _: u32) -> Result<(), std::num::ParseIntError> {
1212
unimplemented!()
1313
}
1414

15+
// to test parenthesis addition
16+
struct Test;
17+
18+
impl std::ops::Add<Test> for Test {
19+
type Output = &'static str;
20+
21+
fn add(self, _: Self) -> Self::Output {
22+
"304"
23+
}
24+
}
25+
1526
fn main() -> Result<(), Box<dyn std::error::Error>> {
1627
// all of these should trigger the lint
1728
u32::from_str_radix("30", 10)?;
1829
i64::from_str_radix("24", 10)?;
1930
isize::from_str_radix("100", 10)?;
2031
u8::from_str_radix("7", 10)?;
32+
u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
33+
i128::from_str_radix(Test + Test, 10)?;
2134

2235
let string = "300";
2336
i32::from_str_radix(string, 10)?;

tests/ui/from_str_radix_10.stderr

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
2-
--> $DIR/from_str_radix_10.rs:17:5
2+
--> $DIR/from_str_radix_10.rs:28:5
33
|
44
LL | u32::from_str_radix("30", 10)?;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("30").parse()`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"30".parse::<u32>()`
66
|
77
= note: `-D clippy::from-str-radix-10` implied by `-D warnings`
88

99
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
10-
--> $DIR/from_str_radix_10.rs:18:5
10+
--> $DIR/from_str_radix_10.rs:29:5
1111
|
1212
LL | i64::from_str_radix("24", 10)?;
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("24").parse()`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"24".parse::<i64>()`
1414

1515
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
16-
--> $DIR/from_str_radix_10.rs:19:5
16+
--> $DIR/from_str_radix_10.rs:30:5
1717
|
1818
LL | isize::from_str_radix("100", 10)?;
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("100").parse()`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"100".parse::<isize>()`
2020

2121
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
22-
--> $DIR/from_str_radix_10.rs:20:5
22+
--> $DIR/from_str_radix_10.rs:31:5
2323
|
2424
LL | u8::from_str_radix("7", 10)?;
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("7").parse()`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"7".parse::<u8>()`
2626

2727
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
28-
--> $DIR/from_str_radix_10.rs:23:5
28+
--> $DIR/from_str_radix_10.rs:32:5
29+
|
30+
LL | u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(&("10".to_owned() + "5")).parse::<u16>()`
32+
33+
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
34+
--> $DIR/from_str_radix_10.rs:33:5
35+
|
36+
LL | i128::from_str_radix(Test + Test, 10)?;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(Test + Test).parse::<i128>()`
38+
39+
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
40+
--> $DIR/from_str_radix_10.rs:36:5
2941
|
3042
LL | i32::from_str_radix(string, 10)?;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(string).parse()`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.parse::<i32>()`
3244

33-
error: aborting due to 5 previous errors
45+
error: aborting due to 7 previous errors
3446

0 commit comments

Comments
 (0)