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

More CoreLib code cleanup #26104

Merged
merged 6 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class RegistryKey : IDisposable
private const int MaxKeyLength = 255;
private const int MaxValueLength = 16383;

private SafeRegistryHandle _hkey;
private readonly SafeRegistryHandle _hkey;

private RegistryKey(SafeRegistryHandle hkey)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace System
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class AggregateException : Exception
{
private ReadOnlyCollection<Exception> m_innerExceptions; // Complete set of exceptions. Do not rename (binary serialization)
private readonly ReadOnlyCollection<Exception> m_innerExceptions; // Complete set of exceptions. Do not rename (binary serialization)

/// <summary>
/// Initializes a new instance of the <see cref="AggregateException"/> class.
Expand Down Expand Up @@ -258,7 +258,7 @@ protected AggregateException(SerializationInfo info, StreamingContext context) :
}

Exception[]? innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
if (innerExceptions == null)
if (innerExceptions is null)
{
throw new SerializationException(SR.AggregateException_DeserializationFailure);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace System
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class ArgumentException : SystemException
{
private string? _paramName;
private readonly string? _paramName;

// Creates a new ArgumentException with its message
// string set to the empty string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace System
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class ArgumentOutOfRangeException : ArgumentException
{
private object? _actualValue;
private readonly object? _actualValue;

// Creates a new ArgumentOutOfRangeException with its message
// string set to a default message explaining an argument was out of range.
Expand Down
10 changes: 5 additions & 5 deletions src/System.Private.CoreLib/shared/System/Array.Enumerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace System
{
internal sealed class ArrayEnumerator : IEnumerator, ICloneable
{
private Array array;
private readonly Array array;
private int index;
private int endIndex;
private int startIndex; // Save for Reset.
private int[] _indices; // The current position in a multidim array
private readonly int endIndex;
private readonly int startIndex; // Save for Reset.
private readonly int[] _indices; // The current position in a multidim array
private bool _complete;

internal ArrayEnumerator(Array array, int index, int count)
Expand Down Expand Up @@ -110,7 +110,7 @@ internal sealed class SZArrayEnumerator : IEnumerator, ICloneable
{
private readonly Array _array;
private int _index;
private int _endIndex; // Cache Array.Length, since it's a little slow.
private readonly int _endIndex; // Cache Array.Length, since it's a little slow.

internal SZArrayEnumerator(Array array)
{
Expand Down
6 changes: 2 additions & 4 deletions src/System.Private.CoreLib/shared/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,8 @@ public static int IndexOf(Array array, object? value, int startIndex, int count)
return retVal;
#endif

object[]? objArray = array as object[];
int endIndex = startIndex + count;
if (objArray != null)
if (array is object[] objArray)
{
if (value == null)
{
Expand Down Expand Up @@ -1120,9 +1119,8 @@ public static int LastIndexOf(Array array, object? value, int startIndex, int co
return retVal;
#endif

object[]? objArray = array as object[];
int endIndex = startIndex - count + 1;
if (objArray != null)
if (array is object[] objArray)
{
if (value == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace System
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class AttributeUsageAttribute : Attribute
{
private AttributeTargets _attributeTarget = AttributeTargets.All; // Defaults to all
private readonly AttributeTargets _attributeTarget = AttributeTargets.All; // Defaults to all
private bool _allowMultiple = false; // Defaults to false
private bool _inherited = true; // Defaults to true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace System
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public partial class BadImageFormatException : SystemException
{
private string? _fileName; // The name of the corrupt PE file.
private string? _fusionLog; // fusion log (when applicable)
private readonly string? _fileName; // The name of the corrupt PE file.
private readonly string? _fusionLog; // fusion log (when applicable)

public BadImageFormatException()
: base(SR.Arg_BadImageFormatException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override T[] Rent(int minimumLength)
}

var log = ArrayPoolEventSource.Log;
T[]? buffer = null;
T[]? buffer;

int index = Utilities.SelectBucketIndex(minimumLength);
if (index < _buckets.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static bool TryFormatDateTimeO(DateTime value, TimeSpan offset, Span<byt
bytesWritten = bytesRequired;

// Hoist most of the bounds checks on buffer.
{ var unused = destination[MinimumBytesNeeded - 1]; }
{ _ = destination[MinimumBytesNeeded - 1]; }

FormattingHelpers.WriteFourDecimalDigits((uint)value.Year, destination, 0);
destination[4] = Utf8Constants.Minus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,14 @@ public static bool TryFormat(DateTimeOffset value, Span<byte> destination, out i
offset = value.Offset;
}

switch (symbol)
return symbol switch
{
case 'R':
return TryFormatDateTimeR(value.UtcDateTime, destination, out bytesWritten);

case 'l':
return TryFormatDateTimeL(value.UtcDateTime, destination, out bytesWritten);

case 'O':
return TryFormatDateTimeO(value.DateTime, value.Offset, destination, out bytesWritten);

case 'G':
return TryFormatDateTimeG(value.DateTime, offset, destination, out bytesWritten);

default:
return FormattingHelpers.TryFormatThrowFormatException(out bytesWritten);
}
'R' => TryFormatDateTimeR(value.UtcDateTime, destination, out bytesWritten),
'l' => TryFormatDateTimeL(value.UtcDateTime, destination, out bytesWritten),
'O' => TryFormatDateTimeO(value.DateTime, value.Offset, destination, out bytesWritten),
'G' => TryFormatDateTimeG(value.DateTime, offset, destination, out bytesWritten),
_ => FormattingHelpers.TryFormatThrowFormatException(out bytesWritten),
};
}

/// <summary>
Expand Down Expand Up @@ -149,23 +140,14 @@ public static bool TryFormat(DateTime value, Span<byte> destination, out int byt
{
char symbol = FormattingHelpers.GetSymbolOrDefault(format, 'G');

switch (symbol)
return symbol switch
{
case 'R':
return TryFormatDateTimeR(value, destination, out bytesWritten);

case 'l':
return TryFormatDateTimeL(value, destination, out bytesWritten);

case 'O':
return TryFormatDateTimeO(value, Utf8Constants.NullUtcOffset, destination, out bytesWritten);

case 'G':
return TryFormatDateTimeG(value, Utf8Constants.NullUtcOffset, destination, out bytesWritten);

default:
return FormattingHelpers.TryFormatThrowFormatException(out bytesWritten);
}
'R' => TryFormatDateTimeR(value, destination, out bytesWritten),
'l' => TryFormatDateTimeL(value, destination, out bytesWritten),
'O' => TryFormatDateTimeO(value, Utf8Constants.NullUtcOffset, destination, out bytesWritten),
'G' => TryFormatDateTimeG(value, Utf8Constants.NullUtcOffset, destination, out bytesWritten),
_ => FormattingHelpers.TryFormatThrowFormatException(out bytesWritten),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
// because it may have an observable side effect (throwing).
// We use 8 instead of 7 so that we also capture the dash if we're asked to insert one.

{ var unused = destination[8]; }
{ _ = destination[8]; }
FormattingHelpers.WriteHexByte(guidAsBytes.Byte03, destination, 0, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte02, destination, 2, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte01, destination, 4, FormattingHelpers.HexCasing.Lowercase);
Expand All @@ -133,7 +133,7 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
destination = destination.Slice(8);
}

{ var unused = destination[4]; }
{ _ = destination[4]; }
FormattingHelpers.WriteHexByte(guidAsBytes.Byte05, destination, 0, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte04, destination, 2, FormattingHelpers.HexCasing.Lowercase);

Expand All @@ -147,7 +147,7 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
destination = destination.Slice(4);
}

{ var unused = destination[4]; }
{ _ = destination[4]; }
FormattingHelpers.WriteHexByte(guidAsBytes.Byte07, destination, 0, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte06, destination, 2, FormattingHelpers.HexCasing.Lowercase);

Expand All @@ -161,7 +161,7 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
destination = destination.Slice(4);
}

{ var unused = destination[4]; }
{ _ = destination[4]; }
FormattingHelpers.WriteHexByte(guidAsBytes.Byte08, destination, 0, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte09, destination, 2, FormattingHelpers.HexCasing.Lowercase);

Expand All @@ -175,7 +175,7 @@ public static bool TryFormat(Guid value, Span<byte> destination, out int bytesWr
destination = destination.Slice(4);
}

{ var unused = destination[11]; } // can't hoist bounds check on the final brace (if exists)
{ _ = destination[11]; } // can't hoist bounds check on the final brace (if exists)
FormattingHelpers.WriteHexByte(guidAsBytes.Byte10, destination, 0, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte11, destination, 2, FormattingHelpers.HexCasing.Lowercase);
FormattingHelpers.WriteHexByte(guidAsBytes.Byte12, destination, 4, FormattingHelpers.HexCasing.Lowercase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,15 @@ public static bool TryParse(ReadOnlySpan<byte> source, out DateTime value, out i
/// </exceptions>
public static bool TryParse(ReadOnlySpan<byte> source, out DateTimeOffset value, out int bytesConsumed, char standardFormat = default)
{
switch (standardFormat)
return standardFormat switch
{
case 'R':
return TryParseDateTimeOffsetR(source, NoFlipCase, out value, out bytesConsumed);

case 'l':
return TryParseDateTimeOffsetR(source, FlipCase, out value, out bytesConsumed);

case 'O':
return TryParseDateTimeOffsetO(source, out value, out bytesConsumed, out _);

case default(char):
return TryParseDateTimeOffsetDefault(source, out value, out bytesConsumed);

case 'G':
return TryParseDateTimeG(source, out DateTime _, out value, out bytesConsumed);

default:
return ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed);
}
'R' => TryParseDateTimeOffsetR(source, NoFlipCase, out value, out bytesConsumed),
'l' => TryParseDateTimeOffsetR(source, FlipCase, out value, out bytesConsumed),
'O' => TryParseDateTimeOffsetO(source, out value, out bytesConsumed, out _),
default(char) => TryParseDateTimeOffsetDefault(source, out value, out bytesConsumed),
'G' => TryParseDateTimeG(source, out DateTime _, out value, out bytesConsumed),
_ => ParserHelpers.TryParseThrowFormatException(out value, out bytesConsumed),
};
}

private const uint FlipCase = 0x00000020u; // XOR mask to flip the case of a letter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace System
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
public sealed class CLSCompliantAttribute : Attribute
{
private bool _compliant;
private readonly bool _compliant;

public CLSCompliantAttribute(bool isCompliant)
{
Expand Down
37 changes: 18 additions & 19 deletions src/System.Private.CoreLib/shared/System/Collections/ArrayList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ public virtual void TrimToSize()
// Note this requires reimplementing half of ArrayList...
private class IListWrapper : ArrayList
{
private IList _list;
private readonly IList _list;

internal IListWrapper(IList list)
{
Expand Down Expand Up @@ -983,8 +983,7 @@ public override void InsertRange(int index, ICollection c)

if (c.Count > 0)
{
ArrayList? al = _list as ArrayList;
if (al != null)
if (_list is ArrayList al)
{
// We need to special case ArrayList.
// When c is a range of _list, we need to handle this in a special way.
Expand Down Expand Up @@ -1235,8 +1234,8 @@ public void Reset()

private class SyncArrayList : ArrayList
{
private ArrayList _list;
private object _root;
private readonly ArrayList _list;
private readonly object _root;

internal SyncArrayList(ArrayList list)
: base(false)
Expand Down Expand Up @@ -1590,8 +1589,8 @@ public override void TrimToSize()

private class SyncIList : IList
{
private IList _list;
private object _root;
private readonly IList _list;
private readonly object _root;

internal SyncIList(IList list)
{
Expand Down Expand Up @@ -1719,7 +1718,7 @@ public virtual void RemoveAt(int index)

private class FixedSizeList : IList
{
private IList _list;
private readonly IList _list;

internal FixedSizeList(IList l)
{
Expand Down Expand Up @@ -2031,7 +2030,7 @@ public override void TrimToSize()

private class ReadOnlyList : IList
{
private IList _list;
private readonly IList _list;

internal ReadOnlyList(IList l)
{
Expand Down Expand Up @@ -2343,12 +2342,12 @@ public override void TrimToSize()
// made to the list while an enumeration is in progress.
private sealed class ArrayListEnumerator : IEnumerator, ICloneable
{
private ArrayList _list;
private readonly ArrayList _list;
private int _index;
private int _endIndex; // Where to stop.
private int _version;
private readonly int _endIndex; // Where to stop.
private readonly int _version;
private object? _currentElement;
private int _startIndex; // Save this for Reset.
private readonly int _startIndex; // Save this for Reset.

internal ArrayListEnumerator(ArrayList list, int index, int count)
{
Expand Down Expand Up @@ -2404,7 +2403,7 @@ public void Reset()
private class Range : ArrayList
{
private ArrayList _baseList;
private int _baseIndex;
private readonly int _baseIndex;
private int _baseSize;
private int _baseVersion;

Expand Down Expand Up @@ -2819,13 +2818,13 @@ public override void TrimToSize()

private sealed class ArrayListEnumeratorSimple : IEnumerator, ICloneable
{
private ArrayList _list;
private readonly ArrayList _list;
private int _index;
private int _version;
private readonly int _version;
private object? _currentElement;
private bool _isArrayList;
private readonly bool _isArrayList;
// this object is used to indicate enumeration has not started or has terminated
private static object s_dummyObject = new object();
private static readonly object s_dummyObject = new object();

internal ArrayListEnumeratorSimple(ArrayList list)
{
Expand Down Expand Up @@ -2910,7 +2909,7 @@ public void Reset()

internal class ArrayListDebugView
{
private ArrayList _arrayList;
private readonly ArrayList _arrayList;

public ArrayListDebugView(ArrayList arrayList)
{
Expand Down
Loading