Skip to content

Commit

Permalink
Add and apply nullable attributes (dotnet/coreclr#24679)
Browse files Browse the repository at this point in the history
* Add and apply nullable attributes

* Adapt to API review decisions

* Address PR feedback

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
stephentoub authored and dotnet-bot committed May 28, 2019
1 parent f158ae9 commit 233503e
Show file tree
Hide file tree
Showing 174 changed files with 1,121 additions and 860 deletions.
12 changes: 7 additions & 5 deletions src/System.Private.CoreLib/shared/Internal/Win32/RegistryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security;

Expand Down Expand Up @@ -219,7 +220,8 @@ public unsafe string[] GetValueNames()
return GetValue(name, null);
}

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

string[] strings = Array.Empty<string>();
Expand Down Expand Up @@ -414,13 +416,13 @@ public unsafe string[] GetValueNames()
{
if (strings.Length == stringsCount)
{
Array.Resize(ref strings!, stringsCount > 0 ? stringsCount * 2 : 4); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Array.Resize(ref strings!, stringsCount > 0 ? stringsCount * 2 : 4); // TODO-NULLABLE: Remove ! when nullable attributes are respected
}
strings![stringsCount++] = toAdd; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
strings![stringsCount++] = toAdd; // TODO-NULLABLE: Remove ! when nullable attributes are respected
}
}

Array.Resize(ref strings!, stringsCount); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Array.Resize(ref strings!, stringsCount); // TODO-NULLABLE: Remove ! when nullable attributes are respected
data = strings;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ internal string GetErrorMessage()
return Interop.Sys.StrError(RawErrno);
}

#pragma warning disable CS8609 // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/23268
public override string ToString()
#pragma warning restore CS8609
{
return $"RawErrno: {RawErrno} Error: {Error} GetErrorMessage: {GetErrorMessage()}"; // No localization required; text is member names used for debugging purposes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ internal static string[] GetAllMountPoints()
{
if (count == found.Length)
{
Array.Resize(ref found!, count * 2); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Array.Resize(ref found!, count * 2); // TODO-NULLABLE: Remove ! when nullable attributes are respected
}
found[count++] = Marshal.PtrToStringAnsi((IntPtr)name)!;
});
}

Array.Resize(ref found!, count); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
Array.Resize(ref found!, count); // TODO-NULLABLE: Remove ! when nullable attributes are respected
return found;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -34,9 +33,8 @@ internal static SafeFileHandle CreateFile(
int dwFlagsAndAttributes,
IntPtr hTemplateFile)
{
string? lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
return CreateFilePrivate(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ internal static SafeFileHandle CreateFile2(
FileMode dwCreationDisposition,
ref Kernel32.CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams)
{
string? lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
return CreateFile2Private(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams);
lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
return CreateFile2Private(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ internal partial class Kernel32

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

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

internal enum FINDEX_INFO_LEVELS : uint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ internal partial class Kernel32

internal static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
{
string? nameWithExtendedPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(name);
Debug.Assert(nameWithExtendedPrefix != null, "null not expected when non-null is passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
return GetFileAttributesExPrivate(nameWithExtendedPrefix, fileInfoLevel, ref lpFileInformation);
name = PathInternal.EnsureExtendedPrefixIfNeeded(name)!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
return GetFileAttributesExPrivate(name, fileInfoLevel, ref lpFileInformation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Delegate.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\SuppressMessageAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\ConditionalAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Contracts\ContractException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Contracts\ContractFailedEventArgs.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/System.Private.CoreLib/shared/System/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static void SetSwitch(string switchName, bool isEnabled)
Interlocked.CompareExchange(ref s_switches, new Dictionary<string, bool>(), null);
}

lock (s_switches!) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
lock (s_switches!) // TODO-NULLABLE: Remove ! when compiler specially-recognizes CompareExchange for nullability
{
s_switches[switchName] = isEnabled;
}
Expand Down
4 changes: 2 additions & 2 deletions src/System.Private.CoreLib/shared/System/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void SetThreadPrincipal(IPrincipal principal)
(Func<IPrincipal>)mi.CreateDelegate(typeof(Func<IPrincipal>)));
}

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

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

principal = s_getWindowsPrincipal!(); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
principal = s_getWindowsPrincipal!(); // TODO-NULLABLE: Remove ! when nullable attributes are respected
break;
}
}
Expand Down
Loading

0 comments on commit 233503e

Please sign in to comment.