rust-lang / rust-lang/rust-clippy

Trigger 'misnamed_getters' on traits' functions might be a false positive

Open
#10,514 1 comment 0 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

misnamed_getters is triggered on traits' functions, but the person who write the trait, and the person who write the type which will implement the trait might disagree on naming. So a difference between method's name and field's name in the case of a trait implementation is maybe justified and should not trigger this lint?

Lint Name

misnamed_getters

Reproducer

I tried this code:

use uuid::Uuid;

pub trait Id<T> {
    fn id(&self) -> Uuid;
}

pub struct House {
    pub id: Uuid,
    pub architect_id: Uuid,
}

pub struct Architect {
    pub id: Uuid,
    pub houses: Vec<Uuid>,
}

impl Id<House> for House {
    fn id(&self) -> Uuid {
        self.id
    }
}

impl Id<Architect> for House {
    fn id(&self) -> Uuid {
        self.architect_id
    }
}

I saw this happen:

warning: getter function appears to return the wrong field
  --> src/lib.rs:24:5
   |
24 | /     fn id(&self) -> Uuid {
25 | |         self.architect_id
   | |         ----------------- help: consider using: `self.id`
26 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#misnamed_getters
   = note: `#[warn(clippy::misnamed_getters)]` on by default

warning: `misnamed_getters` (lib) generated 1 warning

I expected to see nothing happening, since it's not an error in this case.

Note that I understand that it is questionable to remove this lint in this case. I see two arguments, one for, one against:

  • for name of trait's function cannot be chosen, and types that implement these traits might disagree on the naming and choose different fields' name
  • against it's still possible to #[(allow(clippy::misnamed_getters)] and it might still catch so honest mistakes

I'm happy to discuss it... and I might try to work on it at some point if we agree to disable this lint for traits' functions. Note that a quick win would be to document it in a Known problems section.

Version
rustc 1.68.0 (2c8cc3432 2023-03-06)
binary: rustc
commit-hash: 2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74
commit-date: 2023-03-06
host: x86_64-unknown-linux-gnu
release: 1.68.0
LLVM version: 15.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 by reproducing the reported misnamed_getters warning with the trait implementation shown in the issue, then locate the lint's implementation and documentation in rust-clippy. Check how trait functions are handled and review the existing discussion before choosing whether the warning should be suppressed or documented as a known problem. Done means the agreed behavior is covered by a regression test or documentation change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.