Repository: neovim/neovim
File: src/nvim/buffer.c (C)

Code before:

  if (!bufref_valid(&bufref)) {
    // Autocommands may have deleted the buffer.
    return false;
  }
  // autocmds may abort script processing.
  if (!ignore_abort && aborting()) {
    return false;
  }

  // It's possible that autocommands change curbuf to the one being deleted.
  // This might cause the previous curbuf to be deleted unexpectedly.  But
  // in some cases it's OK to delete the curbuf, because a new one is
  // obtained anyway.  Therefore only return if curbuf changed to the
  // deleted buffer.
  if (buf == curbuf && !is_curbuf) {
    return false;
  }

  bool clear_w_buf = false;
  if (win != NULL  // Avoid bogus clang warning.
      && win_valid_any_tab(win)
      && win->w_buffer == buf) {
    /// Defer clearing w_buffer until after operations that may invoke dict
    /// watchers (e.g., buf_clear_file()), so callers like tabpagebuflist()
    /// never see a window in the winlist with a NULL buffer.
    clear_w_buf = true;
  }

  // Autocommands may have opened or closed windows for this buffer.
  // Decrement the count for the close we do here.
  if (buf->b_nwindows > 0) {
    buf->b_nwindows--;
  }

  // Remove the buffer from the list.
  if (wipe_buf) {
    if (clear_w_buf) {
      win->w_buffer = NULL;
    }
    // Do not wipe out the buffer if it is used in a window.
    if (buf->b_nwindows > 0) {
      return true;
    }
    FOR_ALL_TAB_WINDOWS(tp, wp) {
      mark_forget_file(wp, buf->b_fnum);
    }
    if (buf->b_sfname != buf->b_ffname) {
      XFREE_CLEAR(buf->b_sfname);
    } else {
      buf->b_sfname = NULL;

Review comment (suggestion):
```suggestion
    // Defer clearing w_buffer until after operations that may invoke dict
    // watchers (e.g., buf_clear_file()), so callers like tabpagebuflist()
    // never see a window in the winlist with a NULL buffer.
```

Code after:

  if (!bufref_valid(&bufref)) {
    // Autocommands may have deleted the buffer.
    return false;
  }
  // autocmds may abort script processing.
  if (!ignore_abort && aborting()) {
    return false;
  }

  // It's possible that autocommands change curbuf to the one being deleted.
  // This might cause the previous curbuf to be deleted unexpectedly.  But
  // in some cases it's OK to delete the curbuf, because a new one is
  // obtained anyway.  Therefore only return if curbuf changed to the
  // deleted buffer.
  if (buf == curbuf && !is_curbuf) {
    return false;
  }

  bool clear_w_buf = false;
  if (win != NULL  // Avoid bogus clang warning.
      && win_valid_any_tab(win)
      && win->w_buffer == buf) {
    // Defer clearing w_buffer until after operations that may invoke dict
    // watchers (e.g., buf_clear_file()), so callers like tabpagebuflist()
    // never see a window in the winlist with a NULL buffer.
    clear_w_buf = true;
  }

  // Autocommands may have opened or closed windows for this buffer.
  // Decrement the count for the close we do here.
  if (buf->b_nwindows > 0) {
    buf->b_nwindows--;
  }

  // Remove the buffer from the list.
  if (wipe_buf) {
    if (clear_w_buf) {
      win->w_buffer = NULL;
    }
    // Do not wipe out the buffer if it is used in a window.
    if (buf->b_nwindows > 0) {
      return true;
    }
    FOR_ALL_TAB_WINDOWS(tp, wp) {
      mark_forget_file(wp, buf->b_fnum);
    }
    if (buf->b_sfname != buf->b_ffname) {
      XFREE_CLEAR(buf->b_sfname);
    } else {
      buf->b_sfname = NULL;