Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt at removing explicit #nullable enable in source #44936

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 5 additions & 5 deletions src/Cli/Microsoft.DotNet.Cli.Utils/BlockingMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override int Read(byte[] buffer, int offset, int count)

if (_remaining.Count == 0)
{
byte[] tmp;
byte[]? tmp;
if (!_buffers.TryTake(out tmp, Timeout.Infinite) || tmp.Length == 0)
{
return 0;
Expand All @@ -40,13 +40,13 @@ public override int Read(byte[] buffer, int offset, int count)
if (_remaining.Count <= count)
{
count = _remaining.Count;
Buffer.BlockCopy(_remaining.Array, _remaining.Offset, buffer, offset, count);
_remaining = default(ArraySegment<byte>);
Buffer.BlockCopy(_remaining.Array!, _remaining.Offset, buffer, offset, count);
_remaining = default;
}
else
{
Buffer.BlockCopy(_remaining.Array, _remaining.Offset, buffer, offset, count);
_remaining = new ArraySegment<byte>(_remaining.Array, _remaining.Offset + count, _remaining.Count - count);
Buffer.BlockCopy(_remaining.Array!, _remaining.Offset, buffer, offset, count);
_remaining = new ArraySegment<byte>(_remaining.Array!, _remaining.Offset + count, _remaining.Count - count);
}
return count;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BuiltInCommand : ICommand
private readonly IBuiltInCommandEnvironment _environment;
private readonly StreamForwarder _stdOut;
private readonly StreamForwarder _stdErr;
private string _workingDirectory;
private string? _workingDirectory;

public string CommandName { get; }
public string CommandArgs => string.Join(" ", _commandArgs);
Expand Down Expand Up @@ -57,7 +57,7 @@ public CommandResult Execute()

if (!string.IsNullOrEmpty(_workingDirectory))
{
_environment.SetWorkingDirectory(_workingDirectory);
_environment.SetWorkingDirectory(_workingDirectory!);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! Justification: string.IsNullOrEmpty check

}

var taskOut = _stdOut.BeginRead(new StreamReader(outStream));
Expand Down Expand Up @@ -163,17 +163,17 @@ public ICommand CaptureStdOut()
return this;
}

public ICommand EnvironmentVariable(string name, string value)
public ICommand EnvironmentVariable(string name, string? value)
{
throw new NotImplementedException();
}

public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
public ICommand ForwardStdErr(TextWriter? to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotImplementedException();
}

public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
public ICommand ForwardStdOut(TextWriter? to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotImplementedException();
}
Expand Down
32 changes: 16 additions & 16 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class Command : ICommand
{
private readonly Process _process;

private StreamForwarder _stdOut;
private StreamForwarder? _stdOut;

private StreamForwarder _stdErr;
private StreamForwarder? _stdErr;

private bool _running = false;

private bool _trimTrailingNewlines = false;

public Command(Process process, bool trimtrailingNewlines = false)
public Command(Process? process, bool trimtrailingNewlines = false)
{
_trimTrailingNewlines = trimtrailingNewlines;
_process = process ?? throw new ArgumentNullException(nameof(process));
Expand All @@ -28,7 +28,7 @@ public CommandResult Execute()
{
return Execute(null);
}
public CommandResult Execute(Action<Process> processStarted)
public CommandResult Execute(Action<Process>? processStarted)
{
Reporter.Verbose.WriteLine(string.Format(
LocalizableStrings.RunningFileNameArguments,
Expand Down Expand Up @@ -98,7 +98,7 @@ public ICommand WorkingDirectory(string projectDirectory)
return this;
}

public ICommand EnvironmentVariable(string name, string value)
public ICommand EnvironmentVariable(string name, string? value)
{
_process.StartInfo.Environment[name] = value;
return this;
Expand All @@ -108,19 +108,19 @@ public ICommand CaptureStdOut()
{
ThrowIfRunning();
EnsureStdOut();
_stdOut.Capture(_trimTrailingNewlines);
_stdOut?.Capture(_trimTrailingNewlines);
return this;
}

public ICommand CaptureStdErr()
{
ThrowIfRunning();
EnsureStdErr();
_stdErr.Capture(_trimTrailingNewlines);
_stdErr?.Capture(_trimTrailingNewlines);
return this;
}

public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
public ICommand ForwardStdOut(TextWriter? to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
ThrowIfRunning();
if (!onlyIfVerbose || CommandLoggingContext.IsVerbose)
Expand All @@ -129,18 +129,18 @@ public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false,

if (to == null)
{
_stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine);
_stdOut?.ForwardTo(writeLine: Reporter.Output.WriteLine);
EnvironmentVariable(CommandLoggingContext.Variables.AnsiPassThru, ansiPassThrough.ToString());
}
else
{
_stdOut.ForwardTo(writeLine: to.WriteLine);
_stdOut?.ForwardTo(writeLine: to.WriteLine);
}
}
return this;
}

public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
public ICommand ForwardStdErr(TextWriter? to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
ThrowIfRunning();
if (!onlyIfVerbose || CommandLoggingContext.IsVerbose)
Expand All @@ -149,12 +149,12 @@ public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false,

if (to == null)
{
_stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine);
_stdErr?.ForwardTo(writeLine: Reporter.Error.WriteLine);
EnvironmentVariable(CommandLoggingContext.Variables.AnsiPassThru, ansiPassThrough.ToString());
}
else
{
_stdErr.ForwardTo(writeLine: to.WriteLine);
_stdErr?.ForwardTo(writeLine: to.WriteLine);
}
}
return this;
Expand All @@ -165,7 +165,7 @@ public ICommand OnOutputLine(Action<string> handler)
ThrowIfRunning();
EnsureStdOut();

_stdOut.ForwardTo(writeLine: handler);
_stdOut?.ForwardTo(writeLine: handler);
return this;
}

Expand All @@ -174,7 +174,7 @@ public ICommand OnErrorLine(Action<string> handler)
ThrowIfRunning();
EnsureStdErr();

_stdErr.ForwardTo(writeLine: handler);
_stdErr?.ForwardTo(writeLine: handler);
return this;
}

Expand Down Expand Up @@ -210,7 +210,7 @@ private void EnsureStdErr()
_process.StartInfo.RedirectStandardError = true;
}

private void ThrowIfRunning([CallerMemberName] string memberName = null)
private void ThrowIfRunning([CallerMemberName] string? memberName = null)
{
if (_running)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Cli/Microsoft.DotNet.Cli.Utils/CommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public struct CommandResult

public ProcessStartInfo StartInfo { get; }
public int ExitCode { get; }
public string StdOut { get; }
public string StdErr { get; }
public string? StdOut { get; }
public string? StdErr { get; }

public CommandResult(ProcessStartInfo startInfo, int exitCode, string stdOut, string stdErr)
public CommandResult(ProcessStartInfo startInfo, int exitCode, string? stdOut, string? stdErr)
{
StartInfo = startInfo;
ExitCode = exitCode;
Expand Down
8 changes: 4 additions & 4 deletions src/Cli/Microsoft.DotNet.Cli.Utils/DangerousFileDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static class InternetSecurity
private const uint ZoneInternet = 3;
private const uint ZoneUntrusted = 4;
private const int REGDB_E_CLASSNOTREG = unchecked((int)0x80040154);
private static IInternetSecurityManager internetSecurityManager = null;
private static IInternetSecurityManager? internetSecurityManager = null;

#if NETCOREAPP
[SupportedOSPlatform("windows")]
Expand All @@ -38,11 +38,11 @@ public static bool IsDangerous(string filename)
// First check the zone, if they are not an untrusted zone, they aren't dangerous
if (internetSecurityManager == null)
{
Type iismType = Type.GetTypeFromCLSID(new Guid(CLSID_InternetSecurityManager));
internetSecurityManager = (IInternetSecurityManager)Activator.CreateInstance(iismType);
Type iismType = Type.GetTypeFromCLSID(new Guid(CLSID_InternetSecurityManager))!;
internetSecurityManager = Activator.CreateInstance(iismType!) as IInternetSecurityManager;
}
int zone = 0;
internetSecurityManager.MapUrlToZone(Path.GetFullPath(filename), out zone, 0);
internetSecurityManager?.MapUrlToZone(Path.GetFullPath(filename), out zone, 0);
if (zone < ZoneInternet)
{
return false;
Expand Down
20 changes: 10 additions & 10 deletions src/Cli/Microsoft.DotNet.Cli.Utils/DependencyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public sealed class DependencyProvider
/// <summary>
/// The product code of the MSI associated with the dependency provider.
/// </summary>
public string ProductCode => GetProductCode();
public string? ProductCode => GetProductCode();

/// <summary>
/// The path of the provider key, relative to the <see cref="BaseKey"/>.
Expand Down Expand Up @@ -143,12 +143,12 @@ public void RemoveDependent(string dependent, bool removeProvider)
throw new ArgumentException($"{nameof(dependent)} cannot be empty.");
}

using RegistryKey dependentsKey = BaseKey.OpenSubKey(DependentsKeyPath, writable: true);
using RegistryKey? dependentsKey = BaseKey.OpenSubKey(DependentsKeyPath, writable: true);
dependentsKey?.DeleteSubKeyTree(dependent);

if ((removeProvider) && (Dependents.Count() == 0))
{
using RegistryKey providerKey = BaseKey.OpenSubKey(DependenciesKeyRelativePath, writable: true);
using RegistryKey? providerKey = BaseKey.OpenSubKey(DependenciesKeyRelativePath, writable: true);
providerKey?.DeleteSubKeyTree(ProviderKeyName);
}
}
Expand All @@ -159,7 +159,7 @@ public void RemoveDependent(string dependent, bool removeProvider)
/// <returns>All dependents of the provider key.</returns>
private IEnumerable<string> GetDependents()
{
using RegistryKey dependentsKey = BaseKey.OpenSubKey(DependentsKeyPath);
using RegistryKey? dependentsKey = BaseKey.OpenSubKey(DependentsKeyPath);

return dependentsKey?.GetSubKeyNames() ?? Enumerable.Empty<string>();
}
Expand All @@ -169,22 +169,22 @@ private IEnumerable<string> GetDependents()
/// value.
/// </summary>
/// <returns>The ProductCode associated with this dependency provider or <see langword="null"/> if it does not exist.</returns>
private string GetProductCode()
private string? GetProductCode()
{
using RegistryKey providerKey = BaseKey.OpenSubKey(ProviderKeyPath);
using RegistryKey? providerKey = BaseKey.OpenSubKey(ProviderKeyPath);
return providerKey?.GetValue(null) as string ?? null;
}

public override string ToString() => ProviderKeyName;

public static DependencyProvider GetFromProductCode(string productCode, bool allUsers = true)
public static DependencyProvider? GetFromProductCode(string productCode, bool allUsers = true)
{
var baseKey = allUsers ? Registry.LocalMachine : Registry.CurrentUser;
using RegistryKey dependenciesKey = baseKey.OpenSubKey(DependenciesKeyRelativePath);
using RegistryKey? dependenciesKey = baseKey.OpenSubKey(DependenciesKeyRelativePath);

foreach (var providerKeyName in dependenciesKey.GetSubKeyNames())
foreach (var providerKeyName in dependenciesKey!.GetSubKeyNames())
{
using RegistryKey providerKey = dependenciesKey.OpenSubKey(providerKeyName);
using RegistryKey? providerKey = dependenciesKey.OpenSubKey(providerKeyName);
var thisProductCode = providerKey?.GetValue(null) as string ?? null;
if (string.Equals(thisProductCode, productCode, StringComparison.OrdinalIgnoreCase))
{
Expand Down
11 changes: 4 additions & 7 deletions src/Cli/Microsoft.DotNet.Cli.Utils/DotDefaultPathCorrector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@ public static class DotDefaultPathCorrector
public static void Correct()
{
var pathEditor = new WindowsRegistryEnvironmentPathEditor();
var dotDefaultPath =
pathEditor.Get(
SdkEnvironmentVariableTarget.DotDefault);
var dotDefaultPath = pathEditor.Get(SdkEnvironmentVariableTarget.DotDefault);
if (NeedCorrection(dotDefaultPath, out var correctedPath))
{
pathEditor.Set(correctedPath,
SdkEnvironmentVariableTarget.DotDefault);
pathEditor.Set(correctedPath, SdkEnvironmentVariableTarget.DotDefault);
}
}

internal static bool NeedCorrection(string existingPath, out string correctedPath)
internal static bool NeedCorrection(string? existingPath, out string correctedPath)
{
correctedPath = string.Empty;
if (string.IsNullOrWhiteSpace(existingPath))
{
return false;
}

IEnumerable<string> paths = existingPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<string> paths = existingPath!.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! Justification: string.IsNullOrEmpty check


var inCorrectToolsPaths =
paths.Where(p => p.EndsWith(DotnetToolsSuffix, StringComparison.OrdinalIgnoreCase));
Expand Down
10 changes: 5 additions & 5 deletions src/Cli/Microsoft.DotNet.Cli.Utils/DotnetVersionFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ internal class DotnetVersionFile
{
public bool Exists { get; init; }

public string CommitSha { get; init; }
public string? CommitSha { get; init; }

public string BuildNumber { get; init; }
public string? BuildNumber { get; init; }

public string FullNugetVersion { get; init; }
public string? FullNugetVersion { get; init; }

public string SdkFeatureBand { get; init; }
public string? SdkFeatureBand { get; init; }

/// <summary>
/// The runtime identifier (rid) that this CLI was built for.
Expand All @@ -24,7 +24,7 @@ internal class DotnetVersionFile
/// RuntimeInformation.RuntimeIdentifier may be for a new version of the OS that
/// doesn't have full support yet.
/// </remarks>
public string BuildRid { get; init; }
public string? BuildRid { get; init; }

public DotnetVersionFile(string versionFilePath)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public static IEnumerable<string> ExecutableExtensions
}
}

public static string GetCommandPath(string commandName, params string[] extensions)
public static string? GetCommandPath(string commandName, params string[] extensions)
{
return _environment.GetCommandPath(commandName, extensions);
}

public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
public static string? GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
{
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}

public static string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
public static string? GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
{
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}
Expand All @@ -40,7 +40,7 @@ public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue =
return _environment.GetEnvironmentVariableAsNullableInt(name);
}

public static string GetEnvironmentVariable(string name)
public static string? GetEnvironmentVariable(string name)
{
return _environment.GetEnvironmentVariable(name);
}
Expand Down
Loading