Skip to content

False positive with redundant_closure and Deref #3942

Closed
@sfackler

Description

@sfackler

The new Clippy in 1.34 makes a suggestion that doesn't work:

use std::ops::Deref;

struct Foo;

impl Deref for Foo {
    type Target = str;

    fn deref(&self) -> &str {
        "hi"
    }
}

fn main() {
    let _ = [Foo].iter().map(|s| s.to_string()).collect::<Vec<_>>();;
}
warning: redundant closure found
  --> src/main.rs:14:30
   |
14 |     let _ = [Foo].iter().map(|s| s.to_string()).collect::<Vec<_>>();
   |                              ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `std::string::ToString::to_string`
   |
   = note: #[warn(clippy::redundant_closure)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

You can't remove the closure since ToString::to_string won't deref the Foo to a &str.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions