Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 23cd3cf

Browse files
committed
Add and apply nullable attributes (dotnet/coreclr#24679)
* Add and apply nullable attributes * Adapt to API review decisions * Address PR feedback Signed-off-by: dotnet-bot <[email protected]>
1 parent f121f06 commit 23cd3cf

File tree

174 files changed

+1121
-860
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+1121
-860
lines changed

src/Common/src/CoreLib/Internal/Win32/RegistryKey.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Buffers;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
1011
using System.Security;
1112

@@ -219,7 +220,8 @@ public unsafe string[] GetValueNames()
219220
return GetValue(name, null);
220221
}
221222

222-
public object? GetValue(string name, object? defaultValue) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
223+
[return: NotNullIfNotNull("defaultValue")]
224+
public object? GetValue(string name, object? defaultValue)
223225
{
224226
object? data = defaultValue;
225227
int type = 0;
@@ -369,7 +371,7 @@ public unsafe string[] GetValueNames()
369371
// make sure the string is null terminated before processing the data
370372
if (blob.Length > 0 && blob[blob.Length - 1] != (char)0)
371373
{
372-
Array.Resize(ref blob!, blob.Length + 1); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
374+
Array.Resize(ref blob!, blob.Length + 1); // TODO-NULLABLE: Remove ! when nullable attributes are respected
373375
}
374376

375377
string[] strings = Array.Empty<string>();
@@ -414,13 +416,13 @@ public unsafe string[] GetValueNames()
414416
{
415417
if (strings.Length == stringsCount)
416418
{
417-
Array.Resize(ref strings!, stringsCount > 0 ? stringsCount * 2 : 4); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
419+
Array.Resize(ref strings!, stringsCount > 0 ? stringsCount * 2 : 4); // TODO-NULLABLE: Remove ! when nullable attributes are respected
418420
}
419-
strings![stringsCount++] = toAdd; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
421+
strings![stringsCount++] = toAdd; // TODO-NULLABLE: Remove ! when nullable attributes are respected
420422
}
421423
}
422424

423-
Array.Resize(ref strings!, stringsCount); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
425+
Array.Resize(ref strings!, stringsCount); // TODO-NULLABLE: Remove ! when nullable attributes are respected
424426
data = strings;
425427
}
426428
break;

src/Common/src/CoreLib/Interop/Unix/Interop.Errors.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ internal string GetErrorMessage()
145145
return Interop.Sys.StrError(RawErrno);
146146
}
147147

148-
#pragma warning disable CS8609 // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/23268
149148
public override string ToString()
150-
#pragma warning restore CS8609
151149
{
152150
return $"RawErrno: {RawErrno} Error: {Error} GetErrorMessage: {GetErrorMessage()}"; // No localization required; text is member names used for debugging purposes
153151
}

src/Common/src/CoreLib/Interop/Unix/System.Native/Interop.MountPoints.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ internal static string[] GetAllMountPoints()
2727
{
2828
if (count == found.Length)
2929
{
30-
Array.Resize(ref found!, count * 2); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
30+
Array.Resize(ref found!, count * 2); // TODO-NULLABLE: Remove ! when nullable attributes are respected
3131
}
3232
found[count++] = Marshal.PtrToStringAnsi((IntPtr)name)!;
3333
});
3434
}
3535

36-
Array.Resize(ref found!, count); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
36+
Array.Resize(ref found!, count); // TODO-NULLABLE: Remove ! when nullable attributes are respected
3737
return found;
3838
}
3939
}

src/Common/src/CoreLib/Interop/Windows/Kernel32/Interop.CreateFile.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using Microsoft.Win32.SafeHandles;
66
using System;
7-
using System.Diagnostics;
87
using System.IO;
98
using System.Runtime.InteropServices;
109

@@ -34,9 +33,8 @@ internal static SafeFileHandle CreateFile(
3433
int dwFlagsAndAttributes,
3534
IntPtr hTemplateFile)
3635
{
37-
string? lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
38-
Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
39-
return CreateFilePrivate(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
36+
lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
37+
return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
4038
}
4139
}
4240
}

src/Common/src/CoreLib/Interop/Windows/Kernel32/Interop.CreateFile2.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ internal static SafeFileHandle CreateFile2(
2626
FileMode dwCreationDisposition,
2727
ref Kernel32.CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams)
2828
{
29-
string? lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
30-
Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
31-
return CreateFile2Private(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams);
29+
lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
30+
return CreateFile2Private(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams);
3231
}
3332
}
3433
}

src/Common/src/CoreLib/Interop/Windows/Kernel32/Interop.FindFirstFileEx.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ internal partial class Kernel32
2121

2222
internal static SafeFindHandle FindFirstFile(string fileName, ref WIN32_FIND_DATA data)
2323
{
24-
string? fileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(fileName);
25-
Debug.Assert(fileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
24+
fileName = PathInternal.EnsureExtendedPrefixIfNeeded(fileName)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
2625

2726
// use FindExInfoBasic since we don't care about short name and it has better perf
28-
return FindFirstFileExPrivate(fileNameWithPrefix, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0);
27+
return FindFirstFileExPrivate(fileName, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0);
2928
}
3029

3130
internal enum FINDEX_INFO_LEVELS : uint

src/Common/src/CoreLib/Interop/Windows/Kernel32/Interop.GetFileAttributesEx.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ internal partial class Kernel32
2121

2222
internal static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
2323
{
24-
string? nameWithExtendedPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(name);
25-
Debug.Assert(nameWithExtendedPrefix != null, "null not expected when non-null is passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
26-
return GetFileAttributesExPrivate(nameWithExtendedPrefix, fileInfoLevel, ref lpFileInformation);
24+
name = PathInternal.EnsureExtendedPrefixIfNeeded(name)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
25+
return GetFileAttributesExPrivate(name, fileInfoLevel, ref lpFileInformation);
2726
}
2827
}
2928
}

src/Common/src/CoreLib/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
<Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs" />
199199
<Compile Include="$(MSBuildThisFileDirectory)System\Delegate.cs" />
200200
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\SuppressMessageAttribute.cs" />
201+
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
201202
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\ConditionalAttribute.cs" />
202203
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Contracts\ContractException.cs" />
203204
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Contracts\ContractFailedEventArgs.cs" />

src/Common/src/CoreLib/System/AppContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static void SetSwitch(string switchName, bool isEnabled)
126126
Interlocked.CompareExchange(ref s_switches, new Dictionary<string, bool>(), null);
127127
}
128128

129-
lock (s_switches!) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
129+
lock (s_switches!) // TODO-NULLABLE: Remove ! when compiler specially-recognizes CompareExchange for nullability
130130
{
131131
s_switches[switchName] = isEnabled;
132132
}

src/Common/src/CoreLib/System/AppDomain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public void SetThreadPrincipal(IPrincipal principal)
409409
(Func<IPrincipal>)mi.CreateDelegate(typeof(Func<IPrincipal>)));
410410
}
411411

412-
principal = s_getUnauthenticatedPrincipal!(); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
412+
principal = s_getUnauthenticatedPrincipal!(); // TODO-NULLABLE: Remove ! when nullable attributes are respected
413413
break;
414414

415415
case PrincipalPolicy.WindowsPrincipal:
@@ -425,7 +425,7 @@ public void SetThreadPrincipal(IPrincipal principal)
425425
(Func<IPrincipal>)mi.CreateDelegate(typeof(Func<IPrincipal>)));
426426
}
427427

428-
principal = s_getWindowsPrincipal!(); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
428+
principal = s_getWindowsPrincipal!(); // TODO-NULLABLE: Remove ! when nullable attributes are respected
429429
break;
430430
}
431431
}

0 commit comments

Comments
 (0)