Skip to content

Directory.Exists and File.Exists annotated parameter with [NotNullWhen(true)] #47573

@huoyaoyuan

Description

@huoyaoyuan

Background and Motivation

Found when annotating roslyn. These methods should never return true for null.

Proposed API

namespace System.IO
{
    public static class Directory {
-        public static bool Exists(string? path);
+        public static bool Exists([NotNullWhen(true)] string? path);
    }
    public static class File {
-        public static bool Exists(string? path);
+        public static bool Exists([NotNullWhen(true)] string? path);
    }
}

Usage Examples

string? path = ...
if (Directory.Exists(path))
{
    var subDirectories = Directory.GetDirectories(path); // no warning
}

Alternative Designs

None.

Risks

I couldn't imagine how they can return true for null.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions