Skip to content

manual_filter wrongly suggesting filter when match has side-effects #9766

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

Closed
badboy opened this issue Nov 1, 2022 · 2 comments · Fixed by #10091
Closed

manual_filter wrongly suggesting filter when match has side-effects #9766

badboy opened this issue Nov 1, 2022 · 2 comments · Fixed by #10091
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

@badboy
Copy link
Member

badboy commented Nov 1, 2022

Summary

The manual_filter lint's (#9451) suggestion can cause side-effect code to be removed.
While the code can be rewritten to use filter correctly, the current suggestion will change the meaning of the code and is therefore wrong.

Lint Name

manual_filter

Reproducer

I tried this code:

pub fn test() {
    let reason = Some("reason");
    let allowed = vec![
        "reason", "other"
    ];

    let corrected_reason = match reason {
        Some(reason) => {
            if allowed.contains(&reason) {
                Some(reason)
            } else {
                println!("Invalid reason: {reason:?}");
                None
            }
        }
        None => None,
    };

    println!("{corrected_reason:?}")
}

I saw this happen:

❯ cargo +beta clippy
    Checking clippy-manual-filter v0.1.0 (/Users/jer/projects/rust/clippy-manual-filter)
warning: manual implementation of `Option::filter`
  --> src/lib.rs:7:28
   |
7  |       let corrected_reason = match reason {
   |  ____________________________^
8  | |         Some(reason) => {
9  | |             if allowed.contains(&reason) {
10 | |                 Some(reason)
...  |
16 | |         None => None,
17 | |     };
   | |_____^ help: try this: `reason.filter(|&reason| allowed.contains(&reason))`

I expected to see this happen:

Either not triggering that lint or if it triggers suggesting something that doesn't remove the side-effect line (println).

Version

rustc 1.66.0-beta.1 (e080cc5a6 2022-11-01)
binary: rustc
commit-hash: e080cc5a659fb760c0bc561b722a790dad35b5e1
commit-date: 2022-11-01
host: aarch64-apple-darwin
release: 1.66.0-beta.1
LLVM version: 15.0.2

Additional Labels

No response

@badboy badboy 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 Nov 1, 2022
@badboy
Copy link
Member Author

badboy commented Nov 1, 2022

Actually a "trivial" rewrite like match reason.filter(|&reason| ... { would still be wrong.

ericwu17 added a commit to ericwu17/rust-clippy that referenced this issue Dec 16, 2022
The related issue is rust-lang#9766 where the `manual_filter`
lint would remove side effects
@bors bors closed this as completed in 065c6f7 Dec 21, 2022
@jcamiel
Copy link

jcamiel commented Jan 26, 2023

Hi,
With the following code:

pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
    let cacert_file = match get::<String>(matches, "cacert_file") {
        None => None,
        Some(filename) => {
            if !Path::new(&filename).is_file() {
                let message = format!("File {} does not exist", filename);
                return Err(CliError { message });
            } else {
                Some(filename)
            }
        }
    };
    
   // .... end of the functions
}

clippy 0.1.67 (fc594f15 2023-01-24) gives the following hint:

warning: manual implementation of `Option::filter`
   --> packages/hurl/src/cli/options.rs:415:23
    |
415 |       let cacert_file = match get::<String>(matches, "cacert_file") {
    |  _______________________^
416 | |         None => None,
417 | |         Some(filename) => {
418 | |             if !Path::new(&filename).is_file() {
...   |
424 | |         }
425 | |     };
    | |_____^ help: try this: `get::<String>(matches, "cacert_file").filter(|filename| !(!Path::new(&filename).is_file()))`

The proposed fixes seems still erroneous to me as it take not into account the return Err(CliError { message }); side effect.

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