Fix NRE introduced in FileSystemWatcher during nullability annotations (#41315)
authorKrzysztof Wicher <mordotymoja@gmail.com>
Tue, 25 Aug 2020 17:35:31 +0000 (19:35 +0200)
committerGitHub <noreply@github.com>
Tue, 25 Aug 2020 17:35:31 +0000 (10:35 -0700)
* Fix NRE introduced during nullability annotations

* Apply Jeff's suggestions from code review

Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs

index 96bbb65..ed4f163 100644 (file)
@@ -390,7 +390,12 @@ namespace System.IO
                     // of the world, but there's little that can be done about that.)
                     if (directoryEntry.Parent != parent)
                     {
-                        directoryEntry.Parent?.Children!.Remove (directoryEntry);
+                        // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected
+                        if (directoryEntry.Parent != null)
+                        {
+                            directoryEntry.Parent.Children!.Remove(directoryEntry);
+                        }
+
                         directoryEntry.Parent = parent;
                         if (parent != null)
                         {
@@ -443,7 +448,12 @@ namespace System.IO
                 Debug.Assert (_includeSubdirectories);
                 lock (SyncObj)
                 {
-                    directoryEntry.Parent?.Children!.Remove(directoryEntry);
+                    // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected
+                    if (directoryEntry.Parent != null)
+                    {
+                        directoryEntry.Parent.Children!.Remove(directoryEntry);
+                    }
+
                     RemoveWatchedDirectoryUnlocked (directoryEntry, removeInotify);
                 }
             }