Use Nullable<T>.GetValueOrDefault() instead of Nullable<T>.Value when a it is known to have a value. (#1063)
This commit is contained in:
parent
5da68a2bb7
commit
31a836c9f3
|
|
@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.Http
|
|||
Domain = Domain,
|
||||
IsEssential = IsEssential,
|
||||
Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps),
|
||||
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?)
|
||||
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.GetValueOrDefault()) : default(DateTimeOffset?)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,13 +103,13 @@ namespace Microsoft.AspNetCore.Http.Internal
|
|||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
if (!values.HasValue || StringValues.IsNullOrEmpty(values.Value))
|
||||
if (!values.HasValue || StringValues.IsNullOrEmpty(values.GetValueOrDefault()))
|
||||
{
|
||||
headers.Remove(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
headers[key] = values.Value;
|
||||
headers[key] = values.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Http
|
|||
|
||||
if (value.HasValue)
|
||||
{
|
||||
headers[name] = HeaderUtilities.FormatDate(value.Value);
|
||||
headers[name] = HeaderUtilities.FormatDate(value.GetValueOrDefault());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Http
|
|||
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
|
||||
}
|
||||
if (count.HasValue &&
|
||||
(count.Value < 0 || count.Value > fileLength - offset))
|
||||
(count.GetValueOrDefault() < 0 || count.GetValueOrDefault() > fileLength - offset))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,13 +42,13 @@ namespace Microsoft.AspNetCore.Http.Extensions
|
|||
{
|
||||
Debug.Assert(source != null);
|
||||
Debug.Assert(destination != null);
|
||||
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0);
|
||||
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.GetValueOrDefault() >= 0);
|
||||
Debug.Assert(buffer != null);
|
||||
|
||||
while (true)
|
||||
{
|
||||
// The natural end of the range.
|
||||
if (bytesRemaining.HasValue && bytesRemaining.Value <= 0)
|
||||
if (bytesRemaining.HasValue && bytesRemaining.GetValueOrDefault() <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
|
|||
int readLength = buffer.Length;
|
||||
if (bytesRemaining.HasValue)
|
||||
{
|
||||
readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength);
|
||||
readLength = (int)Math.Min(bytesRemaining.GetValueOrDefault(), (long)readLength);
|
||||
}
|
||||
int read = await source.ReadAsync(buffer, 0, readLength, cancel);
|
||||
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ namespace Microsoft.AspNetCore.Http.Features
|
|||
if (section.BaseStreamOffset.HasValue)
|
||||
{
|
||||
// Relative reference to buffered request body
|
||||
file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName);
|
||||
file = new FormFile(_request.Body, section.BaseStreamOffset.GetValueOrDefault(), section.Body.Length, name, fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Http
|
|||
ThrowIfReadOnly();
|
||||
if (value.HasValue)
|
||||
{
|
||||
this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.Value);
|
||||
this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.Owin
|
|||
{
|
||||
if (value.HasValue)
|
||||
{
|
||||
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.Value);
|
||||
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -151,9 +151,9 @@ namespace Microsoft.AspNetCore.WebUtilities
|
|||
if (_observedLength < _position)
|
||||
{
|
||||
_observedLength = _position;
|
||||
if (LengthLimit.HasValue && _observedLength > LengthLimit.Value)
|
||||
if (LengthLimit.HasValue && _observedLength > LengthLimit.GetValueOrDefault())
|
||||
{
|
||||
throw new InvalidDataException($"Multipart body length limit {LengthLimit.Value} exceeded.");
|
||||
throw new InvalidDataException($"Multipart body length limit {LengthLimit.GetValueOrDefault()} exceeded.");
|
||||
}
|
||||
}
|
||||
return read;
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ namespace Microsoft.AspNetCore.WebUtilities
|
|||
{
|
||||
// Not all streams support cancellation directly.
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (limit.HasValue && limit.Value - total < read)
|
||||
if (limit.HasValue && limit.GetValueOrDefault() - total < read)
|
||||
{
|
||||
throw new InvalidDataException($"The stream exceeded the data limit {limit.Value}.");
|
||||
throw new InvalidDataException($"The stream exceeded the data limit {limit.GetValueOrDefault()}.");
|
||||
}
|
||||
total += read;
|
||||
read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
|
||||
|
|
|
|||
|
|
@ -197,14 +197,14 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
AppendValueWithSeparatorIfRequired(sb, MaxAgeString);
|
||||
sb.Append('=');
|
||||
sb.Append(((int)_maxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(((int)_maxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
|
||||
if (_sharedMaxAge.HasValue)
|
||||
{
|
||||
AppendValueWithSeparatorIfRequired(sb, SharedMaxAgeString);
|
||||
sb.Append('=');
|
||||
sb.Append(((int)_sharedMaxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(((int)_sharedMaxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
|
||||
if (_maxStale)
|
||||
|
|
@ -213,7 +213,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
if (_maxStaleLimit.HasValue)
|
||||
{
|
||||
sb.Append('=');
|
||||
sb.Append(((int)_maxStaleLimit.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(((int)_maxStaleLimit.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -221,7 +221,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
AppendValueWithSeparatorIfRequired(sb, MinFreshString);
|
||||
sb.Append('=');
|
||||
sb.Append(((int)_minFresh.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(((int)_minFresh.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
|
||||
if (_private)
|
||||
|
|
@ -291,10 +291,10 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
// XOR the hashcode of timespan values with different numbers to make sure two instances with the same
|
||||
// timespan set on different fields result in different hashcodes.
|
||||
result = result ^ (_maxAge.HasValue ? _maxAge.Value.GetHashCode() ^ 1 : 0) ^
|
||||
(_sharedMaxAge.HasValue ? _sharedMaxAge.Value.GetHashCode() ^ 2 : 0) ^
|
||||
(_maxStaleLimit.HasValue ? _maxStaleLimit.Value.GetHashCode() ^ 4 : 0) ^
|
||||
(_minFresh.HasValue ? _minFresh.Value.GetHashCode() ^ 8 : 0);
|
||||
result = result ^ (_maxAge.HasValue ? _maxAge.GetValueOrDefault().GetHashCode() ^ 1 : 0) ^
|
||||
(_sharedMaxAge.HasValue ? _sharedMaxAge.GetValueOrDefault().GetHashCode() ^ 2 : 0) ^
|
||||
(_maxStaleLimit.HasValue ? _maxStaleLimit.GetValueOrDefault().GetHashCode() ^ 4 : 0) ^
|
||||
(_minFresh.HasValue ? _minFresh.GetValueOrDefault().GetHashCode() ^ 8 : 0);
|
||||
|
||||
if ((_noCacheHeaders != null) && (_noCacheHeaders.Count > 0))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -137,11 +137,11 @@ namespace Microsoft.Net.Http.Headers
|
|||
}
|
||||
else if (sizeParameter != null)
|
||||
{
|
||||
sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture);
|
||||
sizeParameter.Value = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
else
|
||||
{
|
||||
string sizeString = value.Value.ToString(CultureInfo.InvariantCulture);
|
||||
string sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture);
|
||||
_parameters.Add(new NameValueHeaderValue(SizeString, sizeString));
|
||||
}
|
||||
}
|
||||
|
|
@ -324,7 +324,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
else
|
||||
{
|
||||
// Must always be quoted
|
||||
var dateString = HeaderUtilities.FormatDate(date.Value, quoted: true);
|
||||
var dateString = HeaderUtilities.FormatDate(date.GetValueOrDefault(), quoted: true);
|
||||
if (dateParameter != null)
|
||||
{
|
||||
dateParameter.Value = dateString;
|
||||
|
|
|
|||
|
|
@ -151,9 +151,9 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
if (HasRange)
|
||||
{
|
||||
sb.Append(_from.Value.ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(_from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append('-');
|
||||
sb.Append(_to.Value.ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(_to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -163,7 +163,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
sb.Append('/');
|
||||
if (HasLength)
|
||||
{
|
||||
sb.Append(_length.Value.ToString(NumberFormatInfo.InvariantInfo));
|
||||
sb.Append(_length.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
if (_entityTag == null)
|
||||
{
|
||||
return HeaderUtilities.FormatDate(_lastModified.Value);
|
||||
return HeaderUtilities.FormatDate(_lastModified.GetValueOrDefault());
|
||||
}
|
||||
return _entityTag.ToString();
|
||||
}
|
||||
|
|
@ -70,7 +70,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
if (_entityTag == null)
|
||||
{
|
||||
return (other._lastModified != null) && (_lastModified.Value == other._lastModified.Value);
|
||||
return (other._lastModified != null) && (_lastModified.GetValueOrDefault() == other._lastModified.GetValueOrDefault());
|
||||
}
|
||||
|
||||
return _entityTag.Equals(other._entityTag);
|
||||
|
|
@ -80,7 +80,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
if (_entityTag == null)
|
||||
{
|
||||
return _lastModified.Value.GetHashCode();
|
||||
return _lastModified.GetValueOrDefault().GetHashCode();
|
||||
}
|
||||
|
||||
return _entityTag.GetHashCode();
|
||||
|
|
|
|||
|
|
@ -20,15 +20,15 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
throw new ArgumentException("Invalid header range.");
|
||||
}
|
||||
if (from.HasValue && (from.Value < 0))
|
||||
if (from.HasValue && (from.GetValueOrDefault() < 0))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(from));
|
||||
}
|
||||
if (to.HasValue && (to.Value < 0))
|
||||
if (to.HasValue && (to.GetValueOrDefault() < 0))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(to));
|
||||
}
|
||||
if (from.HasValue && to.HasValue && (from.Value > to.Value))
|
||||
if (from.HasValue && to.HasValue && (from.GetValueOrDefault() > to.GetValueOrDefault()))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(from));
|
||||
}
|
||||
|
|
@ -51,38 +51,37 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
if (!_from.HasValue)
|
||||
{
|
||||
return "-" + _to.Value.ToString(NumberFormatInfo.InvariantInfo);
|
||||
return "-" + _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
else if (!_to.HasValue)
|
||||
{
|
||||
return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-";
|
||||
return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-";
|
||||
}
|
||||
return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-" +
|
||||
_to.Value.ToString(NumberFormatInfo.InvariantInfo);
|
||||
return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-" +
|
||||
_to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var other = obj as RangeItemHeaderValue;
|
||||
|
||||
if (other == null)
|
||||
if (obj is RangeItemHeaderValue other)
|
||||
{
|
||||
return false;
|
||||
return ((_from == other._from) && (_to == other._to));
|
||||
}
|
||||
return ((_from == other._from) && (_to == other._to));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
if (!_from.HasValue)
|
||||
{
|
||||
return _to.GetHashCode();
|
||||
return _to.GetValueOrDefault().GetHashCode();
|
||||
}
|
||||
else if (!_to.HasValue)
|
||||
{
|
||||
return _from.GetHashCode();
|
||||
return _from.GetValueOrDefault().GetHashCode();
|
||||
}
|
||||
return _from.GetHashCode() ^ _to.GetHashCode();
|
||||
return _from.GetValueOrDefault().GetHashCode() ^ _to.GetValueOrDefault().GetHashCode();
|
||||
}
|
||||
|
||||
// Returns the length of a range list. E.g. "1-2, 3-4, 5-6" adds 3 ranges to 'rangeCollection'. Note that empty
|
||||
|
|
|
|||
|
|
@ -105,13 +105,13 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
if (Expires.HasValue)
|
||||
{
|
||||
expires = HeaderUtilities.FormatDate(Expires.Value);
|
||||
expires = HeaderUtilities.FormatDate(Expires.GetValueOrDefault());
|
||||
length += SeparatorToken.Length + ExpiresToken.Length + EqualsToken.Length + expires.Length;
|
||||
}
|
||||
|
||||
if (MaxAge.HasValue)
|
||||
{
|
||||
maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds);
|
||||
maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds);
|
||||
length += SeparatorToken.Length + MaxAgeToken.Length + EqualsToken.Length + maxAge.Length;
|
||||
}
|
||||
|
||||
|
|
@ -212,12 +212,12 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
if (Expires.HasValue)
|
||||
{
|
||||
AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value));
|
||||
AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.GetValueOrDefault()));
|
||||
}
|
||||
|
||||
if (MaxAge.HasValue)
|
||||
{
|
||||
AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds));
|
||||
AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds));
|
||||
}
|
||||
|
||||
if (Domain != null)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
{
|
||||
if (_quality.HasValue)
|
||||
{
|
||||
return _value + "; q=" + _quality.Value.ToString("0.0##", NumberFormatInfo.InvariantInfo);
|
||||
return _value + "; q=" + _quality.GetValueOrDefault().ToString("0.0##", NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
return _value.ToString();
|
||||
|
|
@ -83,7 +83,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
// Note that we don't consider double.Epsilon here. We really consider two values equal if they're
|
||||
// actually equal. This makes sure that we also get the same hashcode for two values considered equal
|
||||
// by Equals().
|
||||
return other._quality.HasValue && (_quality.Value == other._quality.Value);
|
||||
return other._quality.HasValue && (_quality.GetValueOrDefault() == other._quality.Value);
|
||||
}
|
||||
|
||||
// If we don't have a quality value, then 'other' must also have no quality assigned in order to be
|
||||
|
|
@ -97,7 +97,7 @@ namespace Microsoft.Net.Http.Headers
|
|||
|
||||
if (_quality.HasValue)
|
||||
{
|
||||
result = result ^ _quality.Value.GetHashCode();
|
||||
result = result ^ _quality.GetValueOrDefault().GetHashCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
|||
Loading…
Reference in New Issue