Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs (C#)

Code before:
                        else
                        {
                            // Name surrogate reparse point, don't recurse, simply remove the directory.
                            // If a volume mount point, we have to delete the mount point first.
                            // Note that IO_REPARSE_TAG_MOUNT_POINT is used for both volume mount points
                            // and directory junctions. DeleteVolumeMountPoint only works for volume mount
                            // points; for directory junctions, RemoveDirectory handles removal directly.
                            Exception? mountPointException = null;
                            if (findData.dwReserved0 == Interop.Kernel32.IOReparseOptions.IO_REPARSE_TAG_MOUNT_POINT)
                            {
                                // Mount point. Unmount using full path plus a trailing '\'.
                                // (Note: This doesn't remove the underlying directory)
                                string mountPoint = Path.Join(fullPath, fileName, PathInternal.DirectorySeparatorCharAsString);
                                if (!Interop.Kernel32.DeleteVolumeMountPoint(mountPoint))
                                {
                                    errorCode = Marshal.GetLastPInvokeError();
                                    if (errorCode != Interop.Errors.ERROR_SUCCESS &&
                                        errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND)
                                    {
                                        mountPointException = Win32Marshal.GetExceptionForWin32Error(errorCode, fileName);
                                    }
                                }
                            }

                            // Note that RemoveDirectory on a symbolic link will remove the link itself.
                            if (!Interop.Kernel32.RemoveDirectory(Path.Combine(fullPath, fileName)))
                            {
                                if (exception == null)
                                {
                                    errorCode = Marshal.GetLastPInvokeError();
                                    if (errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND)
                                    {
                                        // For a true volume mount point, use its error (it indicates why the
                                        // unmount step failed). If this is a directory junction, RemoveDirectory
                                        // succeeds and this code path is not reached.
                                        exception = mountPointException ?? Win32Marshal.GetExceptionForWin32Error(errorCode, fileName);
                                    }
                                }
                            }
                            // If RemoveDirectory succeeded, mountPointException is discarded. This correctly
                            // handles directory junctions: DeleteVolumeMountPoint fails for them (since they
                            // are not volume mount points), but RemoveDirectory removes them successfully.
                        }
                    }
                } while (Interop.Kernel32.FindNextFile(handle, ref findData));

                if (exception != null)
                    throw exception;

                errorCode = Marshal.GetLastPInvokeError();
                if (errorCode != Interop.Errors.ERROR_SUCCESS && errorCode != Interop.Errors.ERROR_NO_MORE_FILES)

Review comment (suggestion):
This should be reverted for simplification, and then you can remove `if (exception == null)`.

```suggestion
                            if (!Interop.Kernel32.RemoveDirectory(Path.Combine(fullPath, fileName)) && exception == null)
```


Code after:
                        else
                        {
                            // Name surrogate reparse point, don't recurse, simply remove the directory.
                            // If a volume mount point, we have to delete the mount point first.
                            // Note that IO_REPARSE_TAG_MOUNT_POINT is used for both volume mount points
                            // and directory junctions. DeleteVolumeMountPoint only works for volume mount
                            // points; for directory junctions, RemoveDirectory handles removal directly.
                            Exception? mountPointException = null;
                            if (findData.dwReserved0 == Interop.Kernel32.IOReparseOptions.IO_REPARSE_TAG_MOUNT_POINT)
                            {
                                // Mount point. Unmount using full path plus a trailing '\'.
                                // (Note: This doesn't remove the underlying directory)
                                string mountPoint = Path.Join(fullPath, fileName, PathInternal.DirectorySeparatorCharAsString);
                                if (!Interop.Kernel32.DeleteVolumeMountPoint(mountPoint))
                                {
                                    errorCode = Marshal.GetLastPInvokeError();
                                    if (errorCode != Interop.Errors.ERROR_SUCCESS &&
                                        errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND)
                                    {
                                        mountPointException = Win32Marshal.GetExceptionForWin32Error(errorCode, fileName);
                                    }
                                }
                            }

                            // Note that RemoveDirectory on a symbolic link will remove the link itself.
                            if (!Interop.Kernel32.RemoveDirectory(Path.Combine(fullPath, fileName)) && exception == null)
                            {
                                errorCode = Marshal.GetLastPInvokeError();
                                if (errorCode != Interop.Errors.ERROR_PATH_NOT_FOUND)
                                {
                                    // For a true volume mount point, use its error (it indicates why the
                                    // unmount step failed). If this is a directory junction, RemoveDirectory
                                    // succeeds and this code path is not reached.
                                    exception = mountPointException ?? Win32Marshal.GetExceptionForWin32Error(errorCode, fileName);
                                }
                            }
                            // If RemoveDirectory succeeded, mountPointException is discarded. This correctly
                            // handles directory junctions: DeleteVolumeMountPoint fails for them (since they
                            // are not volume mount points), but RemoveDirectory removes them successfully.
                        }
                    }
                } while (Interop.Kernel32.FindNextFile(handle, ref findData));

                if (exception != null)
                    throw exception;

                errorCode = Marshal.GetLastPInvokeError();
                if (errorCode != Interop.Errors.ERROR_SUCCESS && errorCode != Interop.Errors.ERROR_NO_MORE_FILES)
                    throw Win32Marshal.GetExceptionForWin32Error(errorCode, fullPath);
            }