rust-lang / rust-lang/rust-clippy

bind_instead_of_map ignores conditionally compiled contents

Open
#8,082 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

bind_instead_of_map seems to ignore conditionally compiled code in a closure passed to Option::and_then(), resulting in a removal of the call when automatically applying fixes.

Lint Name

bind_instead_of_map

Reproducer

cargo clippy --fix made this change:

diff --git a/rustls/src/client/hs.rs b/rustls/src/client/hs.rs
index 821c372..a5e252b 100644
--- a/rustls/src/client/hs.rs
+++ b/rustls/src/client/hs.rs
@@ -68,14 +68,6 @@ fn find_session(
                 true => None,
             }
         })
-        .and_then(|resuming| {
-            #[cfg(feature = "quic")]
-            if cx.common.is_quic() {
-                let params = PayloadU16::read(&mut reader)?;
-                cx.common.quic.params = Some(params.0);
-            }
-            Some(resuming)
-        })
 }
 
 pub(super) fn start_handshake(

Without --fix, it gives me this suggestion:

warning: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`
  --> rustls/src/client/hs.rs:60:5
   |
60 | /     CipherSuite::read_bytes(&value[..2])
61 | |         .and_then(|suite| {
62 | |             persist::ClientSessionValue::read(&mut reader, suite, &config.cipher_suites)
63 | |         })
...  |
77 | |             Some(resuming)
78 | |         })
   | |__________^
   |
   = note: `#[warn(clippy::bind_instead_of_map)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
help: try this
   |
60 ~     CipherSuite::read_bytes(&value[..2])
61 +         .and_then(|suite| {
62 +             persist::ClientSessionValue::read(&mut reader, suite, &config.cipher_suites)
63 +         })
64 +         .and_then(|resuming| {
65 +             let retrieved = persist::Retrieved::new(resuming, TimeBase::now().ok()?);
 ...

Which also ignores the real code:

    CipherSuite::read_bytes(&value[..2])
        .and_then(|suite| {
            persist::ClientSessionValue::read(&mut reader, suite, &config.cipher_suites)
        })
        .and_then(|resuming| {
            let retrieved = persist::Retrieved::new(resuming, TimeBase::now().ok()?);
            match retrieved.has_expired() {
                false => Some(retrieved),
                true => None,
            }
        })
        .and_then(|resuming| {
            #[cfg(feature = "quic")] // <-- NOTE THIS PART
            if cx.common.is_quic() {
                let params = PayloadU16::read(&mut reader)?;
                cx.common.quic.params = Some(params.0);
            }
            Some(resuming)
        })
Version
rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: aarch64-apple-darwin
release: 1.57.0
LLVM version: 13.0.0
Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the bind_instead_of_map lint and reproduce the issue using the closure in rustls/src/client/hs.rs with cargo clippy --fix. Check the conditionally compiled block under the quic feature and verify that applying the fix preserves it and the surrounding closure behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.