rust-lang / rust-lang/rust-clippy

should_implement_trait shouldn't emit for from_str methods on types with lifetimes

Open
#9,762 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy wrongly suggests implementing an incompatible trait

Lint Name

should_implement_trait

Reproducer

I tried this code:

pub struct HoldsStr<'a> {
    _s: &'a str,
}

impl<'a> HoldsStr<'a> {
    pub fn from_str(_s: &'a str) -> Self {
        Self { _s }
    }
}

I saw this happen:

warning: method `from_str` can be confused for the standard trait method `std::str::FromStr::from_str`
 --> src/lib.rs:6:5
  |
6 | /     pub fn from_str(_s: &'a str) -> Self {
7 | |         Self { _s }
8 | |     }
  | |_____^
  |
  = help: consider implementing the trait `std::str::FromStr` or choosing a less ambiguous method name
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
  = note: `#[warn(clippy::should_implement_trait)]` on by default

I expected to see this happen:

No warnings

Version
rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6
Additional Labels

No response

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 at the should_implement_trait lint and reproduce the warning with the HoldsStr<'a> example from the issue. Trace how from_str methods on types with lifetimes are classified, then add coverage for the reproducer and confirm that it produces no warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.