Skip to content

Lint towards replacing from_str_radix with str::parse when the radix is 10 #6713

Closed
@booleancoercion

Description

@booleancoercion

What it does

The lint suggests replacing e.g. u16::from_str_radix(&string, 10) with string.parse().
(Obviously, it could also be any other primitive with such associated function).

Categories (optional)

  • Kind: perhaps this could go in clippy::style.

What is the advantage of the recommended code over the original code?

The resulting code is shorter, easier to read and doesn't involve using 10 as a magic number.

Drawbacks

Using str::parse instead, the user might have to specify types in some cases.

Example

let input: &str = get_input();
let num = u16::from_str_radix(input, 10)?;

Could be written as:

let input: &str = get_input();
let num: u16 = input.parse()?;

(In a more complicated use case, the type of num will probably be inferred.)

Metadata

Metadata

Labels

A-lintArea: New lintsT-middleType: Probably requires verifiying types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions