Skip to content

Commit c660b52

Browse files
committed
Code analysis fixes
1 parent fd43ac6 commit c660b52

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/Lucene.Net.Replicator/Http/HttpClientBase.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Lucene.Net.Diagnostics;
21
using Lucene.Net.Support;
32
using System;
43
using System.IO;
@@ -7,6 +6,8 @@
76
using System.Net.Http;
87
using System.Threading;
98
using System.Threading.Tasks;
9+
// ReSharper disable VirtualMemberNeverOverridden.Global
10+
// ReSharper disable MemberCanBePrivate.Global
1011

1112
#nullable enable
1213

@@ -396,7 +397,8 @@ protected virtual T DoAction<T>(HttpResponseMessage response, bool consume, Func
396397
}
397398
}
398399
}
399-
if (Debugging.AssertsEnabled) Debugging.Assert(th != null); // extra safety - if we get here, it means the Func<T> failed
400+
401+
// if (Debugging.AssertsEnabled) Debugging.Assert(th != null); // LUCENENET: Removed assertion because it'll never be null here, ensured by NRT
400402
Util.IOUtils.ReThrow(th);
401403
return default!; // silly, if we're here, IOUtils.reThrow always throws an exception
402404
}
@@ -408,7 +410,7 @@ protected virtual T DoAction<T>(HttpResponseMessage response, bool consume, Func
408410
/// </summary>
409411
protected virtual async Task<T> DoActionAsync<T>(HttpResponseMessage response, bool consume, Func<Task<T>> call)
410412
{
411-
Exception? th = null;
413+
Exception? th /* = null */;
412414
try
413415
{
414416
VerifyStatus(response);
@@ -440,8 +442,8 @@ protected virtual async Task<T> DoActionAsync<T>(HttpResponseMessage response, b
440442
}
441443
}
442444

443-
if (Debugging.AssertsEnabled) Debugging.Assert(th != null);
444-
Util.IOUtils.ReThrow(th!);
445+
// if (Debugging.AssertsEnabled) Debugging.Assert(th != null); // LUCENENET: Removed assertion because it'll never be null here, ensured by NRT
446+
Util.IOUtils.ReThrow(th);
445447
return default!; // never reached, rethrow above always throws
446448
}
447449

@@ -496,7 +498,7 @@ private static void ConsumeQuietly(HttpResponseMessage response)
496498
private class ConsumingStream : Stream
497499
{
498500
private readonly Stream input;
499-
private bool consumed = false;
501+
private bool consumed /* = false */;
500502

501503
public ConsumingStream(Stream input)
502504
{

src/Lucene.Net.Replicator/Http/HttpReplicator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ public virtual void Release(string sessionId)
128128
public async Task<SessionToken?> CheckForUpdateAsync(string? currentVersion, CancellationToken cancellationToken = default)
129129
{
130130
string[]? parameters = !string.IsNullOrEmpty(currentVersion)
131-
? new[] { ReplicationService.REPLICATE_VERSION_PARAM, currentVersion! }
131+
? new[] { ReplicationService.REPLICATE_VERSION_PARAM, currentVersion! } // [!]: verified above
132132
: null;
133133

134134
using var response = await ExecuteGetAsync(
135-
ReplicationService.ReplicationAction.UPDATE.ToString(),
136-
parameters ?? Array.Empty<string>(),
135+
nameof(ReplicationService.ReplicationAction.UPDATE),
136+
parameters,
137137
cancellationToken: cancellationToken).ConfigureAwait(false);
138138

139139
return await DoActionAsync(response, async () =>
140140
{
141+
// ReSharper disable once AccessToDisposedClosure - DoActionAsync definitively returns after this lambda is invoked
141142
using var inputStream = new DataInputStream(
142143
await GetResponseStreamAsync(response, cancellationToken).ConfigureAwait(false));
143144

@@ -156,16 +157,16 @@ public virtual void Release(string sessionId)
156157
public async Task<Stream> ObtainFileAsync(string sessionId, string source, string fileName, CancellationToken cancellationToken = default)
157158
{
158159
using var response = await ExecuteGetAsync(
159-
ReplicationService.ReplicationAction.OBTAIN.ToString(),
160+
nameof(ReplicationService.ReplicationAction.OBTAIN),
160161
ReplicationService.REPLICATE_SESSION_ID_PARAM, sessionId,
161162
ReplicationService.REPLICATE_SOURCE_PARAM, source,
162163
ReplicationService.REPLICATE_FILENAME_PARAM, fileName,
163164
cancellationToken: cancellationToken).ConfigureAwait(false);
164165

165-
return await DoActionAsync(response, async () =>
166-
{
167-
return await GetResponseStreamAsync(response, cancellationToken).ConfigureAwait(false);
168-
}).ConfigureAwait(false);
166+
return await DoActionAsync(response,
167+
// ReSharper disable once AccessToDisposedClosure - DoActionAsync definitively returns after this lambda is invoked
168+
async () => await GetResponseStreamAsync(response, cancellationToken).ConfigureAwait(false))
169+
.ConfigureAwait(false);
169170
}
170171

171172
/// <summary>
@@ -191,7 +192,7 @@ public Task PublishAsync(IRevision revision, CancellationToken cancellationToken
191192
public async Task ReleaseAsync(string sessionId, CancellationToken cancellationToken = default)
192193
{
193194
using var response = await ExecuteGetAsync(
194-
ReplicationService.ReplicationAction.RELEASE.ToString(),
195+
nameof(ReplicationService.ReplicationAction.RELEASE),
195196
ReplicationService.REPLICATE_SESSION_ID_PARAM, sessionId,
196197
cancellationToken: cancellationToken).ConfigureAwait(false);
197198

@@ -206,4 +207,3 @@ await DoActionAsync(response, () =>
206207

207208
}
208209
}
209-
#nullable restore

src/Lucene.Net/Util/IOUtils.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
1011
using System.Runtime.CompilerServices;
1112
using System.Runtime.ExceptionServices;
@@ -627,13 +628,14 @@ public static void Copy(FileInfo source, FileInfo target)
627628
}
628629
}
629630

631+
#nullable enable
630632
/// <summary>
631-
/// Simple utilty method that takes a previously caught
633+
/// Simple utility method that takes a previously caught
632634
/// <see cref="Exception"/> and rethrows either
633635
/// <see cref="IOException"/> or an unchecked exception. If the
634636
/// argument is <c>null</c> then this method does nothing.
635637
/// </summary>
636-
public static void ReThrow(Exception th)
638+
public static void ReThrow(Exception? th) // LUCENENET TODO: determine if we can remove the nullability here
637639
{
638640
if (th != null)
639641
{
@@ -646,11 +648,11 @@ public static void ReThrow(Exception th)
646648
}
647649

648650
/// <summary>
649-
/// Simple utilty method that takes a previously caught
651+
/// Simple utility method that takes a previously caught
650652
/// <see cref="Exception"/> and rethrows it as an unchecked exception.
651653
/// If the argument is <c>null</c> then this method does nothing.
652654
/// </summary>
653-
public static void ReThrowUnchecked(Exception th)
655+
public static void ReThrowUnchecked(Exception? th) // LUCENENET TODO: determine if we can remove the nullability here
654656
{
655657
if (th != null)
656658
{
@@ -661,6 +663,7 @@ public static void ReThrowUnchecked(Exception th)
661663
throw RuntimeException.Create(th);
662664
}
663665
}
666+
#nullable restore
664667

665668
// LUCENENET specific: using string instead of FileSystemInfo to avoid extra allocation
666669
public static void Fsync(string fileToSync, bool isDir)

0 commit comments

Comments
 (0)