rust-lang / rust-lang/rust-clippy
Lint for use of operator's trait's method instead of the operator itself
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
If somebody calls std::ops::Add::add(foo, bar), suggest foo + bar.
In my experience with Exercism, beginners often find the API they need in the trait, not realizing its relation to the operator. Naming the trait method requires an import and is usually unnecessarily verbose, so we might want to lint against it.
Categories
- Kind:
clippy::style
Drawbacks
None.
Example
use std::ops::Add;
use chrono::{DateTime, Duration, Utc};
pub fn time_travel(now: DateTime<Utc>, how_long: Duration)
-> DateTime<Utc>
{
now.add(how_long)
}
Could be written as:
use std::ops::Add;
use chrono::{DateTime, Duration, Utc};
pub fn time_travel(now: DateTime<Utc>, how_long: Duration)
-> DateTime<Utc>
{
now + how_long
}
Note
This is a follow-up to #5679, which described a similar issue for FromIterator, and the case of operator traits got mentioned in the comments. The PR that solved the FromIterator case didn't handle this case, so i'm opening a separate issue.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the related #5679 issue and the implementation that handled its FromIterator case, then locate the corresponding Clippy lint and test structure. Add coverage for operator trait method calls such as Add::add and verify that the lint suggests the equivalent operator expression.
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
- 35/100