rust-lang / rust-lang/rust-clippy

Suggest Usage of AsRef, Into, …

Open
#1,114 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-hard T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

And now for something completely crazy* that just came to my mind**…

Given a function f that has an input parameter p of type X, it is possible to determine all method calls (and thus the trait the method is implemented on) in the function body (and, recursively, all functions that are called in the body with p as parameter).

If X is a concrete type (i.e., not generic), and all the methods are on traits that X implements, it may be possible to find some constraints C to replace X with a generic type T: C so that f<T: C>(p: T) returns the same result as f(p: X) for all possible values of x, but allows strictly more inputs.

A lint can then suggest to use this constraint generic type instead of the concrete one. It should probably only suggest that if there are less than 4 constraints needed (incl. lifetimes). (Giving up in the collection phase of one input parameter when the 5th constraint is found is a possible way to speed this up a bit.)

tl;dr Instead of fn foo(s: &str) you could probably also write fn foo<T: AsRef<str>>(s: T) and some other, more complicated stuff.


Yes I know this will be slow and crazy and—look, I needed to go all science-y to even describe it, I know this is a long shot 😄


* So basically the same as all the other issues I opened here 😉
** I got the idea from https://github.com/cksac/fake-rs/pull/2 (which is probably even more complicated).

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

No files, tests, or entry points are named. Start by determining whether Clippy has an existing lint architecture suitable for recursively analyzing parameter usage and trait constraints; done would require a defined, bounded analysis with tests proving safe suggestions for concrete parameters.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
18/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.