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

Clippy fails to auto-fix search_is_some issue when .then_some() is used #11910

Closed
andrewswait opened this issue Dec 2, 2023 · 1 comment · Fixed by #12094
Closed

Clippy fails to auto-fix search_is_some issue when .then_some() is used #11910

andrewswait opened this issue Dec 2, 2023 · 1 comment · Fixed by #12094
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@andrewswait
Copy link

andrewswait commented Dec 2, 2023

Summary

When clippy attempted to auto-fix an instance of search_is_some, there was a compiler error.

The use of .any() is followed by .then_some(...) so it needs to add parentheses around the part it's negating with ! rather than attempting to negate the entire expression (you can't 'negate' Option<T>).

Reproducer

I tried this code:

impl Game {
    pub fn is_valid(&self, max_red: u32, max_green: u32, max_blue: u32) -> Option<u32> {
        self.turns
            .iter()
            .find(|t| !t.is_valid(max_green, max_blue, max_red))
            .is_none()
            .then_some(self.id)
    }
}

I expected to see this happen:

impl Game {
    pub fn is_valid(&self, max_red: u32, max_green: u32, max_blue: u32) -> Option<u32> {
        (!self
            .turns
            .iter()
            .any(|t| !t.is_valid(max_green, max_blue, max_red)))
        .then_some(self.id)
    }
}

Instead, this happened:

The following errors were reported:
error[E0600]: cannot apply unary operator `!` to type `std::option::Option<u32>`
  --> src/day2/part1.rs:80:9
   |
80 | /         !self.turns
81 | |             .iter().any(|t| !t.is_valid(max_green, max_blue, max_red))
82 | |             .then_some(self.id)
   | |_______________________________^ cannot apply unary operator `!`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0600`.

Version

rustc 1.76.0-nightly (8c2b57721 2023-12-01)
binary: rustc
commit-hash: 8c2b57721728233e074db69d93517614de338055
commit-date: 2023-12-01
host: x86_64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.5

Additional Labels

@rustbot labels +I-suggestion-causes-error

@andrewswait andrewswait added the C-bug Category: Clippy is not doing the correct thing label Dec 2, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 5, 2023
@yuxqiu
Copy link
Contributor

yuxqiu commented Jan 4, 2024

@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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants