Skip to content

Commit 1c30027

Browse files
authored
[Group 1] Enable nullable annotations for Microsoft.Extensions.FileSystemGlobbing (#57398)
* Annotate src * Annotate ref * Null check for directoryInfo * Nullcheck for PatternMatchingResult.Files * ParentDirectory cannot be null ! need to be addressed * Update PatternBuilder.cs * Use expression body in segments * Update ref * DisableImplicitAssemblyReferences * Fix doc * TestMatchingGroup nonnull * ParentDirectory can be null * Update TestGetHashCodeWithNull * Update FilePatternMatchTests.cs * Update FilePatternMatchTests.cs * Remove TestGetHashCodeWithNull * InMemoryFileInfo.ParentDirectory is not null
1 parent be91858 commit 1c30027

26 files changed

+116
-176
lines changed

src/libraries/Microsoft.Extensions.FileSystemGlobbing/ref/Microsoft.Extensions.FileSystemGlobbing.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ public partial struct FilePatternMatch : System.IEquatable<Microsoft.Extensions.
1010
{
1111
private object _dummy;
1212
private int _dummyPrimitive;
13-
public FilePatternMatch(string path, string stem) { throw null; }
13+
public FilePatternMatch(string path, string? stem) { throw null; }
1414
public readonly string Path { get { throw null; } }
15-
public readonly string Stem { get { throw null; } }
15+
public readonly string? Stem { get { throw null; } }
1616
public bool Equals(Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch other) { throw null; }
17-
public override bool Equals(object obj) { throw null; }
17+
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
1818
public override int GetHashCode() { throw null; }
1919
}
2020
public partial class InMemoryDirectoryInfo : Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase
2121
{
22-
public InMemoryDirectoryInfo(string rootDir, System.Collections.Generic.IEnumerable<string> files) { }
22+
public InMemoryDirectoryInfo(string rootDir, System.Collections.Generic.IEnumerable<string>? files) { }
2323
public override string FullName { get { throw null; } }
2424
public override string Name { get { throw null; } }
25-
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
25+
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
2626
public override System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos() { throw null; }
2727
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string path) { throw null; }
28-
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string path) { throw null; }
28+
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase? GetFile(string path) { throw null; }
2929
}
3030
public partial class Matcher
3131
{
@@ -40,9 +40,9 @@ public static partial class MatcherExtensions
4040
public static void AddExcludePatterns(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, params System.Collections.Generic.IEnumerable<string>[] excludePatternsGroups) { }
4141
public static void AddIncludePatterns(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, params System.Collections.Generic.IEnumerable<string>[] includePatternsGroups) { }
4242
public static System.Collections.Generic.IEnumerable<string> GetResultsInFullPath(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string directoryPath) { throw null; }
43-
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, System.Collections.Generic.IEnumerable<string> files) { throw null; }
43+
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, System.Collections.Generic.IEnumerable<string>? files) { throw null; }
4444
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string file) { throw null; }
45-
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, System.Collections.Generic.IEnumerable<string> files) { throw null; }
45+
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, System.Collections.Generic.IEnumerable<string>? files) { throw null; }
4646
public static Microsoft.Extensions.FileSystemGlobbing.PatternMatchingResult Match(this Microsoft.Extensions.FileSystemGlobbing.Matcher matcher, string rootDir, string file) { throw null; }
4747
}
4848
public partial class PatternMatchingResult
@@ -59,17 +59,17 @@ public abstract partial class DirectoryInfoBase : Microsoft.Extensions.FileSyste
5959
{
6060
protected DirectoryInfoBase() { }
6161
public abstract System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos();
62-
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string path);
63-
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string path);
62+
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? GetDirectory(string path);
63+
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase? GetFile(string path);
6464
}
6565
public partial class DirectoryInfoWrapper : Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase
6666
{
6767
public DirectoryInfoWrapper(System.IO.DirectoryInfo directoryInfo) { }
6868
public override string FullName { get { throw null; } }
6969
public override string Name { get { throw null; } }
70-
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
70+
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
7171
public override System.Collections.Generic.IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos() { throw null; }
72-
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase GetDirectory(string name) { throw null; }
72+
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? GetDirectory(string name) { throw null; }
7373
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase GetFile(string name) { throw null; }
7474
}
7575
public abstract partial class FileInfoBase : Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase
@@ -81,14 +81,14 @@ public partial class FileInfoWrapper : Microsoft.Extensions.FileSystemGlobbing.A
8181
public FileInfoWrapper(System.IO.FileInfo fileInfo) { }
8282
public override string FullName { get { throw null; } }
8383
public override string Name { get { throw null; } }
84-
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get { throw null; } }
84+
public override Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get { throw null; } }
8585
}
8686
public abstract partial class FileSystemInfoBase
8787
{
8888
protected FileSystemInfoBase() { }
8989
public abstract string FullName { get; }
9090
public abstract string Name { get; }
91-
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase ParentDirectory { get; }
91+
public abstract Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase? ParentDirectory { get; }
9292
}
9393
}
9494
namespace Microsoft.Extensions.FileSystemGlobbing.Internal
@@ -133,8 +133,8 @@ public partial struct PatternTestResult
133133
private int _dummyPrimitive;
134134
public static readonly Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Failed;
135135
public readonly bool IsSuccessful { get { throw null; } }
136-
public readonly string Stem { get { throw null; } }
137-
public static Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Success(string stem) { throw null; }
136+
public readonly string? Stem { get { throw null; } }
137+
public static Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Success(string? stem) { throw null; }
138138
}
139139
}
140140
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments
@@ -150,7 +150,7 @@ public partial class LiteralPathSegment : Microsoft.Extensions.FileSystemGlobbin
150150
public LiteralPathSegment(string value, System.StringComparison comparisonType) { }
151151
public bool CanProduceStem { get { throw null; } }
152152
public string Value { get { throw null; } }
153-
public override bool Equals(object obj) { throw null; }
153+
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
154154
public override int GetHashCode() { throw null; }
155155
public bool Match(string value) { throw null; }
156156
}
@@ -195,7 +195,7 @@ public partial struct FrameData
195195
public bool InStem;
196196
public bool IsNotApplicable;
197197
public int SegmentIndex;
198-
public string Stem { get { throw null; } }
198+
public string? Stem { get { throw null; } }
199199
public System.Collections.Generic.IList<string> StemItems { get { throw null; } }
200200
}
201201
}
@@ -232,7 +232,7 @@ public partial struct FrameData
232232
public System.Collections.Generic.IList<Microsoft.Extensions.FileSystemGlobbing.Internal.IPathSegment> SegmentGroup;
233233
public int SegmentGroupIndex;
234234
public int SegmentIndex;
235-
public string Stem { get { throw null; } }
235+
public string? Stem { get { throw null; } }
236236
public System.Collections.Generic.IList<string> StemItems { get { throw null; } }
237237
}
238238
}
@@ -249,12 +249,12 @@ public override void Declare(System.Action<Microsoft.Extensions.FileSystemGlobbi
249249
}
250250
public abstract partial class PatternContext<TFrame> : Microsoft.Extensions.FileSystemGlobbing.Internal.IPatternContext
251251
{
252-
protected TFrame Frame;
252+
protected TFrame? Frame;
253253
protected PatternContext() { }
254254
public virtual void Declare(System.Action<Microsoft.Extensions.FileSystemGlobbing.Internal.IPathSegment, bool> declare) { }
255255
protected bool IsStackEmpty() { throw null; }
256256
public virtual void PopDirectory() { }
257-
protected void PushDataFrame(TFrame frame) { }
257+
protected void PushDataFrame(TFrame? frame) { }
258258
public abstract void PushDirectory(Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase directory);
259259
public abstract bool Test(Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoBase directory);
260260
public abstract Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult Test(Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileInfoBase file);

src/libraries/Microsoft.Extensions.FileSystemGlobbing/ref/Microsoft.Extensions.FileSystemGlobbing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<Nullable>enable</Nullable>
45
</PropertyGroup>
56
<ItemGroup>
67
<Compile Include="Microsoft.Extensions.FileSystemGlobbing.cs" />

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/DirectoryInfoBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public abstract class DirectoryInfoBase : FileSystemInfoBase
2121
/// </summary>
2222
/// <param name="path">The directory name</param>
2323
/// <returns>Instance of <see cref="DirectoryInfoBase" /> even if directory does not exist</returns>
24-
public abstract DirectoryInfoBase GetDirectory(string path);
24+
public abstract DirectoryInfoBase? GetDirectory(string path);
2525

2626
/// <summary>
2727
/// Returns an instance of <see cref="FileInfoBase" /> that represents a file in the directory
2828
/// </summary>
2929
/// <param name="path">The file name</param>
3030
/// <returns>Instance of <see cref="FileInfoBase" /> even if file does not exist</returns>
31-
public abstract FileInfoBase GetFile(string path);
31+
public abstract FileInfoBase? GetFile(string path);
3232
}
3333
}

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/DirectoryInfoWrapper.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
4747

4848
foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
4949
{
50-
var directoryInfo = fileSystemInfo as DirectoryInfo;
51-
if (directoryInfo != null)
50+
if (fileSystemInfo is DirectoryInfo directoryInfo)
5251
{
5352
yield return new DirectoryInfoWrapper(directoryInfo);
5453
}
@@ -68,7 +67,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
6867
/// </remarks>
6968
/// <param name="name">The directory name</param>
7069
/// <returns>The directory</returns>
71-
public override DirectoryInfoBase GetDirectory(string name)
70+
public override DirectoryInfoBase? GetDirectory(string name)
7271
{
7372
bool isParentPath = string.Equals(name, "..", StringComparison.Ordinal);
7473

@@ -120,7 +119,7 @@ public override FileInfoBase GetFile(string name)
120119
/// <remarks>
121120
/// Equals the value of <seealso cref="System.IO.DirectoryInfo.Parent" />.
122121
/// </remarks>
123-
public override DirectoryInfoBase ParentDirectory
124-
=> new DirectoryInfoWrapper(_directoryInfo.Parent);
122+
public override DirectoryInfoBase? ParentDirectory
123+
=> new DirectoryInfoWrapper(_directoryInfo.Parent!);
125124
}
126125
}

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/FileInfoWrapper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.IO;
56

67
namespace Microsoft.Extensions.FileSystemGlobbing.Abstractions
@@ -18,7 +19,7 @@ public class FileInfoWrapper : FileInfoBase
1819
/// <param name="fileInfo">The <see cref="System.IO.FileInfo" /></param>
1920
public FileInfoWrapper(FileInfo fileInfo)
2021
{
21-
_fileInfo = fileInfo;
22+
_fileInfo = fileInfo ?? throw new ArgumentNullException(nameof(fileInfo));
2223
}
2324

2425
/// <summary>
@@ -43,6 +44,7 @@ public FileInfoWrapper(FileInfo fileInfo)
4344
/// <remarks>
4445
/// Equals the value of <see cref="System.IO.FileInfo.Directory" />.
4546
/// </remarks>
46-
public override DirectoryInfoBase ParentDirectory => new DirectoryInfoWrapper(_fileInfo.Directory);
47+
public override DirectoryInfoBase? ParentDirectory
48+
=> new DirectoryInfoWrapper(_fileInfo.Directory!);
4749
}
4850
}

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/FileSystemInfoBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public abstract class FileSystemInfoBase
2121
/// <summary>
2222
/// The parent directory for the current file or directory
2323
/// </summary>
24-
public abstract DirectoryInfoBase ParentDirectory { get; }
24+
public abstract DirectoryInfoBase? ParentDirectory { get; }
2525
}
2626
}

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/FilePatternMatch.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Numerics.Hashing;
67

78
namespace Microsoft.Extensions.FileSystemGlobbing
@@ -27,16 +28,16 @@ public struct FilePatternMatch : IEquatable<FilePatternMatch>
2728
/// If the matcher searched for "src/Project/**/*.cs" and the pattern matcher found "src/Project/Interfaces/IFile.cs",
2829
/// then <see cref="Stem" /> = "Interfaces/IFile.cs" and <see cref="Path" /> = "src/Project/Interfaces/IFile.cs".
2930
/// </remarks>
30-
public string Stem { get; }
31+
public string? Stem { get; }
3132

3233
/// <summary>
3334
/// Initializes new instance of <see cref="FilePatternMatch" />
3435
/// </summary>
3536
/// <param name="path">The path to the file matched, relative to the beginning of the matching search pattern.</param>
3637
/// <param name="stem">The subpath to the file matched, relative to the first wildcard in the matching search pattern.</param>
37-
public FilePatternMatch(string path, string stem)
38+
public FilePatternMatch(string path, string? stem)
3839
{
39-
Path = path;
40+
Path = path ?? throw new ArgumentNullException(nameof(path));
4041
Stem = stem;
4142
}
4243

@@ -56,10 +57,8 @@ public bool Equals(FilePatternMatch other)
5657
/// </summary>
5758
/// <param name="obj">The object to be compared</param>
5859
/// <returns>True when <see cref="Equals(FilePatternMatch)" /></returns>
59-
public override bool Equals(object obj)
60-
{
61-
return Equals((FilePatternMatch) obj);
62-
}
60+
public override bool Equals([NotNullWhen(true)] object? obj) =>
61+
obj is FilePatternMatch match && Equals(match);
6362

6463
/// <summary>
6564
/// Gets a hash for the file pattern match.
@@ -68,7 +67,7 @@ public override bool Equals(object obj)
6867
public override int GetHashCode() =>
6968
HashHelpers.Combine(GetHashCode(Path), GetHashCode(Stem));
7069

71-
private static int GetHashCode(string value) =>
70+
private static int GetHashCode(string? value) =>
7271
value != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(value) : 0;
7372
}
7473
}

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,19 @@ public class InMemoryDirectoryInfo : DirectoryInfoBase
2323
/// </summary>
2424
/// <param name="rootDir">The root directory that this FileSystem will use.</param>
2525
/// <param name="files">Collection of file names. If relative paths <paramref name="rootDir"/> will be prepended to the paths.</param>
26-
public InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files)
26+
public InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files)
2727
: this(rootDir, files, false)
2828
{
2929
}
3030

31-
private InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files, bool normalized)
31+
private InMemoryDirectoryInfo(string rootDir, IEnumerable<string>? files, bool normalized)
3232
{
3333
if (string.IsNullOrEmpty(rootDir))
3434
{
3535
throw new ArgumentNullException(nameof(rootDir));
3636
}
3737

38-
if (files == null)
39-
{
40-
files = new List<string>();
41-
}
38+
files ??= new List<string>();
4239

4340
Name = Path.GetFileName(rootDir);
4441
if (normalized)
@@ -69,13 +66,8 @@ private InMemoryDirectoryInfo(string rootDir, IEnumerable<string> files, bool no
6966
public override string Name { get; }
7067

7168
/// <inheritdoc />
72-
public override DirectoryInfoBase ParentDirectory
73-
{
74-
get
75-
{
76-
return new InMemoryDirectoryInfo(Path.GetDirectoryName(FullName), _files, true);
77-
}
78-
}
69+
public override DirectoryInfoBase? ParentDirectory =>
70+
new InMemoryDirectoryInfo(Path.GetDirectoryName(FullName)!, _files, true);
7971

8072
/// <inheritdoc />
8173
public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
@@ -99,8 +91,7 @@ public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
9991
else
10092
{
10193
string name = file.Substring(0, endSegment);
102-
List<string> list;
103-
if (!dict.TryGetValue(name, out list))
94+
if (!dict.TryGetValue(name, out List<string>? list))
10495
{
10596
dict[name] = new List<string> { file };
10697
}
@@ -145,7 +136,7 @@ public override DirectoryInfoBase GetDirectory(string path)
145136
/// </summary>
146137
/// <param name="path">The filename.</param>
147138
/// <returns>Instance of <see cref="FileInfoBase"/> if the file exists, null otherwise.</returns>
148-
public override FileInfoBase GetFile(string path)
139+
public override FileInfoBase? GetFile(string path)
149140
{
150141
string normPath = Path.GetFullPath(path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
151142
foreach (string file in _files)

src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/InMemoryFileInfo.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public InMemoryFileInfo(string file, InMemoryDirectoryInfo parent)
2121

2222
public override string Name { get; }
2323

24-
public override DirectoryInfoBase ParentDirectory
25-
{
26-
get
27-
{
28-
return _parent;
29-
}
30-
}
24+
public override DirectoryInfoBase ParentDirectory => _parent;
3125
}
3226
}

0 commit comments

Comments
 (0)