From: Stephen Toub Date: Wed, 17 Apr 2019 19:21:35 +0000 (-0400) Subject: Nullable: System.Runtime.Extensions.dll X-Git-Tag: submit/tizen/20210909.063632~11031^2~1633 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2382d5d22859b8db86c8d8ed3fc77026a331344e;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Nullable: System.Runtime.Extensions.dll Commit migrated from https://github.com/dotnet/corefx/commit/9ed871ca7d963c3a03242c172082ed0099213860 --- diff --git a/src/libraries/Common/src/System/HResults.cs b/src/libraries/Common/src/System/HResults.cs index c578712f446..1809df4bd1d 100644 --- a/src/libraries/Common/src/System/HResults.cs +++ b/src/libraries/Common/src/System/HResults.cs @@ -10,8 +10,6 @@ // //===========================================================================*/ -using System; - namespace System { // Note: FACILITY_URT is defined as 0x13 (0x8013xxxx). Within that diff --git a/src/libraries/Common/src/System/SR.cs b/src/libraries/Common/src/System/SR.cs index fcccb944d87..8ab0ffa81d3 100644 --- a/src/libraries/Common/src/System/SR.cs +++ b/src/libraries/Common/src/System/SR.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Resources; using System.Runtime.CompilerServices; @@ -18,14 +19,14 @@ namespace System [MethodImpl(MethodImplOptions.NoInlining)] private static bool UsingResourceKeys() => false; - internal static string GetResourceString(string resourceKey, string defaultString = null) + internal static string GetResourceString(string resourceKey, string? defaultString = null) { if (UsingResourceKeys()) { return defaultString ?? resourceKey; } - string resourceString = null; + string? resourceString = null; try { resourceString = ResourceManager.GetString(resourceKey); @@ -37,10 +38,10 @@ namespace System return defaultString; } - return resourceString; + return resourceString!; // only null if missing resources } - internal static string Format(string resourceFormat, object p1) + internal static string Format(string resourceFormat, object? p1) { if (UsingResourceKeys()) { @@ -50,7 +51,7 @@ namespace System return string.Format(resourceFormat, p1); } - internal static string Format(string resourceFormat, object p1, object p2) + internal static string Format(string resourceFormat, object? p1, object? p2) { if (UsingResourceKeys()) { @@ -60,7 +61,7 @@ namespace System return string.Format(resourceFormat, p1, p2); } - internal static string Format(string resourceFormat, object p1, object p2, object p3) + internal static string Format(string resourceFormat, object? p1, object? p2, object? p3) { if (UsingResourceKeys()) { @@ -70,7 +71,7 @@ namespace System return string.Format(resourceFormat, p1, p2, p3); } - internal static string Format(string resourceFormat, params object[] args) + internal static string Format(string resourceFormat, params object?[]? args) { if (args != null) { @@ -85,7 +86,7 @@ namespace System return resourceFormat; } - internal static string Format(IFormatProvider provider, string resourceFormat, object p1) + internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1) { if (UsingResourceKeys()) { @@ -95,7 +96,7 @@ namespace System return string.Format(provider, resourceFormat, p1); } - internal static string Format(IFormatProvider provider, string resourceFormat, object p1, object p2) + internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2) { if (UsingResourceKeys()) { @@ -105,7 +106,7 @@ namespace System return string.Format(provider, resourceFormat, p1, p2); } - internal static string Format(IFormatProvider provider, string resourceFormat, object p1, object p2, object p3) + internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2, object? p3) { if (UsingResourceKeys()) { @@ -115,7 +116,7 @@ namespace System return string.Format(provider, resourceFormat, p1, p2, p3); } - internal static string Format(IFormatProvider provider, string resourceFormat, params object[] args) + internal static string Format(IFormatProvider? provider, string resourceFormat, params object?[]? args) { if (args != null) { diff --git a/src/libraries/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj b/src/libraries/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj index 5aa1156a716..416a49a4991 100644 --- a/src/libraries/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj +++ b/src/libraries/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj @@ -8,6 +8,7 @@ true true netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;uapaot-Windows_NT-Debug;uapaot-Windows_NT-Release + enable diff --git a/src/libraries/System.Runtime.Extensions/src/System/AppDomainUnloadedException.cs b/src/libraries/System.Runtime.Extensions/src/System/AppDomainUnloadedException.cs index 60b471c704a..d05bd970c8a 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/AppDomainUnloadedException.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/AppDomainUnloadedException.cs @@ -17,13 +17,13 @@ namespace System HResult = COR_E_APPDOMAINUNLOADED; } - public AppDomainUnloadedException(string message) + public AppDomainUnloadedException(string? message) : base(message) { HResult = COR_E_APPDOMAINUNLOADED; } - public AppDomainUnloadedException(string message, Exception innerException) + public AppDomainUnloadedException(string? message, Exception? innerException) : base(message, innerException) { HResult = COR_E_APPDOMAINUNLOADED; diff --git a/src/libraries/System.Runtime.Extensions/src/System/ApplicationId.cs b/src/libraries/System.Runtime.Extensions/src/System/ApplicationId.cs index d5a0decd84f..540ac22ae14 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/ApplicationId.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/ApplicationId.cs @@ -2,9 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.IO; -using System.Reflection; -using System.Runtime.ExceptionServices; using System.Text; namespace System @@ -13,7 +10,7 @@ namespace System { private readonly byte[] _publicKeyToken; - public ApplicationId(byte[] publicKeyToken, string name, Version version, string processorArchitecture, string culture) + public ApplicationId(byte[] publicKeyToken, string name, Version version, string? processorArchitecture, string? culture) { if (name == null) throw new ArgumentNullException(nameof(name)); if (name.Length == 0) throw new ArgumentException(SR.Argument_EmptyApplicationName); @@ -27,11 +24,11 @@ namespace System Culture = culture; } - public string Culture { get; } + public string? Culture { get; } public string Name { get; } - public string ProcessorArchitecture { get; } + public string? ProcessorArchitecture { get; } public Version Version { get; } @@ -39,7 +36,9 @@ namespace System public ApplicationId Copy() => new ApplicationId(_publicKeyToken, Name, Version, ProcessorArchitecture, Culture); - public override string ToString () +#pragma warning disable CS8609 // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/23268 + public override string ToString() +#pragma warning restore CS8609 { Span charSpan = stackalloc char[128]; var sb = new ValueStringBuilder(charSpan); @@ -83,9 +82,9 @@ namespace System (char)((num < 10) ? (num + '0') : (num + ('A' - 10))); } - public override bool Equals (object o) + public override bool Equals(object? o) { - ApplicationId other = (o as ApplicationId); + ApplicationId? other = o as ApplicationId; if (other == null) return false; diff --git a/src/libraries/System.Runtime.Extensions/src/System/CodeDom/Compiler/IndentedTextWriter.cs b/src/libraries/System.Runtime.Extensions/src/System/CodeDom/Compiler/IndentedTextWriter.cs index 16bb4d5b9ff..160e059759a 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/CodeDom/Compiler/IndentedTextWriter.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/CodeDom/Compiler/IndentedTextWriter.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Diagnostics; using System.IO; using System.Text; using System.Globalization; @@ -30,7 +29,7 @@ namespace System.CodeDom.Compiler public override Encoding Encoding => _writer.Encoding; - public override string NewLine + public override string? NewLine // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/2384 { get { return _writer.NewLine; } set { _writer.NewLine = value; } @@ -60,7 +59,7 @@ namespace System.CodeDom.Compiler } } - public override void Write(string s) + public override void Write(string? s) { OutputTabs(); _writer.Write(s); @@ -78,7 +77,7 @@ namespace System.CodeDom.Compiler _writer.Write(value); } - public override void Write(char[] buffer) + public override void Write(char[]? buffer) { OutputTabs(); _writer.Write(buffer); @@ -114,36 +113,36 @@ namespace System.CodeDom.Compiler _writer.Write(value); } - public override void Write(object value) + public override void Write(object? value) { OutputTabs(); _writer.Write(value); } - public override void Write(string format, object arg0) + public override void Write(string format, object? arg0) { OutputTabs(); _writer.Write(format, arg0); } - public override void Write(string format, object arg0, object arg1) + public override void Write(string format, object? arg0, object? arg1) { OutputTabs(); _writer.Write(format, arg0, arg1); } - public override void Write(string format, params object[] arg) + public override void Write(string format, params object?[] arg) { OutputTabs(); _writer.Write(format, arg); } - public void WriteLineNoTabs(string s) + public void WriteLineNoTabs(string? s) { _writer.WriteLine(s); } - public override void WriteLine(string s) + public override void WriteLine(string? s) { OutputTabs(); _writer.WriteLine(s); @@ -171,7 +170,7 @@ namespace System.CodeDom.Compiler _tabsPending = true; } - public override void WriteLine(char[] buffer) + public override void WriteLine(char[]? buffer) { OutputTabs(); _writer.WriteLine(buffer); @@ -213,28 +212,28 @@ namespace System.CodeDom.Compiler _tabsPending = true; } - public override void WriteLine(object value) + public override void WriteLine(object? value) { OutputTabs(); _writer.WriteLine(value); _tabsPending = true; } - public override void WriteLine(string format, object arg0) + public override void WriteLine(string format, object? arg0) { OutputTabs(); _writer.WriteLine(format, arg0); _tabsPending = true; } - public override void WriteLine(string format, object arg0, object arg1) + public override void WriteLine(string format, object? arg0, object? arg1) { OutputTabs(); _writer.WriteLine(format, arg0, arg1); _tabsPending = true; } - public override void WriteLine(string format, params object[] arg) + public override void WriteLine(string format, params object?[] arg) { OutputTabs(); _writer.WriteLine(format, arg); diff --git a/src/libraries/System.Runtime.Extensions/src/System/Context.cs b/src/libraries/System.Runtime.Extensions/src/System/Context.cs index 737b01a0617..376198f1d53 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Context.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Context.cs @@ -6,7 +6,7 @@ using System.Runtime.Serialization; namespace System { - public abstract class ContextBoundObject : System.MarshalByRefObject + public abstract class ContextBoundObject : MarshalByRefObject { protected ContextBoundObject() { } } @@ -19,11 +19,11 @@ namespace System { } - public ContextMarshalException(string message) : this(message, null) + public ContextMarshalException(string? message) : this(message, null) { } - public ContextMarshalException(string message, Exception inner) : base(message, inner) + public ContextMarshalException(string? message, Exception? inner) : base(message, inner) { HResult = HResults.COR_E_CONTEXTMARSHAL; } @@ -34,7 +34,7 @@ namespace System } [AttributeUsage(AttributeTargets.Field, Inherited = false)] - public partial class ContextStaticAttribute : System.Attribute + public partial class ContextStaticAttribute : Attribute { public ContextStaticAttribute() { } } diff --git a/src/libraries/System.Runtime.Extensions/src/System/IO/BufferedStream.cs b/src/libraries/System.Runtime.Extensions/src/System/IO/BufferedStream.cs index 2c7f25af1dd..259b3d1c71f 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/IO/BufferedStream.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/IO/BufferedStream.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -50,23 +49,23 @@ namespace System.IO private const int MaxShadowBufferSize = 81920; // Make sure not to get to the Large Object Heap. private const int DefaultBufferSize = 4096; - private Stream _stream; // Underlying stream. Close sets _stream to null. - private byte[] _buffer; // Shared read/write buffer. Alloc on first use. + private Stream? _stream; // Underlying stream. Close sets _stream to null. + private byte[]? _buffer; // Shared read/write buffer. Alloc on first use. private readonly int _bufferSize; // Length of internal buffer (not counting the shadow buffer). private int _readPos; // Read pointer within shared buffer. private int _readLen; // Number of bytes read in buffer from _stream. private int _writePos; // Write pointer within shared buffer. - private Task _lastSyncCompletedReadTask; // The last successful Task returned from ReadAsync + private Task? _lastSyncCompletedReadTask; // The last successful Task returned from ReadAsync // (perf optimization for successive reads of the same size) // Removing a private default constructor is a breaking change for the DataDebugSerializer. // Because this ctor was here previously we need to keep it around. - private SemaphoreSlim _asyncActiveSemaphore; + private SemaphoreSlim? _asyncActiveSemaphore; internal SemaphoreSlim LazyEnsureAsyncActiveSemaphoreInitialized() { // Lazily-initialize _asyncActiveSemaphore. As we're never accessing the SemaphoreSlim's // WaitHandle, we don't need to worry about Disposing it. - return LazyInitializer.EnsureInitialized(ref _asyncActiveSemaphore, () => new SemaphoreSlim(1, 1)); + return LazyInitializer.EnsureInitialized(ref _asyncActiveSemaphore, () => new SemaphoreSlim(1, 1))!; } public BufferedStream(Stream stream) @@ -146,7 +145,7 @@ namespace System.IO _buffer = new byte[_bufferSize]; } - public Stream UnderlyingStream + public Stream? UnderlyingStream { get { @@ -195,7 +194,7 @@ namespace System.IO if (_writePos > 0) FlushWrite(); - return _stream.Length; + return _stream!.Length; } } @@ -207,7 +206,7 @@ namespace System.IO EnsureCanSeek(); Debug.Assert(!(_writePos > 0 && _readPos != _readLen), "Read and Write buffers cannot both have data in them at the same time."); - return _stream.Position + (_readPos - _readLen + _writePos); + return _stream!.Position + (_readPos - _readLen + _writePos); } set { @@ -222,7 +221,7 @@ namespace System.IO _readPos = 0; _readLen = 0; - _stream.Seek(value, SeekOrigin.Begin); + _stream!.Seek(value, SeekOrigin.Begin); } } @@ -293,7 +292,7 @@ namespace System.IO // If the underlying stream is not seekable AND we have something in the read buffer, then FlushRead would throw. // We can either throw away the buffer resulting in data loss (!) or ignore the Flush. // (We cannot throw because it would be a breaking change.) We opt into ignoring the Flush in that situation. - if (_stream.CanSeek) + if (_stream!.CanSeek) { FlushRead(); } @@ -310,7 +309,7 @@ namespace System.IO } // We had no data in the buffer, but we still need to tell the underlying stream to flush. - if (_stream.CanWrite) + if (_stream!.CanWrite) _stream.Flush(); _writePos = _readPos = _readLen = 0; @@ -381,6 +380,7 @@ namespace System.IO // All write functions should call this function to ensure that the buffered data is not lost. private void FlushRead() { + Debug.Assert(_stream != null); Debug.Assert(_writePos == 0, "BufferedStream: Write buffer must be empty in FlushRead!"); if (_readPos - _readLen != 0) @@ -395,6 +395,7 @@ namespace System.IO /// private void ClearReadBufferBeforeWrite() { + Debug.Assert(_stream != null); Debug.Assert(_readPos <= _readLen, "_readPos <= _readLen [" + _readPos + " <= " + _readLen + "]"); // No read data in the buffer: @@ -418,6 +419,7 @@ namespace System.IO private void FlushWrite() { + Debug.Assert(_stream != null); Debug.Assert(_readPos == 0 && _readLen == 0, "BufferedStream: Read buffer must be empty in FlushWrite!"); Debug.Assert(_buffer != null && _bufferSize >= _writePos, @@ -430,7 +432,7 @@ namespace System.IO private async Task FlushWriteAsync(CancellationToken cancellationToken) { - + Debug.Assert(_stream != null); Debug.Assert(_readPos == 0 && _readLen == 0, "BufferedStream: Read buffer must be empty in FlushWrite!"); Debug.Assert(_buffer != null && _bufferSize >= _writePos, @@ -451,7 +453,7 @@ namespace System.IO if (readbytes > count) readbytes = count; - Buffer.BlockCopy(_buffer, _readPos, array, offset, readbytes); + Buffer.BlockCopy(_buffer!, _readPos, array, offset, readbytes); _readPos += readbytes; return readbytes; @@ -469,7 +471,7 @@ namespace System.IO return readbytes; } - private int ReadFromBuffer(byte[] array, int offset, int count, out Exception error) + private int ReadFromBuffer(byte[] array, int offset, int count, out Exception? error) { try { @@ -496,6 +498,7 @@ namespace System.IO EnsureNotClosed(); EnsureCanRead(); + Debug.Assert(_stream != null); int bytesFromBuffer = ReadFromBuffer(array, offset, count); @@ -533,7 +536,7 @@ namespace System.IO // Ok. We can fill the buffer: EnsureBufferAllocated(); - _readLen = _stream.Read(_buffer, 0, _bufferSize); + _readLen = _stream.Read(_buffer!, 0, _bufferSize); bytesFromBuffer = ReadFromBuffer(array, offset, count); @@ -551,6 +554,7 @@ namespace System.IO { EnsureNotClosed(); EnsureCanRead(); + Debug.Assert(_stream != null); // Try to read from the buffer. int bytesFromBuffer = ReadFromBuffer(destination); @@ -587,14 +591,14 @@ namespace System.IO { // Otherwise, fill the buffer, then read from that. EnsureBufferAllocated(); - _readLen = _stream.Read(_buffer, 0, _bufferSize); + _readLen = _stream.Read(_buffer!, 0, _bufferSize); return ReadFromBuffer(destination) + bytesFromBuffer; } } private Task LastSyncCompletedReadTask(int val) { - Task t = _lastSyncCompletedReadTask; + Task? t = _lastSyncCompletedReadTask; Debug.Assert(t == null || t.IsCompletedSuccessfully); if (t != null && t.Result == val) @@ -634,7 +638,7 @@ namespace System.IO bool completeSynchronously = true; try { - Exception error; + Exception? error; bytesFromBuffer = ReadFromBuffer(buffer, offset, count, out error); // If we satisfied enough data from the buffer, we can complete synchronously. @@ -766,7 +770,7 @@ namespace System.IO } } - public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => + public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object? state) => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) => @@ -775,7 +779,7 @@ namespace System.IO public override int ReadByte() { return _readPos != _readLen ? - _buffer[_readPos++] : + _buffer![_readPos++] : ReadByteSlow(); } @@ -790,18 +794,19 @@ namespace System.IO // the stream is closed, its read buffer is flushed, so we'll take this slow path. EnsureNotClosed(); EnsureCanRead(); + Debug.Assert(_stream != null); if (_writePos > 0) FlushWrite(); EnsureBufferAllocated(); - _readLen = _stream.Read(_buffer, 0, _bufferSize); + _readLen = _stream.Read(_buffer!, 0, _bufferSize); _readPos = 0; if (_readLen == 0) return -1; - return _buffer[_readPos++]; + return _buffer![_readPos++]; } private void WriteToBuffer(byte[] array, ref int offset, ref int count) @@ -812,7 +817,7 @@ namespace System.IO return; EnsureBufferAllocated(); - Buffer.BlockCopy(array, offset, _buffer, _writePos, bytesToWrite); + Buffer.BlockCopy(array, offset, _buffer!, _writePos, bytesToWrite); _writePos += bytesToWrite; count -= bytesToWrite; @@ -831,7 +836,7 @@ namespace System.IO return bytesToWrite; } - private void WriteToBuffer(byte[] array, ref int offset, ref int count, out Exception error) + private void WriteToBuffer(byte[] array, ref int offset, ref int count, out Exception? error) { try { @@ -857,6 +862,7 @@ namespace System.IO EnsureNotClosed(); EnsureCanWrite(); + Debug.Assert(_stream != null); if (_writePos == 0) ClearReadBufferBeforeWrite(); @@ -984,6 +990,7 @@ namespace System.IO { EnsureNotClosed(); EnsureCanWrite(); + Debug.Assert(_stream != null); if (_writePos == 0) { @@ -1202,7 +1209,7 @@ namespace System.IO } } - public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => + public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object? state) => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); public override void EndWrite(IAsyncResult asyncResult) => @@ -1223,7 +1230,7 @@ namespace System.IO if (_writePos >= _bufferSize - 1) FlushWrite(); - _buffer[_writePos++] = value; + _buffer![_writePos++] = value; Debug.Assert(_writePos < _bufferSize); } @@ -1232,6 +1239,7 @@ namespace System.IO { EnsureNotClosed(); EnsureCanSeek(); + Debug.Assert(_stream != null); // If we have bytes in the write buffer, flush them out, seek and be done. if (_writePos > 0) @@ -1285,6 +1293,7 @@ namespace System.IO EnsureNotClosed(); EnsureCanSeek(); EnsureCanWrite(); + Debug.Assert(_stream != null); Flush(); _stream.SetLength(value); @@ -1293,6 +1302,7 @@ namespace System.IO public override void CopyTo(Stream destination, int bufferSize) { StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); + Debug.Assert(_stream != null); int readBytes = _readLen - _readPos; Debug.Assert(readBytes >= 0, $"Expected a non-negative number of bytes in buffer, got {readBytes}"); @@ -1301,7 +1311,7 @@ namespace System.IO { // If there's any read data in the buffer, write it all to the destination stream. Debug.Assert(_writePos == 0, "Write buffer must be empty if there's data in the read buffer"); - destination.Write(_buffer, _readPos, readBytes); + destination.Write(_buffer!, _readPos, readBytes); _readPos = _readLen = 0; } else if (_writePos > 0) @@ -1324,6 +1334,8 @@ namespace System.IO private async Task CopyToAsyncCore(Stream destination, int bufferSize, CancellationToken cancellationToken) { + Debug.Assert(_stream != null); + // Synchronize async operations as does Read/WriteAsync. await LazyEnsureAsyncActiveSemaphoreInitialized().WaitAsync().ConfigureAwait(false); try @@ -1347,7 +1359,7 @@ namespace System.IO // Our buffer is now clear. Copy data directly from the source stream to the destination stream. await _stream.CopyToAsync(destination, bufferSize, cancellationToken).ConfigureAwait(false); } - finally { _asyncActiveSemaphore.Release(); } + finally { _asyncActiveSemaphore!.Release(); } } } // class BufferedStream } // namespace diff --git a/src/libraries/System.Runtime.Extensions/src/System/IO/InvalidDataException.cs b/src/libraries/System.Runtime.Extensions/src/System/IO/InvalidDataException.cs index cf43001aa78..5d380caff06 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/IO/InvalidDataException.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/IO/InvalidDataException.cs @@ -15,12 +15,12 @@ namespace System.IO { } - public InvalidDataException(string message) + public InvalidDataException(string? message) : base(message) { } - public InvalidDataException(string message, Exception innerException) + public InvalidDataException(string? message, Exception? innerException) : base(message, innerException) { } diff --git a/src/libraries/System.Runtime.Extensions/src/System/IO/StringReader.cs b/src/libraries/System.Runtime.Extensions/src/System/IO/StringReader.cs index be884994a9c..cbaad153e4d 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/IO/StringReader.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/IO/StringReader.cs @@ -10,18 +10,13 @@ namespace System.IO // This class implements a text reader that reads from a string. public class StringReader : TextReader { - private string _s; + private string? _s; private int _pos; private int _length; public StringReader(string s) { - if (s == null) - { - throw new ArgumentNullException(nameof(s)); - } - - _s = s; + _s = s ?? throw new ArgumentNullException(nameof(s)); _length = s.Length; } @@ -174,7 +169,7 @@ namespace System.IO // contain the terminating carriage return and/or line feed. The returned // value is null if the end of the underlying string has been reached. // - public override string ReadLine() + public override string? ReadLine() { if (_s == null) { @@ -211,7 +206,7 @@ namespace System.IO } #region Task based Async APIs - public override Task ReadLineAsync() + public override Task ReadLineAsync() { return Task.FromResult(ReadLine()); } diff --git a/src/libraries/System.Runtime.Extensions/src/System/IO/StringWriter.cs b/src/libraries/System.Runtime.Extensions/src/System/IO/StringWriter.cs index 98e79391c9d..cd885d8171f 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/IO/StringWriter.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/IO/StringWriter.cs @@ -13,7 +13,7 @@ namespace System.IO // the resulting sequence of characters to be presented as a string. public class StringWriter : TextWriter { - private static volatile UnicodeEncoding s_encoding = null; + private static volatile UnicodeEncoding? s_encoding = null; private StringBuilder _sb; private bool _isOpen; @@ -25,7 +25,7 @@ namespace System.IO { } - public StringWriter(IFormatProvider formatProvider) + public StringWriter(IFormatProvider? formatProvider) : this(new StringBuilder(), formatProvider) { } @@ -36,7 +36,7 @@ namespace System.IO { } - public StringWriter(StringBuilder sb, IFormatProvider formatProvider) : base(formatProvider) + public StringWriter(StringBuilder sb, IFormatProvider? formatProvider) : base(formatProvider) { if (sb == null) { @@ -146,7 +146,7 @@ namespace System.IO // Writes a string to the underlying string buffer. If the given string is // null, nothing is written. // - public override void Write(string value) + public override void Write(string? value) { if (!_isOpen) { @@ -159,7 +159,7 @@ namespace System.IO } } - public override void Write(StringBuilder value) + public override void Write(StringBuilder? value) { if (GetType() != typeof(StringWriter)) { @@ -196,7 +196,7 @@ namespace System.IO WriteLine(); } - public override void WriteLine(StringBuilder value) + public override void WriteLine(StringBuilder? value) { if (GetType() != typeof(StringWriter)) { @@ -223,7 +223,7 @@ namespace System.IO return Task.CompletedTask; } - public override Task WriteAsync(string value) + public override Task WriteAsync(string? value) { Write(value); return Task.CompletedTask; @@ -246,7 +246,7 @@ namespace System.IO return Task.CompletedTask; } - public override Task WriteAsync(StringBuilder value, CancellationToken cancellationToken = default) + public override Task WriteAsync(StringBuilder? value, CancellationToken cancellationToken = default) { if (GetType() != typeof(StringWriter)) { @@ -275,13 +275,13 @@ namespace System.IO return Task.CompletedTask; } - public override Task WriteLineAsync(string value) + public override Task WriteLineAsync(string? value) { WriteLine(value); return Task.CompletedTask; } - public override Task WriteLineAsync(StringBuilder value, CancellationToken cancellationToken = default) + public override Task WriteLineAsync(StringBuilder? value, CancellationToken cancellationToken = default) { if (GetType() != typeof(StringWriter)) { @@ -326,13 +326,13 @@ namespace System.IO { return Task.CompletedTask; } - + #endregion - // Returns a string containing the characters written to this TextWriter - // so far. - // + // Returns a string containing the characters written to this TextWriter so far. +#pragma warning disable CS8609 // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/23268 public override string ToString() +#pragma warning restore CS8609 { return _sb.ToString(); } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Net/WebUtility.cs b/src/libraries/System.Runtime.Extensions/src/System/Net/WebUtility.cs index e4c65d401a8..ec35101cf3c 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Net/WebUtility.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Net/WebUtility.cs @@ -5,8 +5,6 @@ // Don't entity encode high chars (160 to 256) #define ENTITY_ENCODE_HIGH_ASCII_CHARS -using System; -using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -32,7 +30,7 @@ namespace System.Net #region HtmlEncode / HtmlDecode methods - public static string HtmlEncode(string value) + public static string? HtmlEncode(string? value) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (string.IsNullOrEmpty(value)) { @@ -66,7 +64,7 @@ namespace System.Net return sb.ToString(); } - public static void HtmlEncode(string value, TextWriter output) + public static void HtmlEncode(string? value, TextWriter output) { if (output == null) { @@ -184,7 +182,7 @@ namespace System.Net } } - public static string HtmlDecode(string value) + public static string? HtmlDecode(string? value) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (string.IsNullOrEmpty(value)) { @@ -214,7 +212,7 @@ namespace System.Net return sb.ToString(); } - public static void HtmlDecode(string value, TextWriter output) + public static void HtmlDecode(string? value, TextWriter output) { if (output == null) { @@ -405,7 +403,7 @@ namespace System.Net #region UrlEncode public methods [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "Already shipped public API; code moved here as part of API consolidation")] - public static string UrlEncode(string value) + public static string? UrlEncode(string? value) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (string.IsNullOrEmpty(value)) return value; @@ -456,7 +454,7 @@ namespace System.Net return Encoding.UTF8.GetString(newBytes); } - public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count) + public static byte[]? UrlEncodeToBytes(byte[]? value, int offset, int count) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (!ValidateUrlEncodingParameters(value, offset, count)) { @@ -469,7 +467,7 @@ namespace System.Net // count them first for (int i = 0; i < count; i++) { - char ch = (char)value[offset + i]; + char ch = (char)value![offset + i]; if (ch == ' ') foundSpaces = true; @@ -481,13 +479,13 @@ namespace System.Net if (!foundSpaces && unsafeCount == 0) { var subarray = new byte[count]; - Buffer.BlockCopy(value, offset, subarray, 0, count); + Buffer.BlockCopy(value!, offset, subarray, 0, count); return subarray; } // expand not 'safe' characters into %XX, spaces to +s byte[] expandedBytes = new byte[count + unsafeCount * 2]; - GetEncodedBytes(value, offset, count, expandedBytes); + GetEncodedBytes(value!, offset, count, expandedBytes); return expandedBytes; } @@ -495,7 +493,7 @@ namespace System.Net #region UrlDecode implementation - private static string UrlDecodeInternal(string value, Encoding encoding) + private static string? UrlDecodeInternal(string? value, Encoding encoding) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (string.IsNullOrEmpty(value)) { @@ -557,7 +555,7 @@ namespace System.Net return helper.GetString(); } - private static byte[] UrlDecodeInternal(byte[] bytes, int offset, int count) + private static byte[]? UrlDecodeInternal(byte[]? bytes, int offset, int count) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { if (!ValidateUrlEncodingParameters(bytes, offset, count)) { @@ -570,7 +568,7 @@ namespace System.Net for (int i = 0; i < count; i++) { int pos = offset + i; - byte b = bytes[pos]; + byte b = bytes![pos]; if (b == '+') { @@ -593,7 +591,7 @@ namespace System.Net if (decodedBytesCount < decodedBytes.Length) { - Array.Resize(ref decodedBytes, decodedBytesCount); + Array.Resize(ref decodedBytes!, decodedBytesCount); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 } return decodedBytes; @@ -605,12 +603,12 @@ namespace System.Net [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "Already shipped public API; code moved here as part of API consolidation")] - public static string UrlDecode(string encodedValue) + public static string? UrlDecode(string? encodedValue) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { return UrlDecodeInternal(encodedValue, Encoding.UTF8); } - public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count) + public static byte[]? UrlDecodeToBytes(byte[]? encodedValue, int offset, int count) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761 { return UrlDecodeInternal(encodedValue, offset, count); } @@ -719,7 +717,7 @@ namespace System.Net } } - private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count) + private static bool ValidateUrlEncodingParameters(byte[]? bytes, int offset, int count) { if (bytes == null && count == 0) return false; @@ -763,11 +761,11 @@ namespace System.Net // Accumulate characters in a special array private int _numChars; - private char[] _charBuffer; + private char[]? _charBuffer; // Accumulate bytes for decoding into characters in a special array private int _numBytes; - private byte[] _byteBuffer; + private byte[]? _byteBuffer; // Encoding to convert chars to bytes private Encoding _encoding; @@ -778,7 +776,7 @@ namespace System.Net if (_charBuffer == null) _charBuffer = new char[_bufferSize]; - _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars); + _numChars += _encoding.GetChars(_byteBuffer!, 0, _numBytes, _charBuffer, _numChars); _numBytes = 0; } @@ -819,7 +817,7 @@ namespace System.Net FlushBytes(); Debug.Assert(_numChars > 0); - return new string(_charBuffer, 0, _numChars); + return new string(_charBuffer!, 0, _numChars); } } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Reflection/AssemblyNameProxy.cs b/src/libraries/System.Runtime.Extensions/src/System/Reflection/AssemblyNameProxy.cs index a9337af605f..3a2e27aec19 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Reflection/AssemblyNameProxy.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Reflection/AssemblyNameProxy.cs @@ -2,10 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace System.Reflection { - using System; - using System.Runtime.Versioning; - +namespace System.Reflection +{ public class AssemblyNameProxy : MarshalByRefObject { public AssemblyName GetAssemblyName(string assemblyFile) diff --git a/src/libraries/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs b/src/libraries/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs index b19af26cef9..51f4520a2f5 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs @@ -17,10 +17,10 @@ namespace System.Runtime.CompilerServices public SwitchExpressionException() : base(SR.Arg_SwitchExpressionException) { } - public SwitchExpressionException(Exception innerException) : + public SwitchExpressionException(Exception? innerException) : base(SR.Arg_SwitchExpressionException, innerException) { } - public SwitchExpressionException(object unmatchedValue) : this() + public SwitchExpressionException(object? unmatchedValue) : this() { UnmatchedValue = unmatchedValue; } @@ -31,12 +31,12 @@ namespace System.Runtime.CompilerServices UnmatchedValue = info.GetValue(nameof(UnmatchedValue), typeof(object)); } - public SwitchExpressionException(string message) : base(message) { } + public SwitchExpressionException(string? message) : base(message) { } - public SwitchExpressionException(string message, Exception innerException) + public SwitchExpressionException(string? message, Exception? innerException) : base(message, innerException) { } - public object UnmatchedValue { get; } + public object? UnmatchedValue { get; } public override void GetObjectData(SerializationInfo info, StreamingContext context) { @@ -52,6 +52,7 @@ namespace System.Runtime.CompilerServices { return base.Message; } + string valueMessage = SR.Format(SR.SwitchExpressionException_UnmatchedValue, UnmatchedValue); return base.Message + Environment.NewLine + valueMessage; } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/FrameworkName.cs b/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/FrameworkName.cs index 4bf249ff1d7..811d32df5e6 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/FrameworkName.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/FrameworkName.cs @@ -6,12 +6,12 @@ using System.Diagnostics; namespace System.Runtime.Versioning { - public sealed class FrameworkName : IEquatable + public sealed class FrameworkName : IEquatable { private readonly string _identifier; - private readonly Version _version; + private readonly Version _version = null!; private readonly string _profile; - private string _fullName; + private string? _fullName; private const char ComponentSeparator = ','; private const char KeyValueSeparator = '='; @@ -71,19 +71,20 @@ namespace System.Runtime.Versioning Profile; } } + Debug.Assert(_fullName != null); return _fullName; } } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return Equals(obj as FrameworkName); } - public bool Equals(FrameworkName other) + public bool Equals(FrameworkName? other) { - if (object.ReferenceEquals(other, null)) + if (other is null) { return false; } @@ -98,7 +99,9 @@ namespace System.Runtime.Versioning return Identifier.GetHashCode() ^ Version.GetHashCode() ^ Profile.GetHashCode(); } +#pragma warning disable CS8609 // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/23268 public override string ToString() +#pragma warning restore CS8609 { return FullName; } @@ -108,7 +111,7 @@ namespace System.Runtime.Versioning { } - public FrameworkName(string identifier, Version version, string profile) + public FrameworkName(string identifier, Version version, string? profile) { if (identifier == null) { @@ -227,16 +230,17 @@ namespace System.Runtime.Versioning } } - public static bool operator ==(FrameworkName left, FrameworkName right) + public static bool operator ==(FrameworkName? left, FrameworkName? right) { - if (object.ReferenceEquals(left, null)) + if (left is null) { - return object.ReferenceEquals(right, null); + return right is null; } + return left.Equals(right); } - public static bool operator !=(FrameworkName left, FrameworkName right) + public static bool operator !=(FrameworkName? left, FrameworkName? right) { return !(left == right); } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/VersioningHelper.cs b/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/VersioningHelper.cs index 42fc9d88f19..bbebb8d5e31 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/VersioningHelper.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Runtime/Versioning/VersioningHelper.cs @@ -25,12 +25,12 @@ namespace System.Runtime.Versioning private const ResourceScope ResTypeMask = ResourceScope.Machine | ResourceScope.Process | ResourceScope.AppDomain | ResourceScope.Library; private const ResourceScope VisibilityMask = ResourceScope.Private | ResourceScope.Assembly; - public static string MakeVersionSafeName(string name, ResourceScope from, ResourceScope to) + public static string MakeVersionSafeName(string? name, ResourceScope from, ResourceScope to) { - return MakeVersionSafeName(name, from, to, null); + return MakeVersionSafeName(name, from, to, type: null); } - public static string MakeVersionSafeName(string name, ResourceScope from, ResourceScope to, Type type) + public static string MakeVersionSafeName(string? name, ResourceScope from, ResourceScope to, Type? type) { ResourceScope fromResType = from & ResTypeMask; ResourceScope toResType = to & ResTypeMask; @@ -68,12 +68,12 @@ namespace System.Runtime.Versioning if ((requires & SxSRequirements.TypeName) != 0) { safeName.Append(separator); - safeName.Append(type.Name); + safeName.Append(type!.Name); } if ((requires & SxSRequirements.AssemblyName) != 0) { safeName.Append(separator); - safeName.Append(type.Assembly.FullName); + safeName.Append(type!.Assembly.FullName); } return safeName.ToString(); } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityAttribute.cs b/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityAttribute.cs index 73c0f891b88..090893761d6 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityAttribute.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityAttribute.cs @@ -10,6 +10,6 @@ namespace System.Security.Permissions protected SecurityAttribute(SecurityAction action) { } public SecurityAction Action { get; set; } public bool Unrestricted { get; set; } - public abstract IPermission CreatePermission(); + public abstract IPermission? CreatePermission(); } } diff --git a/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityPermissionAttribute.cs b/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityPermissionAttribute.cs index d7a896ec015..e1e74c4166a 100644 --- a/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityPermissionAttribute.cs +++ b/src/libraries/System.Runtime.Extensions/src/System/Security/Permissions/SecurityPermissionAttribute.cs @@ -23,6 +23,6 @@ namespace System.Security.Permissions public bool SerializationFormatter { get; set; } public bool SkipVerification { get; set; } public bool UnmanagedCode { get; set; } - public override IPermission CreatePermission() { return default(IPermission); } + public override IPermission? CreatePermission() { return null; } } }