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 suggestion of const fn if member of member implements Drop #4979

Open
ArekPiekarz opened this issue Jan 1, 2020 · 1 comment
Open
Labels
C-bug Category: Clippy is not doing the correct thing E-hard Call for participation: This a hard problem and requires more experience or effort to work on

Comments

@ArekPiekarz
Copy link

ArekPiekarz commented Jan 1, 2020

cargo clippy -V: clippy 0.0.212 (c807fbc 2019-12-29)
rustc -V: rustc 1.40.0 (73528e339 2019-12-16)

Summary

A warning "this could be a const_fn" is triggered for a function of a struct that doesn't implement Drop, but on some level of depth contains a member that does implement it. Adding const to such function results in compilation error.

Steps to reproduce

Run the following code with cargo clippy -- -W clippy::missing-const-for-fn:

#![allow(dead_code)]

struct Foo {
    field: String
}

impl Foo {
    fn take(self) -> String {
        self.field
    }
}

fn main() {
}

It results in a warning:

warning: this could be a const_fn
  --> src/main.rs:8:5
   |
8  | /     fn take(self) -> String {
9  | |         self.field
10 | |     }
   | |_____^
   |
   = note: requested on the command line with `-W clippy::missing-const-for-fn`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn

After adding const to the function Foo::take we get an error:

error[E0493]: destructors cannot be evaluated at compile-time
 --> src/main.rs:8:19
  |
8 |     const fn take(self) -> String {
  |                   ^^^^ constant functions cannot evaluate destructors

Note that Foo doesn't implement Drop, its field String also doesn't, but String's field Vec does implement it.
Also note there was already a similar fixed issue, but for structs that implement Drop: #4449

@phansch phansch self-assigned this Jan 2, 2020
@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Jan 2, 2020
phansch added a commit to phansch/rust-clippy that referenced this issue Jan 2, 2020
This fixes some additional false positives with functions or methods
that return types which implement drop. Since `drop` can't be
const-evaluated, these cases can't be made const.

Fixes rust-lang#4979
phansch added a commit to phansch/rust-clippy that referenced this issue Jan 3, 2020
This fixes some additional false positives with functions or methods
that return types which implement drop. Since `drop` can't be
const-evaluated, these cases can't be made const.

Fixes rust-lang#4979
@phansch phansch added the E-hard Call for participation: This a hard problem and requires more experience or effort to work on label Apr 26, 2020
@phansch phansch removed their assignment Jun 1, 2020
@expenses
Copy link

This is now triggering more because more things were made const in rust 1.61.0.

naga has contains the following code:

impl<W: Write> Writer<W> {
    /// Finishes writing and returns the output.
    pub fn finish(self) -> W {
        self.out
    }
...
}

This triggers the lint:

warning: this could be a `const fn`
   --> src/back/msl/writer.rs:650:5
    |
650 | /     pub fn finish(self) -> W {
651 | |         self.out
652 | |     }
    | |_____^
    |
note: the lint level is defined here
   --> src/lib.rs:199:5
    |
199 |     clippy::missing_const_for_fn
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn

but when you actually apply that you get an error:

error[E0493]: destructors cannot be evaluated at compile-time
   --> src/back/msl/writer.rs:650:25
    |
650 |     pub const fn finish(self) -> W {
    |                         ^^^^ constant functions cannot evaluate destructors
651 |         self.out
652 |     }
    |     - value is dropped here

For more information about this error, try `rustc --explain E0493`.

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 E-hard Call for participation: This a hard problem and requires more experience or effort to work on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants