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 for unnecessary_filter_map #4433

Closed
emabee opened this issue Aug 22, 2019 · 7 comments · Fixed by #8479
Closed

False positive for unnecessary_filter_map #4433

emabee opened this issue Aug 22, 2019 · 7 comments · Fixed by #8479
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 I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@emabee
Copy link

emabee commented Aug 22, 2019

cargo clippy -V
clippy 0.0.212 (e3cb40e 2019-06-25)

A warning is produced from

#[warn(clippy::unnecessary_filter_map)]
let errors: Vec<ServerError> = server_errors
    .into_iter()
    .filter_map(|se| match se.severity() {
        Severity::Warning => {
            self.warnings.push(se);
            None
        }
        _ => Some(se),
    })
    .collect();

The code splits moves some items from one collection into a second. If I'd use filter, as recommended, then I would have to clone the ses, and discard the original instances.

@emabee
Copy link
Author

emabee commented Aug 22, 2019

Sorry for the duplications - I have no clue how this happened!

@phansch
Copy link
Member

phansch commented Aug 22, 2019

It was probably due to the problems GitHub had earlier today, no worries!

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Aug 22, 2019
@MightyPork
Copy link

MightyPork commented Apr 8, 2020

Here is another example from my code, just to add some test-case diversity (yeah it's ugly, refactor tips are very welcome :p)

// "drain_filter"
self.pending_requests = self.pending_requests.drain(..)
    .filter_map(|pending| {
        if pending.expires <= now {
            return None; // Expired, remove
        }

        if pending.group_id == group_id {
            // Matched - reuse strings and remove
            node.send_response(pending.reply_to, pending.token, Value::Null)
                .on_error_warn();
            None
        } else {
            // Keep waiting
            Some(pending)
        }
    })
    .collect();

@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. E-hard Call for participation: This a hard problem and requires more experience or effort to work on and removed E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Apr 8, 2020
@drahnr
Copy link

drahnr commented Apr 30, 2020

Another sample point of an enumartion extraction

            .filter_map(|res| match res {
                Ok(::pgp::packet::Packet::Signature(sig_packet)) => Some(sig_packet),
                _ => None,
            })

@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@BartMassey
Copy link

I just ran into this issue as well. The lint needs to check that all uses in the closure body can be by shared reference.

Another reduced test case:

pub struct S(u8);

impl S {
    pub fn consume(self) {
        println!("yum");
    }
}

pub fn filter_owned() -> impl Iterator<Item = S> {
    (0..10)
        .map(|i| S(i))
        .filter_map(|s| {
            if s.0 & 1 == 0 {
                s.consume();
                None
            } else {
                Some(s)
            }
        })
}

If this lint can't be fixed, then I'd like to see Clippy default to allow: it's quite annoying.

@smoelius
Copy link
Contributor

If this lint can't be fixed, then I'd like to see Clippy default to allow: it's quite annoying.

These are good opportunities to thank the Clippy maintainers. That we're willing to put up with a quirk every so often is a testament to Clippy's profound usefulness!

@BartMassey
Copy link

@smoelius Yeah, Clippy is an amazing tool — I only expect so much of it because I know how good its developers are. 🙂

@bors bors closed this as completed in e511476 Feb 28, 2022
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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
7 participants