Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong lint in case of to_*_mut method #6758

Closed
dodomorandi opened this issue Feb 18, 2021 · 1 comment · Fixed by #6828
Closed

Wrong lint in case of to_*_mut method #6758

dodomorandi opened this issue Feb 18, 2021 · 1 comment · Fixed by #6828
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dodomorandi
Copy link

dodomorandi commented Feb 18, 2021

Lint name: wrong_self_convention

I tried this code:

pub enum Test<T> {
    None,
    One(T),
    Many(Vec<T>),
}

impl<T> Test<T> {
    pub fn to_many(&self) -> Option<&[T]> {
        match self {
            Self::Many(data) => Some(data),
            _ => None,
        }
    }

    pub fn to_many_mut(&mut self) -> Option<&mut [T]> {
        match self {
            Self::Many(data) => Some(data),
            _ => None,
        }
    }
}

I expected to see this happen: in order to get an optional variant of the enum, the naming convention to_* should be used, because the match statement cannot be considered free (therefore as_* is not ok). Moreover, the name to_mut (à la Cow::to_mut) cannot be used in this case, because there are two non-empty variants. Therefore using to_many_mut for the name of the method seems the right choice.

Instead, this happened: clippy says that to_* should follow the convention of taking self by reference.

My rationale is that, when a method starts with to_, it should expect a &mut self parameter if ends with _mut, otherwise &self.

Meta

  • cargo clippy -V: clippy 0.0.212 (cb75ad5 2021-02-10)
  • rustc -Vv:
    rustc 1.50.0 (cb75ad5db 2021-02-10)
    binary: rustc
    commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
    commit-date: 2021-02-10
    host: x86_64-unknown-linux-gnu
    release: 1.50.0
    
@dodomorandi dodomorandi added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 18, 2021
@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label Feb 18, 2021
@mgacek8
Copy link
Contributor

mgacek8 commented Mar 2, 2021

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants