Skip to content

Wrong suggestion of const fn if member of member implements Drop #4979

Open
@ArekPiekarz

Description

@ArekPiekarz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onL-nurseryLint: Currently in the nursery group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions