Skip to content

Trait functions cannot be const #3841

@jhpratt

Description

@jhpratt
Member

When implementing a trait, clippy currently suggests making one of the functions const. That is currently not possible. At least for now, it seems like this should be an exception.

Activity

added
C-bugCategory: Clippy is not doing the correct thing
on Mar 4, 2019
phansch

phansch commented on Mar 4, 2019

@phansch
Member

That sounds like a bug, thanks! Could you provide an example that triggers the lint?

jhpratt

jhpratt commented on Mar 4, 2019

@jhpratt
MemberAuthor

@phansch Here's a simple example that actually has the error twice, once for the declared function, and one for the derived Eq trait.

#![warn(clippy::missing_const_for_fn)]

#[derive(PartialEq, Eq)]
struct Point(isize, isize);

impl std::ops::Add for Point {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Point(self.0 + other.0, self.1 + other.1)
    }
}

fn main() {}

Output when running cargo clippy on the above example:

warning: this could be a const_fn
 --> src/main.rs:3:21
  |
3 | #[derive(PartialEq, Eq)]
  |                     ^^
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::missing_const_for_fn)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn

warning: this could be a const_fn
  --> src/main.rs:9:5
   |
9  | /     fn add(self, other: Self) -> Self {
10 | |         Point(self.0 + other.0, self.1 + other.1)
11 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn

added a commit that references this issue on Mar 5, 2019
a8f61e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @phansch@jhpratt

      Issue actions

        Trait functions cannot be `const` · Issue #3841 · rust-lang/rust-clippy