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

False positive redundant_closure when using ref pattern in closure params #8460

Closed
cdmistman opened this issue Feb 22, 2022 · 1 comment · Fixed by #8466
Closed

False positive redundant_closure when using ref pattern in closure params #8460

cdmistman opened this issue Feb 22, 2022 · 1 comment · Fixed by #8466
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@cdmistman
Copy link

cdmistman commented Feb 22, 2022

Summary

Related to #3071 which was fixed in #7661, I found a bug when using the ref pattern in a closure's parameters ie |ref foo| bar(foo)

Lint Name

redundant-closure

Reproducer

I tried this code:

struct Foo;

struct Bar;

impl From<&Foo> for Bar {
    fn from(foo: &Foo) -> Self {
        Bar
    }
}

fn main() {
    // this doesn't trigger redundant-closure, as expected
    Some(Foo).map(|foo| Bar::from(&foo));
    // this triggers redundant-closure
    Some(Foo).map(|ref foo| Bar::from(foo));
    // this causes a compiler error since `From<Foo> for Bar` is not implemented
    // Some(Foo).map(Bar::from);
}

Note that the first statement in main triggers expected behavior - there is no redundant closure, and so the lint is not triggered. However, as in the issue referenced above, redundant-closure is triggered for the second statement, with the suggestion being the third (commented-out) statement - this is unexpected behavior, since the suggested code fails to type-check and causes a compiler error.

playground (with allows to isolate lint): https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ec6a0ed2580e8fc73146abf6ada9b384

Version

No response

Additional Labels

No response

@cdmistman cdmistman added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 22, 2022
@tamaroning
Copy link
Contributor

@rustbot claim

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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants