Repository: tokio-rs/tokio
File: tokio/src/sync/notify.rs (Rust)

Code before:
                                );

                                match res {
                                    Ok(_) => {
                                        // Acquired the notification
                                        *state = Done;
                                        return Poll::Ready(());
                                    }
                                    Err(actual) => {
                                        assert_eq!(get_state(actual), EMPTY);
                                        curr = actual;
                                    }
                                }
                            }
                            _ => unreachable!(),
                        }
                    }

                    let mut old_waker = None;
                    if waker.is_some() {
                        // Safety: called while locked.
                        //
                        // This code makes the following assignment, but it
                        // stores the old waker in `old_waker` so that we can
                        // drop it after releasing the mutex.
                        //
                        // (*waiter.get()).waker = waker;
                        unsafe {
                            old_waker = std::mem::replace(&mut (*waiter.get()).waker, waker);
                        }
                    }

                    // Insert the waiter into the linked list
                    //
                    // safety: pointers from `UnsafeCell` are never null.
                    waiters.push_front(unsafe { NonNull::new_unchecked(waiter.get()) });

                    *state = Waiting;

                    drop(waiters);
                    drop(old_waker);

                    return Poll::Pending;
                }
                Waiting => {
                    // Currently in the "Waiting" state, implying the caller has a waiter stored in
                    // a waiter list (guarded by `notify.waiters`). In order to access the waker
                    // fields, we must acquire the lock.

                    let mut waiters = notify.waiters.lock();


Review comment (suggestion):
`old_waker` is always `None` here, as this is the first place we store a waker and from here we always transition to `Waiting` with no return.

Code after:
                                );

                                match res {
                                    Ok(_) => {
                                        // Acquired the notification
                                        *state = Done;
                                        return Poll::Ready(());
                                    }
                                    Err(actual) => {
                                        assert_eq!(get_state(actual), EMPTY);
                                        curr = actual;
                                    }
                                }
                            }
                            _ => unreachable!(),
                        }
                    }

                    let mut old_waker = None;
                    if waker.is_some() {
                        // Safety: called while locked.
                        //
                        // The use of `old_waiter` here is not necessary, as the field is always
                        // None when we reach this line.
                        unsafe {
                            old_waker = std::mem::replace(&mut (*waiter.get()).waker, waker);
                        }
                    }

                    // Insert the waiter into the linked list
                    //
                    // safety: pointers from `UnsafeCell` are never null.
                    waiters.push_front(unsafe { NonNull::new_unchecked(waiter.get()) });

                    *state = Waiting;

                    drop(waiters);
                    drop(old_waker);

                    return Poll::Pending;
                }
                Waiting => {
                    // Currently in the "Waiting" state, implying the caller has a waiter stored in
                    // a waiter list (guarded by `notify.waiters`). In order to access the waker
                    // fields, we must acquire the lock.

                    let mut waiters = notify.waiters.lock();

                    // Load the state with the lock held.
                    let curr = notify.state.load(SeqCst);