From c9a04c516f97de8387a9a9cd99e48a9c1cee0aaa Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 26 Jun 2023 09:23:54 -0700 Subject: [PATCH 1/7] Update SDK --- global.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/global.json b/global.json index 2e6bd7e72d6e..eb4e1da39b59 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "8.0.100-preview.7.23321.23" + "version": "8.0.100-preview.7.23325.3" }, "tools": { - "dotnet": "8.0.100-preview.7.23321.23", + "dotnet": "8.0.100-preview.7.23325.3", "runtimes": { "dotnet/x86": [ "$(MicrosoftNETCoreBrowserDebugHostTransportVersion)" From 7c384b95c1b1c0184b9feffa6c3cf7d8ac7d14d5 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 07:36:37 -0700 Subject: [PATCH 2/7] Update SDK again --- global.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/global.json b/global.json index eb4e1da39b59..8486404f3e97 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "8.0.100-preview.7.23325.3" + "version": "8.0.100-preview.7.23356.2" }, "tools": { - "dotnet": "8.0.100-preview.7.23325.3", + "dotnet": "8.0.100-preview.7.23356.2", "runtimes": { "dotnet/x86": [ "$(MicrosoftNETCoreBrowserDebugHostTransportVersion)" From e1b574c01b45416201704139d120a739e6bce6bb Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 10:17:16 -0700 Subject: [PATCH 3/7] Fix analyzer warnings - And update SDK again --- global.json | 4 ++-- .../FrameworkParametersCompletionProvider.cs | 2 +- .../RoutePatternCompletionProvider.cs | 2 +- .../src/ContentDispositionHeaderValue.cs | 6 ++--- .../Headers/src/ContentRangeHeaderValue.cs | 10 ++++----- src/Http/Headers/src/RangeItemHeaderValue.cs | 22 ++++++++++--------- .../src/StringWithQualityHeaderValue.cs | 6 ++--- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/global.json b/global.json index 8486404f3e97..a3cb17bf4ea3 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "8.0.100-preview.7.23356.2" + "version": "8.0.100-preview.7.23356.5" }, "tools": { - "dotnet": "8.0.100-preview.7.23356.2", + "dotnet": "8.0.100-preview.7.23356.5", "runtimes": { "dotnet/x86": [ "$(MicrosoftNETCoreBrowserDebugHostTransportVersion)" diff --git a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs index 40f7e607ef3c..cbf3846cc212 100644 --- a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs +++ b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs @@ -263,7 +263,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context) if (change.NewPosition != null) { - properties.Add(NewPositionKey, change.NewPosition.ToString()); + properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture)); } // Keep everything sorted in the order we just produced the items in. diff --git a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs index 26afcc85d64b..6b50c1cc28eb 100644 --- a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs +++ b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs @@ -142,7 +142,7 @@ not CompletionTriggerKind.InvokeAndCommitIfUnique and if (change.NewPosition != null) { - properties.Add(NewPositionKey, change.NewPosition.ToString()); + properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture)); } // Keep everything sorted in the order we just produced the items in. diff --git a/src/Http/Headers/src/ContentDispositionHeaderValue.cs b/src/Http/Headers/src/ContentDispositionHeaderValue.cs index 31b88417b603..9b385f6a89ff 100644 --- a/src/Http/Headers/src/ContentDispositionHeaderValue.cs +++ b/src/Http/Headers/src/ContentDispositionHeaderValue.cs @@ -183,11 +183,11 @@ public long? Size } else if (sizeParameter != null) { - sizeParameter.Value = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); + sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture); } else { - var sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); + var sizeString = value.Value.ToString(CultureInfo.InvariantCulture); Parameters.Add(new NameValueHeaderValue(SizeString, sizeString)); } } @@ -384,7 +384,7 @@ private void SetDate(string parameter, DateTimeOffset? date) else { // Must always be quoted - var dateString = HeaderUtilities.FormatDate(date.GetValueOrDefault(), quoted: true); + var dateString = HeaderUtilities.FormatDate(date.Value, quoted: true); if (dateParameter != null) { dateParameter.Value = dateString; diff --git a/src/Http/Headers/src/ContentRangeHeaderValue.cs b/src/Http/Headers/src/ContentRangeHeaderValue.cs index 79a80ff351b0..d71268948c80 100644 --- a/src/Http/Headers/src/ContentRangeHeaderValue.cs +++ b/src/Http/Headers/src/ContentRangeHeaderValue.cs @@ -159,12 +159,12 @@ public override int GetHashCode() if (HasRange) { - result = result ^ From.GetHashCode() ^ To.GetHashCode(); + result = result ^ From.Value.GetHashCode() ^ To.Value.GetHashCode(); } if (HasLength) { - result = result ^ Length.GetHashCode(); + result = result ^ Length.Value.GetHashCode(); } return result; @@ -179,9 +179,9 @@ public override string ToString() if (HasRange) { - sb.Append(From.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(From.Value.ToString(NumberFormatInfo.InvariantInfo)); sb.Append('-'); - sb.Append(To.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(To.Value.ToString(NumberFormatInfo.InvariantInfo)); } else { @@ -191,7 +191,7 @@ public override string ToString() sb.Append('/'); if (HasLength) { - sb.Append(Length.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(Length.Value.ToString(NumberFormatInfo.InvariantInfo)); } else { diff --git a/src/Http/Headers/src/RangeItemHeaderValue.cs b/src/Http/Headers/src/RangeItemHeaderValue.cs index b95a04dfdd42..b9fdc09af8ee 100644 --- a/src/Http/Headers/src/RangeItemHeaderValue.cs +++ b/src/Http/Headers/src/RangeItemHeaderValue.cs @@ -30,15 +30,15 @@ public RangeItemHeaderValue(long? from, long? to) { throw new ArgumentException("Invalid header range."); } - if (from.HasValue && (from.GetValueOrDefault() < 0)) + if (from.HasValue && (from.Value < 0)) { throw new ArgumentOutOfRangeException(nameof(from)); } - if (to.HasValue && (to.GetValueOrDefault() < 0)) + if (to.HasValue && (to.Value < 0)) { throw new ArgumentOutOfRangeException(nameof(to)); } - if (from.HasValue && to.HasValue && (from.GetValueOrDefault() > to.GetValueOrDefault())) + if (from.HasValue && to.HasValue && (from.Value > to.Value)) { throw new ArgumentOutOfRangeException(nameof(from)); } @@ -68,14 +68,15 @@ public override string ToString() { if (!_from.HasValue) { - return "-" + _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo); + // The ctor prevents both _from and _to from being null. + return "-" + _to!.Value.ToString(NumberFormatInfo.InvariantInfo); } else if (!_to.HasValue) { - return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-"; + return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-"; } - return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-" + - _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo); + return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-" + + _to.Value.ToString(NumberFormatInfo.InvariantInfo); } /// @@ -89,13 +90,14 @@ public override int GetHashCode() { if (!_from.HasValue) { - return _to.GetValueOrDefault().GetHashCode(); + // The ctor prevents both _from and _to from being null. + return _to!.Value.GetHashCode(); } else if (!_to.HasValue) { - return _from.GetValueOrDefault().GetHashCode(); + return _from.Value.GetHashCode(); } - return _from.GetValueOrDefault().GetHashCode() ^ _to.GetValueOrDefault().GetHashCode(); + return _from.Value.GetHashCode() ^ _to.Value.GetHashCode(); } // Returns the length of a range list. E.g. "1-2, 3-4, 5-6" adds 3 ranges to 'rangeCollection'. Note that empty diff --git a/src/Http/Headers/src/StringWithQualityHeaderValue.cs b/src/Http/Headers/src/StringWithQualityHeaderValue.cs index aecf2125ff8d..49229bde3d01 100644 --- a/src/Http/Headers/src/StringWithQualityHeaderValue.cs +++ b/src/Http/Headers/src/StringWithQualityHeaderValue.cs @@ -70,7 +70,7 @@ public override string ToString() { if (_quality.HasValue) { - return _value + "; q=" + _quality.GetValueOrDefault().ToString("0.0##", NumberFormatInfo.InvariantInfo); + return _value + "; q=" + _quality.Value.ToString("0.0##", NumberFormatInfo.InvariantInfo); } return _value.ToString(); @@ -96,7 +96,7 @@ public override bool Equals(object? obj) // 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.GetValueOrDefault() == other._quality.Value); + return other._quality.HasValue && (_quality.Value == other._quality.Value); } // If we don't have a quality value, then 'other' must also have no quality assigned in order to be @@ -111,7 +111,7 @@ public override int GetHashCode() if (_quality.HasValue) { - result = result ^ _quality.GetValueOrDefault().GetHashCode(); + result = result ^ _quality.Value.GetHashCode(); } return result; From bbb5842b5e23c0720529b5e7b8e6080c93536095 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 10:34:53 -0700 Subject: [PATCH 4/7] Fix more analyzer warnings --- global.json | 4 ++-- src/Http/WebUtilities/src/MultipartReaderStream.cs | 4 ++-- src/Http/WebUtilities/src/StreamHelperExtensions.cs | 4 ++-- .../Transport.Quic/src/Internal/QuicConnectionContext.cs | 2 +- .../Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs | 4 ++-- .../common/Http.Connections.Common/src/NegotiateProtocol.cs | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/global.json b/global.json index a3cb17bf4ea3..c91c6190bc19 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "8.0.100-preview.7.23356.5" + "version": "8.0.100-preview.7.23356.6" }, "tools": { - "dotnet": "8.0.100-preview.7.23356.5", + "dotnet": "8.0.100-preview.7.23356.6", "runtimes": { "dotnet/x86": [ "$(MicrosoftNETCoreBrowserDebugHostTransportVersion)" diff --git a/src/Http/WebUtilities/src/MultipartReaderStream.cs b/src/Http/WebUtilities/src/MultipartReaderStream.cs index 5bffde8e2d31..026ed04d3e19 100644 --- a/src/Http/WebUtilities/src/MultipartReaderStream.cs +++ b/src/Http/WebUtilities/src/MultipartReaderStream.cs @@ -145,9 +145,9 @@ private int UpdatePosition(int read) if (_observedLength < _position) { _observedLength = _position; - if (LengthLimit.HasValue && _observedLength > LengthLimit.GetValueOrDefault()) + if (LengthLimit.HasValue && _observedLength > LengthLimit.Value) { - throw new InvalidDataException($"Multipart body length limit {LengthLimit.GetValueOrDefault()} exceeded."); + throw new InvalidDataException($"Multipart body length limit {LengthLimit.Value} exceeded."); } } return read; diff --git a/src/Http/WebUtilities/src/StreamHelperExtensions.cs b/src/Http/WebUtilities/src/StreamHelperExtensions.cs index 4c12e9445bd9..7e304f68992e 100644 --- a/src/Http/WebUtilities/src/StreamHelperExtensions.cs +++ b/src/Http/WebUtilities/src/StreamHelperExtensions.cs @@ -64,9 +64,9 @@ public static async Task DrainAsync(this Stream stream, ArrayPool bytePool { // Not all streams support cancellation directly. cancellationToken.ThrowIfCancellationRequested(); - if (limit.HasValue && limit.GetValueOrDefault() - total < read) + if (limit.HasValue && limit.Value - total < read) { - throw new InvalidDataException($"The stream exceeded the data limit {limit.GetValueOrDefault()}."); + throw new InvalidDataException($"The stream exceeded the data limit {limit.Value}."); } total += read; read = await stream.ReadAsync(buffer.AsMemory(), cancellationToken); diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs index ff02fd3421d5..5ab2d08c6cd9 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs @@ -131,7 +131,7 @@ public override void Abort(ConnectionAbortedException abortReason) { // Shutdown initiated by peer, abortive. _error = ex.ApplicationErrorCode; - QuicLog.ConnectionAborted(_log, this, ex.ApplicationErrorCode.GetValueOrDefault(), ex); + QuicLog.ConnectionAborted(_log, this, ex.ApplicationErrorCode ?? 0, ex); ThreadPool.UnsafeQueueUserWorkItem(state => { diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs index d81f0ec4757a..76e46d9535ad 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs @@ -274,7 +274,7 @@ private async ValueTask DoReceiveAsync() { // Abort from peer. _error = ex.ApplicationErrorCode; - QuicLog.StreamAbortedRead(_log, this, ex.ApplicationErrorCode.GetValueOrDefault()); + QuicLog.StreamAbortedRead(_log, this, ex.ApplicationErrorCode ?? 0); // This could be ignored if _shutdownReason is already set. error = new ConnectionResetException(ex.Message, ex); @@ -435,7 +435,7 @@ private async ValueTask DoSendAsync() { // Abort from peer. _error = ex.ApplicationErrorCode; - QuicLog.StreamAbortedWrite(_log, this, ex.ApplicationErrorCode.GetValueOrDefault()); + QuicLog.StreamAbortedWrite(_log, this, ex.ApplicationErrorCode ?? 0); // This could be ignored if _shutdownReason is already set. shutdownReason = new ConnectionResetException(ex.Message, ex); diff --git a/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs b/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs index 8ff9ba259ed1..beb7b6ef095f 100644 --- a/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs +++ b/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs @@ -186,7 +186,7 @@ public static NegotiationResponse ParseResponse(ReadOnlySpan content) } else if (reader.ValueTextEquals(NegotiateVersionPropertyNameBytes.EncodedUtf8Bytes)) { - version = reader.ReadAsInt32(NegotiateVersionPropertyName).GetValueOrDefault(); + version = reader.ReadAsInt32(NegotiateVersionPropertyName) ?? 0; } else if (reader.ValueTextEquals(AvailableTransportsPropertyNameBytes.EncodedUtf8Bytes)) { From 8b47e3d6a5083cb62586fa020c9bff51f7642f6c Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 11:03:26 -0700 Subject: [PATCH 5/7] More analyzer workarounds --- .../src/Pagination/Paginator.razor.cs | 2 +- src/Http/Http.Extensions/src/SendFileResponseExtensions.cs | 2 +- src/Http/Shared/StreamCopyOperationInternal.cs | 6 +++--- src/Shared/ProblemDetails/ProblemDetailsDefaults.cs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs index bd34bec6272d..3228d090674f 100644 --- a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs +++ b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs @@ -34,7 +34,7 @@ public Paginator() private Task GoFirstAsync() => GoToPageAsync(0); private Task GoPreviousAsync() => GoToPageAsync(State.CurrentPageIndex - 1); private Task GoNextAsync() => GoToPageAsync(State.CurrentPageIndex + 1); - private Task GoLastAsync() => GoToPageAsync(State.LastPageIndex.GetValueOrDefault(0)); + private Task GoLastAsync() => GoToPageAsync(State.LastPageIndex ?? 0); private bool CanGoBack => State.CurrentPageIndex > 0; private bool CanGoForwards => State.CurrentPageIndex < State.LastPageIndex; diff --git a/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs b/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs index 3b00124f51b6..f99e4eed330d 100644 --- a/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs +++ b/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs @@ -129,7 +129,7 @@ private static void CheckRange(long offset, long? count, long fileLength) throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty); } if (count.HasValue && - (count.GetValueOrDefault() < 0 || count.GetValueOrDefault() > fileLength - offset)) + (count.Value < 0 || count.Value > fileLength - offset)) { throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty); } diff --git a/src/Http/Shared/StreamCopyOperationInternal.cs b/src/Http/Shared/StreamCopyOperationInternal.cs index 7c111ea2a632..9e0a7b0d5802 100644 --- a/src/Http/Shared/StreamCopyOperationInternal.cs +++ b/src/Http/Shared/StreamCopyOperationInternal.cs @@ -38,13 +38,13 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co { Debug.Assert(source != null); Debug.Assert(destination != null); - Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.GetValueOrDefault() >= 0); + Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0); Debug.Assert(buffer != null); while (true) { // The natural end of the range. - if (bytesRemaining.HasValue && bytesRemaining.GetValueOrDefault() <= 0) + if (bytesRemaining.HasValue && bytesRemaining.Value <= 0) { return; } @@ -54,7 +54,7 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co var readLength = buffer.Length; if (bytesRemaining.HasValue) { - readLength = (int)Math.Min(bytesRemaining.GetValueOrDefault(), (long)readLength); + readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength); } var read = await source.ReadAsync(buffer.AsMemory(0, readLength), cancel); diff --git a/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs b/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs index f02a32dcc202..d17aaa88dad5 100644 --- a/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs +++ b/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs @@ -126,7 +126,7 @@ public static void Apply(ProblemDetails problemDetails, int? statusCode) } } - var status = problemDetails.Status.GetValueOrDefault(); + var status = problemDetails.Status.Value; if (Defaults.TryGetValue(status, out var defaults)) { problemDetails.Title ??= defaults.Title; From c12fa5239270cce7fac4677451f8292245922242 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 11:18:07 -0700 Subject: [PATCH 6/7] done yet? --- src/Http/Http/src/Features/FormFeature.cs | 2 +- src/Http/Http/src/HeaderDictionary.cs | 2 +- src/Middleware/Rewrite/src/RedirectToHttpsRule.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Http/src/Features/FormFeature.cs b/src/Http/Http/src/Features/FormFeature.cs index 6a3558a46134..b3ddeaa61550 100644 --- a/src/Http/Http/src/Features/FormFeature.cs +++ b/src/Http/Http/src/Features/FormFeature.cs @@ -212,7 +212,7 @@ private async Task InnerReadFormAsync(CancellationToken cancell if (section.BaseStreamOffset.HasValue) { // Relative reference to buffered request body - file = new FormFile(_request.Body, section.BaseStreamOffset.GetValueOrDefault(), section.Body.Length, name, fileName); + file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName); } else { diff --git a/src/Http/Http/src/HeaderDictionary.cs b/src/Http/Http/src/HeaderDictionary.cs index 7b8c0246b4cc..b4aa54e327aa 100644 --- a/src/Http/Http/src/HeaderDictionary.cs +++ b/src/Http/Http/src/HeaderDictionary.cs @@ -136,7 +136,7 @@ public long? ContentLength ThrowIfReadOnly(); if (value.HasValue) { - this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault()); + this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.Value); } else { diff --git a/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs b/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs index 6dbaee5a7ea1..2d89cb649d3a 100644 --- a/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs +++ b/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs @@ -18,7 +18,7 @@ public void ApplyRule(RewriteContext context) { var host = context.HttpContext.Request.Host; int port; - if (SSLPort.HasValue && (port = SSLPort.GetValueOrDefault()) > 0) + if (SSLPort.HasValue && (port = SSLPort.Value) > 0) { // a specific SSL port is specified host = new HostString(host.Host, port); From 0c92494a73dc5ce819f1f5e98c9383d6265f61eb Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 6 Jul 2023 11:35:17 -0700 Subject: [PATCH 7/7] Revert analyzer workarounds and update .editorconfig --- .editorconfig | 3 ++- .../src/Pagination/Paginator.razor.cs | 2 +- .../FrameworkParametersCompletionProvider.cs | 2 +- .../RoutePatternCompletionProvider.cs | 2 +- .../src/ContentDispositionHeaderValue.cs | 6 ++--- .../Headers/src/ContentRangeHeaderValue.cs | 10 ++++----- src/Http/Headers/src/RangeItemHeaderValue.cs | 22 +++++++++---------- .../src/StringWithQualityHeaderValue.cs | 6 ++--- .../src/SendFileResponseExtensions.cs | 2 +- src/Http/Http/src/Features/FormFeature.cs | 2 +- src/Http/Http/src/HeaderDictionary.cs | 2 +- .../Shared/StreamCopyOperationInternal.cs | 6 ++--- .../WebUtilities/src/MultipartReaderStream.cs | 4 ++-- .../src/StreamHelperExtensions.cs | 4 ++-- .../Rewrite/src/RedirectToHttpsRule.cs | 2 +- .../src/Internal/QuicConnectionContext.cs | 2 +- .../src/Internal/QuicStreamContext.cs | 4 ++-- .../ProblemDetails/ProblemDetailsDefaults.cs | 2 +- .../src/NegotiateProtocol.cs | 2 +- 19 files changed, 42 insertions(+), 43 deletions(-) diff --git a/.editorconfig b/.editorconfig index fa462349198b..d24633a2c7bc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -87,7 +87,8 @@ dotnet_diagnostic.CA1018.severity = warning dotnet_diagnostic.CA1047.severity = warning # CA1305: Specify IFormatProvider -dotnet_diagnostic.CA1305.severity = warning +# TODO: Renable as warning after https://github.com/dotnet/roslyn-analyzers/issues/6746 is resolved +dotnet_diagnostic.CA1305.severity = suggestion # CA1507: Use nameof to express symbol names dotnet_diagnostic.CA1507.severity = warning diff --git a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs index 3228d090674f..bd34bec6272d 100644 --- a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs +++ b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Pagination/Paginator.razor.cs @@ -34,7 +34,7 @@ public Paginator() private Task GoFirstAsync() => GoToPageAsync(0); private Task GoPreviousAsync() => GoToPageAsync(State.CurrentPageIndex - 1); private Task GoNextAsync() => GoToPageAsync(State.CurrentPageIndex + 1); - private Task GoLastAsync() => GoToPageAsync(State.LastPageIndex ?? 0); + private Task GoLastAsync() => GoToPageAsync(State.LastPageIndex.GetValueOrDefault(0)); private bool CanGoBack => State.CurrentPageIndex > 0; private bool CanGoForwards => State.CurrentPageIndex < State.LastPageIndex; diff --git a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs index cbf3846cc212..40f7e607ef3c 100644 --- a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs +++ b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs @@ -263,7 +263,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context) if (change.NewPosition != null) { - properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture)); + properties.Add(NewPositionKey, change.NewPosition.ToString()); } // Keep everything sorted in the order we just produced the items in. diff --git a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs index 6b50c1cc28eb..26afcc85d64b 100644 --- a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs +++ b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs @@ -142,7 +142,7 @@ not CompletionTriggerKind.InvokeAndCommitIfUnique and if (change.NewPosition != null) { - properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture)); + properties.Add(NewPositionKey, change.NewPosition.ToString()); } // Keep everything sorted in the order we just produced the items in. diff --git a/src/Http/Headers/src/ContentDispositionHeaderValue.cs b/src/Http/Headers/src/ContentDispositionHeaderValue.cs index 9b385f6a89ff..31b88417b603 100644 --- a/src/Http/Headers/src/ContentDispositionHeaderValue.cs +++ b/src/Http/Headers/src/ContentDispositionHeaderValue.cs @@ -183,11 +183,11 @@ public long? Size } else if (sizeParameter != null) { - sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture); + sizeParameter.Value = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); } else { - var sizeString = value.Value.ToString(CultureInfo.InvariantCulture); + var sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); Parameters.Add(new NameValueHeaderValue(SizeString, sizeString)); } } @@ -384,7 +384,7 @@ private void SetDate(string parameter, DateTimeOffset? date) 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; diff --git a/src/Http/Headers/src/ContentRangeHeaderValue.cs b/src/Http/Headers/src/ContentRangeHeaderValue.cs index d71268948c80..79a80ff351b0 100644 --- a/src/Http/Headers/src/ContentRangeHeaderValue.cs +++ b/src/Http/Headers/src/ContentRangeHeaderValue.cs @@ -159,12 +159,12 @@ public override int GetHashCode() if (HasRange) { - result = result ^ From.Value.GetHashCode() ^ To.Value.GetHashCode(); + result = result ^ From.GetHashCode() ^ To.GetHashCode(); } if (HasLength) { - result = result ^ Length.Value.GetHashCode(); + result = result ^ Length.GetHashCode(); } return result; @@ -179,9 +179,9 @@ public override string ToString() 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 { @@ -191,7 +191,7 @@ public override string ToString() sb.Append('/'); if (HasLength) { - sb.Append(Length.Value.ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(Length.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); } else { diff --git a/src/Http/Headers/src/RangeItemHeaderValue.cs b/src/Http/Headers/src/RangeItemHeaderValue.cs index b9fdc09af8ee..b95a04dfdd42 100644 --- a/src/Http/Headers/src/RangeItemHeaderValue.cs +++ b/src/Http/Headers/src/RangeItemHeaderValue.cs @@ -30,15 +30,15 @@ public RangeItemHeaderValue(long? from, long? to) { 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)); } @@ -68,15 +68,14 @@ public override string ToString() { if (!_from.HasValue) { - // The ctor prevents both _from and _to from being null. - 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); } /// @@ -90,14 +89,13 @@ public override int GetHashCode() { if (!_from.HasValue) { - // The ctor prevents both _from and _to from being null. - return _to!.Value.GetHashCode(); + return _to.GetValueOrDefault().GetHashCode(); } else if (!_to.HasValue) { - return _from.Value.GetHashCode(); + return _from.GetValueOrDefault().GetHashCode(); } - return _from.Value.GetHashCode() ^ _to.Value.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 diff --git a/src/Http/Headers/src/StringWithQualityHeaderValue.cs b/src/Http/Headers/src/StringWithQualityHeaderValue.cs index 49229bde3d01..aecf2125ff8d 100644 --- a/src/Http/Headers/src/StringWithQualityHeaderValue.cs +++ b/src/Http/Headers/src/StringWithQualityHeaderValue.cs @@ -70,7 +70,7 @@ public override string ToString() { 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(); @@ -96,7 +96,7 @@ public override bool Equals(object? obj) // 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 @@ -111,7 +111,7 @@ public override int GetHashCode() if (_quality.HasValue) { - result = result ^ _quality.Value.GetHashCode(); + result = result ^ _quality.GetValueOrDefault().GetHashCode(); } return result; diff --git a/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs b/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs index f99e4eed330d..3b00124f51b6 100644 --- a/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs +++ b/src/Http/Http.Extensions/src/SendFileResponseExtensions.cs @@ -129,7 +129,7 @@ private static void CheckRange(long offset, long? count, long fileLength) 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); } diff --git a/src/Http/Http/src/Features/FormFeature.cs b/src/Http/Http/src/Features/FormFeature.cs index b3ddeaa61550..6a3558a46134 100644 --- a/src/Http/Http/src/Features/FormFeature.cs +++ b/src/Http/Http/src/Features/FormFeature.cs @@ -212,7 +212,7 @@ private async Task InnerReadFormAsync(CancellationToken cancell 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 { diff --git a/src/Http/Http/src/HeaderDictionary.cs b/src/Http/Http/src/HeaderDictionary.cs index b4aa54e327aa..7b8c0246b4cc 100644 --- a/src/Http/Http/src/HeaderDictionary.cs +++ b/src/Http/Http/src/HeaderDictionary.cs @@ -136,7 +136,7 @@ public long? ContentLength ThrowIfReadOnly(); if (value.HasValue) { - this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.Value); + this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault()); } else { diff --git a/src/Http/Shared/StreamCopyOperationInternal.cs b/src/Http/Shared/StreamCopyOperationInternal.cs index 9e0a7b0d5802..7c111ea2a632 100644 --- a/src/Http/Shared/StreamCopyOperationInternal.cs +++ b/src/Http/Shared/StreamCopyOperationInternal.cs @@ -38,13 +38,13 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co { 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; } @@ -54,7 +54,7 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co var readLength = buffer.Length; if (bytesRemaining.HasValue) { - readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength); + readLength = (int)Math.Min(bytesRemaining.GetValueOrDefault(), (long)readLength); } var read = await source.ReadAsync(buffer.AsMemory(0, readLength), cancel); diff --git a/src/Http/WebUtilities/src/MultipartReaderStream.cs b/src/Http/WebUtilities/src/MultipartReaderStream.cs index 026ed04d3e19..5bffde8e2d31 100644 --- a/src/Http/WebUtilities/src/MultipartReaderStream.cs +++ b/src/Http/WebUtilities/src/MultipartReaderStream.cs @@ -145,9 +145,9 @@ private int UpdatePosition(int read) 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; diff --git a/src/Http/WebUtilities/src/StreamHelperExtensions.cs b/src/Http/WebUtilities/src/StreamHelperExtensions.cs index 7e304f68992e..4c12e9445bd9 100644 --- a/src/Http/WebUtilities/src/StreamHelperExtensions.cs +++ b/src/Http/WebUtilities/src/StreamHelperExtensions.cs @@ -64,9 +64,9 @@ public static async Task DrainAsync(this Stream stream, ArrayPool bytePool { // 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.AsMemory(), cancellationToken); diff --git a/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs b/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs index 2d89cb649d3a..6dbaee5a7ea1 100644 --- a/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs +++ b/src/Middleware/Rewrite/src/RedirectToHttpsRule.cs @@ -18,7 +18,7 @@ public void ApplyRule(RewriteContext context) { var host = context.HttpContext.Request.Host; int port; - if (SSLPort.HasValue && (port = SSLPort.Value) > 0) + if (SSLPort.HasValue && (port = SSLPort.GetValueOrDefault()) > 0) { // a specific SSL port is specified host = new HostString(host.Host, port); diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs index 5ab2d08c6cd9..ff02fd3421d5 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs @@ -131,7 +131,7 @@ public override void Abort(ConnectionAbortedException abortReason) { // Shutdown initiated by peer, abortive. _error = ex.ApplicationErrorCode; - QuicLog.ConnectionAborted(_log, this, ex.ApplicationErrorCode ?? 0, ex); + QuicLog.ConnectionAborted(_log, this, ex.ApplicationErrorCode.GetValueOrDefault(), ex); ThreadPool.UnsafeQueueUserWorkItem(state => { diff --git a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs index 76e46d9535ad..d81f0ec4757a 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs @@ -274,7 +274,7 @@ private async ValueTask DoReceiveAsync() { // Abort from peer. _error = ex.ApplicationErrorCode; - QuicLog.StreamAbortedRead(_log, this, ex.ApplicationErrorCode ?? 0); + QuicLog.StreamAbortedRead(_log, this, ex.ApplicationErrorCode.GetValueOrDefault()); // This could be ignored if _shutdownReason is already set. error = new ConnectionResetException(ex.Message, ex); @@ -435,7 +435,7 @@ private async ValueTask DoSendAsync() { // Abort from peer. _error = ex.ApplicationErrorCode; - QuicLog.StreamAbortedWrite(_log, this, ex.ApplicationErrorCode ?? 0); + QuicLog.StreamAbortedWrite(_log, this, ex.ApplicationErrorCode.GetValueOrDefault()); // This could be ignored if _shutdownReason is already set. shutdownReason = new ConnectionResetException(ex.Message, ex); diff --git a/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs b/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs index d17aaa88dad5..f02a32dcc202 100644 --- a/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs +++ b/src/Shared/ProblemDetails/ProblemDetailsDefaults.cs @@ -126,7 +126,7 @@ public static void Apply(ProblemDetails problemDetails, int? statusCode) } } - var status = problemDetails.Status.Value; + var status = problemDetails.Status.GetValueOrDefault(); if (Defaults.TryGetValue(status, out var defaults)) { problemDetails.Title ??= defaults.Title; diff --git a/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs b/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs index beb7b6ef095f..8ff9ba259ed1 100644 --- a/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs +++ b/src/SignalR/common/Http.Connections.Common/src/NegotiateProtocol.cs @@ -186,7 +186,7 @@ public static NegotiationResponse ParseResponse(ReadOnlySpan content) } else if (reader.ValueTextEquals(NegotiateVersionPropertyNameBytes.EncodedUtf8Bytes)) { - version = reader.ReadAsInt32(NegotiateVersionPropertyName) ?? 0; + version = reader.ReadAsInt32(NegotiateVersionPropertyName).GetValueOrDefault(); } else if (reader.ValueTextEquals(AvailableTransportsPropertyNameBytes.EncodedUtf8Bytes)) {