Skip to content

Commit 2375707

Browse files
committed
Merge to patterns cleanup
1 parent a770d79 commit 2375707

8 files changed

Lines changed: 15 additions & 21 deletions

File tree

src/Npgsql/Internal/NpgsqlConnector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ internal async Task Open(NpgsqlTimeout timeout, bool async, CancellationToken ca
504504
SerializerOptions = DataSource.SerializerOptions;
505505
DatabaseInfo = DataSource.DatabaseInfo;
506506

507-
if (Settings.Pooling && !Settings.Multiplexing && !Settings.NoResetOnClose && DatabaseInfo.SupportsDiscard)
507+
if (Settings.Pooling && Settings is { Multiplexing: false, NoResetOnClose: false } && DatabaseInfo.SupportsDiscard)
508508
{
509509
_sendResetOnClose = true;
510510
GenerateResetMessage();
@@ -1145,7 +1145,7 @@ void SetSocketOptions(Socket socket)
11451145

11461146
if (Settings.TcpKeepAlive)
11471147
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
1148-
if (Settings.TcpKeepAliveInterval > 0 && Settings.TcpKeepAliveTime == 0)
1148+
if (Settings is { TcpKeepAliveInterval: > 0, TcpKeepAliveTime: 0 })
11491149
throw new ArgumentException("If TcpKeepAliveInterval is defined, TcpKeepAliveTime must be defined as well");
11501150
if (Settings.TcpKeepAliveTime > 0)
11511151
{
@@ -2001,7 +2001,7 @@ internal async Task CloseOngoingOperations(bool async)
20012001
// therefore vulnerable to the race condition in #615.
20022002
if (copyOperation is NpgsqlBinaryImporter ||
20032003
copyOperation is NpgsqlCopyTextWriter ||
2004-
copyOperation is NpgsqlRawCopyStream rawCopyStream && rawCopyStream.CanWrite)
2004+
copyOperation is NpgsqlRawCopyStream { CanWrite: true })
20052005
{
20062006
try
20072007
{
@@ -2676,7 +2676,7 @@ internal async Task<bool> Wait(bool async, int timeout, CancellationToken cancel
26762676
{
26772677
msg = await ReadMessageWithNotifications(async).ConfigureAwait(false);
26782678
}
2679-
catch (Exception e) when (e is OperationCanceledException || e is NpgsqlException npgEx && npgEx.InnerException is TimeoutException)
2679+
catch (Exception e) when (e is OperationCanceledException || e is NpgsqlException { InnerException: TimeoutException })
26802680
{
26812681
// We're somewhere in the middle of a reading keepalive messages
26822682
// Breaking the connection, as we've lost protocol sync

src/Npgsql/Internal/NpgsqlReadBuffer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ async ValueTask<int> ReadWithTimeoutAsync(Memory<byte> buffer, CancellationToken
223223
// If we should attempt PostgreSQL cancellation, do it the first time we get a timeout.
224224
// TODO: As an optimization, we can still attempt to send a cancellation request, but after
225225
// that immediately break the connection
226-
if (connector.AttemptPostgresCancellation &&
227-
!connector.PostgresCancellationPerformed &&
226+
if (connector is { AttemptPostgresCancellation: true, PostgresCancellationPerformed: false } &&
228227
connector.PerformPostgresCancellation())
229228
{
230229
// Note that if the cancellation timeout is negative, we flow down and break the
@@ -350,8 +349,7 @@ static async ValueTask EnsureLong(
350349
// If we should attempt PostgreSQL cancellation, do it the first time we get a timeout.
351350
// TODO: As an optimization, we can still attempt to send a cancellation request, but after
352351
// that immediately break the connection
353-
if (connector.AttemptPostgresCancellation &&
354-
!connector.PostgresCancellationPerformed &&
352+
if (connector is { AttemptPostgresCancellation: true, PostgresCancellationPerformed: false } &&
355353
connector.PerformPostgresCancellation())
356354
{
357355
// Note that if the cancellation timeout is negative, we flow down and break the

src/Npgsql/NpgsqlBatchCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ internal RowDescriptionMessage? Description
183183
/// </summary>
184184
internal PreparedStatement? PreparedStatement
185185
{
186-
get => _preparedStatement != null && _preparedStatement.State == PreparedState.Unprepared
186+
get => _preparedStatement is { State: PreparedState.Unprepared }
187187
? _preparedStatement = null
188188
: _preparedStatement;
189189
set => _preparedStatement = value;

src/Npgsql/NpgsqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ internal void ProcessRawQuery(SqlQueryParser? parser, bool standardConformingStr
10311031

10321032
static void ValidateParameterCount(NpgsqlBatchCommand batchCommand)
10331033
{
1034-
if (batchCommand.HasParameters && batchCommand.PositionalParameters.Count > ushort.MaxValue)
1034+
if (batchCommand is { HasParameters: true, PositionalParameters.Count: > ushort.MaxValue })
10351035
ThrowHelper.ThrowNpgsqlException("A statement cannot have more than 65535 parameters");
10361036
}
10371037
}

src/Npgsql/NpgsqlRawCopyStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ async ValueTask DisposeAsync(bool disposing, bool async)
383383
}
384384
_connector.SkipUntil(BackendMessageCode.ReadyForQuery);
385385
}
386-
catch (OperationCanceledException e) when (e.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.QueryCanceled)
386+
catch (OperationCanceledException e) when (e.InnerException is PostgresException { SqlState: PostgresErrorCodes.QueryCanceled })
387387
{
388388
LogMessages.CopyOperationCancelled(_copyLogger, _connector.Id);
389389
}

src/Npgsql/NpgsqlTypes/NpgsqlTsVector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static NpgsqlTsVector Parse(string value)
189189
if (value[pos] >= 'B' && value[pos] <= 'D' || value[pos] >= 'b' && value[pos] <= 'd')
190190
{
191191
var weight = value[pos];
192-
if (weight >= 'b' && weight <= 'd')
192+
if (weight is >= 'b' and <= 'd')
193193
weight = (char)(weight - ('b' - 'B'));
194194
wordEntryPositions.Add(new Lexeme.WordEntryPos(wordPos, Lexeme.Weight.D + ('D' - weight)));
195195
pos++;

test/Npgsql.Tests/CommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public async Task Timeout_from_connection_string()
243243
public async Task Timeout_switch_connection()
244244
{
245245
var csb = new NpgsqlConnectionStringBuilder(ConnectionString);
246-
if (csb.CommandTimeout >= 100 && csb.CommandTimeout < 105)
246+
if (csb.CommandTimeout is >= 100 and < 105)
247247
IgnoreExceptOnBuildServer("Bad default command timeout");
248248

249249
await using var dataSource1 = CreateDataSource(ConnectionString + ";CommandTimeout=100");

test/Npgsql.Tests/ConnectionTests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ public async Task Basic_lifecycle()
3232

3333
conn.StateChange += (s, e) =>
3434
{
35-
if (e.OriginalState == ConnectionState.Closed &&
36-
e.CurrentState == ConnectionState.Open)
35+
if (e is { OriginalState: ConnectionState.Closed, CurrentState: ConnectionState.Open })
3736
eventOpen = true;
3837

39-
if (e.OriginalState == ConnectionState.Open &&
40-
e.CurrentState == ConnectionState.Closed)
38+
if (e is { OriginalState: ConnectionState.Open, CurrentState: ConnectionState.Closed })
4139
eventClosed = true;
4240
};
4341

@@ -83,12 +81,10 @@ public async Task Broken_lifecycle([Values] bool openFromClose)
8381

8482
conn.StateChange += (s, e) =>
8583
{
86-
if (e.OriginalState == ConnectionState.Closed &&
87-
e.CurrentState == ConnectionState.Open)
84+
if (e is { OriginalState: ConnectionState.Closed, CurrentState: ConnectionState.Open })
8885
eventOpen = true;
8986

90-
if (e.OriginalState == ConnectionState.Open &&
91-
e.CurrentState == ConnectionState.Closed)
87+
if (e is { OriginalState: ConnectionState.Open, CurrentState: ConnectionState.Closed })
9288
eventClosed = true;
9389
};
9490

0 commit comments

Comments
 (0)