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

Trait functions cannot be const #3841

Closed
jhpratt opened this issue Mar 4, 2019 · 2 comments · Fixed by #3844
Closed

Trait functions cannot be const #3841

jhpratt opened this issue Mar 4, 2019 · 2 comments · Fixed by #3844
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@jhpratt
Copy link
Member

jhpratt commented Mar 4, 2019

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.

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Mar 4, 2019
@phansch
Copy link
Member

phansch commented Mar 4, 2019

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

@jhpratt
Copy link
Member Author

jhpratt commented Mar 4, 2019

@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

phansch added a commit to phansch/rust-clippy that referenced this issue Mar 4, 2019
As reported in rust-lang#3841. Only fixes the part where it triggers on the
`derive`.
bors added a commit that referenced this issue Mar 5, 2019
Fix two missing_const_for_fn false positives

Fixes #3841

* Fixes false positive in external macros
* Fixes false positive when implement trait methods
@bors bors closed this as completed in #3844 Mar 5, 2019
Manishearth pushed a commit that referenced this issue Apr 19, 2019
As reported in #3841. Only fixes the part where it triggers on the
`derive`.
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants