Skip to content

Lint for use of operator's trait's method instead of the operator itself #6286

Open
@meithecatte

Description

@meithecatte

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsS-needs-discussionStatus: Needs further discussion before merging or work can be started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions