rust-lang / rust-lang/rust-clippy

Warn if str.parse if no explicit type specified

Open
#9,107 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint L-suspicious
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warn for &str.parse() if the inferred type is i32 without explicit specify.

Lint Name

STR_PARSE_INFER_TO_I32

Category

suspicious

Advantage

Hint developer that the parse actually only process i32, if the value is larger, the code fails.

Drawbacks

None

Example
x.parse().unwrap_or(0) 

Could be written as:

x.parse::<i32>().unwrap_or(0)
if "111111111111111111".parse().unwrap_or(0) > 0 {
    println!("good");
} else {
    println!("bad");
}

if "111111111111111111".parse::<_>().unwrap_or(0) > 0 {
    println!("good");
} else {
    println!("bad");
}

if "111111111111111111".parse::<i64>().unwrap_or(0) > 0 {
    println!("good");
} else {
    println!("bad");
}

outputs

bad
bad
good

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing str.parse handling and lint registration in rust-clippy, then inspect how similar suspicious lints express diagnostics and tests. Use the provided i32, inferred, and explicit i64 examples to define the expected warnings and non-warnings; done means the STR_PARSE_INFER_TO_I32 lint is implemented and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.