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

filter_map_identity FN on &Option and other non-identity functions #12501

Open
Centri3 opened this issue Mar 17, 2024 · 4 comments
Open

filter_map_identity FN on &Option and other non-identity functions #12501

Centri3 opened this issue Mar 17, 2024 · 4 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@Centri3
Copy link
Member

Centri3 commented Mar 17, 2024

Summary

While it's technically not an identity function, |&x| x and ||x| x.as_ref()| should be linted for the same reasons as |x| x

Lint Name

clippy::filter_map_identity

Reproducer

I tried this code:

struct NonCopy;

fn main() {
    _ = vec![Some(()), None, Some(())].iter().filter_map(|&x| x);
    _ = vec![Some(NonCopy), None, Some(NonCopy)].iter().filter_map(|x| x.as_ref());
    _ = vec![Some(()), None, Some(())].iter_mut().filter_map(|&mut x| x);
    _ = vec![Some(NonCopy), None, Some(NonCopy)].iter_mut().filter_map(|x| x.as_mut());
}

I expected to see this happen: 4 warnings

Instead, this happened: clippy is silent

Version

cargo 1.78.0-nightly (2fe739fcf 2024-03-15)
release: 1.78.0-nightly
commit-hash: 2fe739fcf16c5bf8c2064ab9d357f4a0e6c8539b
commit-date: 2024-03-15
host: x86_64-pc-windows-msvc
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:Schannel)
os: Windows 10.0.19045 (Windows 10 Home) [64-bit]
@Centri3 Centri3 added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 17, 2024
@Centri3
Copy link
Member Author

Centri3 commented Mar 17, 2024

rustc suggestions when the & or as_ref is missing are really bad, too, it suggests Some(x) which is useless. An issue should be opened about that too

@m-rph
Copy link
Contributor

m-rph commented Mar 24, 2024

@rustbot claim

@m-rph
Copy link
Contributor

m-rph commented Mar 24, 2024

Speaking of, maybe we should also lint .filter_map(|x| Some(x)) if we don't already do that.

@Centri3
Copy link
Member Author

Centri3 commented Mar 24, 2024

We do that already yeah, but the suggestion suggests map instead which is pretty bad. We could change that too if it's Option<Option<T>>

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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

2 participants