Skip to content

Commit 7d2fe06

Browse files
committed
Code quality improvements
1 parent 92f8e4b commit 7d2fe06

16 files changed

Lines changed: 58 additions & 79 deletions

MailKit/MailFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public abstract object SyncRoot {
135135
public IMailFolder? ParentFolder {
136136
get { return parent; }
137137
internal protected set {
138-
if (value == parent)
138+
if (object.ReferenceEquals (value, parent))
139139
return;
140140

141141
if (parent != null)

MailKit/Net/Imap/ImapCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static int EstimateStringLength (ImapEngine engine, bool allowAtom, string value
422422

423423
eoln = true;
424424

425-
return length++;
425+
return length;
426426
case ImapStringType.QString:
427427
return Encoding.UTF8.GetByteCount (MimeUtils.Quote (value));
428428
case ImapStringType.Nil:

MailKit/Net/Imap/ImapFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ ImapCommand QueueRenameCommand (IMailFolder parent, string name, CancellationTok
11461146
if (parent == null)
11471147
throw new ArgumentNullException (nameof (parent));
11481148

1149-
if (parent == this)
1149+
if (object.ReferenceEquals (parent, this))
11501150
throw new ArgumentException ("Cannot rename a folder using itself as the new parent folder.", nameof (parent));
11511151

11521152
if (parent is not ImapFolder || ((ImapFolder) parent).Engine != Engine)

MailKit/Net/Imap/ImapFolderFetch.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,12 +1794,12 @@ async Task<MimeEntity> ParseEntityAsync (Stream stream, bool dispose, Cancellati
17941794

17951795
class Section
17961796
{
1797-
public int Index;
1797+
public readonly int Index;
17981798
public UniqueId? UniqueId;
1799-
public Stream Stream;
1800-
public string Name;
1801-
public int Offset;
1802-
public int Length;
1799+
public readonly Stream Stream;
1800+
public readonly string Name;
1801+
public readonly int Offset;
1802+
public readonly int Length;
18031803

18041804
public Section (Stream stream, int index, UniqueId? uid, string name, int offset, int length)
18051805
{

MailKit/Net/Imap/ImapResponseCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ internal NewNameResponseCode (ImapResponseCodeType type) : base (type, false)
220220

221221
class PermanentFlagsResponseCode : ImapResponseCode
222222
{
223-
public HashSet<string> Keywords;
223+
public readonly HashSet<string> Keywords;
224224
public MessageFlags Flags;
225225

226226
internal PermanentFlagsResponseCode (ImapResponseCodeType type) : base (type, false)

MailKit/Net/NetworkStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void Cleanup ()
132132
if (send != null) {
133133
send.Completed -= AsyncOperationCompleted;
134134
send.AcceptSocket = null;
135-
send?.Dispose ();
135+
send.Dispose ();
136136
send = null;
137137
}
138138

MailKit/Net/Proxy/HttpProxyClient.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,19 @@ public override Stream Connect (string host, int port, CancellationToken cancell
185185
try {
186186
Send (socket, command, 0, command.Length, cancellationToken);
187187

188-
var builder = new ByteArrayBuilder (256);
188+
using var builder = new ByteArrayBuilder (256);
189189
var buffer = new byte[1];
190190
var newline = false;
191-
string response;
192191

193-
try {
194-
// read until we consume the end of the headers
195-
do {
196-
int nread = Receive (socket, buffer, 0, 1, cancellationToken);
192+
// read until we consume the end of the headers
193+
do {
194+
int nread = Receive (socket, buffer, 0, 1, cancellationToken);
197195

198-
if (nread < 1 || TryConsumeHeaders (builder, buffer[0], ref newline))
199-
break;
200-
} while (true);
196+
if (nread < 1 || TryConsumeHeaders (builder, buffer[0], ref newline))
197+
break;
198+
} while (true);
201199

202-
response = builder.ToString ();
203-
} finally {
204-
builder.Dispose ();
205-
}
200+
var response = builder.ToString ();
206201

207202
ValidateHttpResponse (response, host, port);
208203
return new NetworkStream (socket, true);
@@ -254,24 +249,19 @@ public override async Task<Stream> ConnectAsync (string host, int port, Cancella
254249
try {
255250
await SendAsync (socket, command, 0, command.Length, cancellationToken).ConfigureAwait (false);
256251

257-
var builder = new ByteArrayBuilder (256);
252+
using var builder = new ByteArrayBuilder (256);
258253
var buffer = new byte[1];
259254
var newline = false;
260-
string response;
261255

262-
try {
263-
// read until we consume the end of the headers
264-
do {
265-
int nread = await ReceiveAsync (socket, buffer, 0, 1, cancellationToken).ConfigureAwait (false);
256+
// read until we consume the end of the headers
257+
do {
258+
int nread = await ReceiveAsync (socket, buffer, 0, 1, cancellationToken).ConfigureAwait (false);
266259

267-
if (nread < 1 || TryConsumeHeaders (builder, buffer[0], ref newline))
268-
break;
269-
} while (true);
260+
if (nread < 1 || TryConsumeHeaders (builder, buffer[0], ref newline))
261+
break;
262+
} while (true);
270263

271-
response = builder.ToString ();
272-
} finally {
273-
builder.Dispose ();
274-
}
264+
var response = builder.ToString ();
275265

276266
ValidateHttpResponse (response, host, port);
277267
return new NetworkStream (socket, true);

MailKit/Net/Proxy/HttpsProxyClient.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -294,24 +294,19 @@ public override Stream Connect (string host, int port, CancellationToken cancell
294294
try {
295295
ssl.Write (command, 0, command.Length);
296296

297-
var builder = new ByteArrayBuilder (256);
297+
using var builder = new ByteArrayBuilder (256);
298298
var buffer = new byte[1];
299299
var newline = false;
300-
string response;
301300

302-
try {
303-
// read until we consume the end of the headers
304-
do {
305-
int nread = ssl.Read (buffer, 0, 1);
301+
// read until we consume the end of the headers
302+
do {
303+
int nread = ssl.Read (buffer, 0, 1);
306304

307-
if (nread < 1 || HttpProxyClient.TryConsumeHeaders (builder, buffer[0], ref newline))
308-
break;
309-
} while (true);
305+
if (nread < 1 || HttpProxyClient.TryConsumeHeaders (builder, buffer[0], ref newline))
306+
break;
307+
} while (true);
310308

311-
response = builder.ToString ();
312-
} finally {
313-
builder.Dispose ();
314-
}
309+
var response = builder.ToString ();
315310

316311
HttpProxyClient.ValidateHttpResponse (response, host, port);
317312
return ssl;
@@ -375,24 +370,19 @@ public override async Task<Stream> ConnectAsync (string host, int port, Cancella
375370
try {
376371
await ssl.WriteAsync (command, 0, command.Length, cancellationToken).ConfigureAwait (false);
377372

378-
var builder = new ByteArrayBuilder (256);
373+
using var builder = new ByteArrayBuilder (256);
379374
var buffer = new byte[1];
380375
var newline = false;
381-
string response;
382376

383-
try {
384-
// read until we consume the end of the headers
385-
do {
386-
int nread = ssl.Read (buffer, 0, 1);
377+
// read until we consume the end of the headers
378+
do {
379+
int nread = ssl.Read (buffer, 0, 1);
387380

388-
if (HttpProxyClient.TryConsumeHeaders (builder, buffer[0], ref newline))
389-
break;
390-
} while (true);
381+
if (HttpProxyClient.TryConsumeHeaders (builder, buffer[0], ref newline))
382+
break;
383+
} while (true);
391384

392-
response = builder.ToString ();
393-
} finally {
394-
builder.Dispose ();
395-
}
385+
var response = builder.ToString ();
396386

397387
HttpProxyClient.ValidateHttpResponse (response, host, port);
398388
return ssl;

MailKit/Net/Smtp/SmtpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public partial class SmtpClient : MailTransport, ISmtpClient
7676
{
7777
static readonly byte[] EndData = Encoding.ASCII.GetBytes (".\r\n");
7878
static readonly char[] NewLineCharacters = { '\r', '\n' };
79-
internal static string DefaultLocalDomain;
79+
internal static readonly string DefaultLocalDomain;
8080
const int MaxLineLength = 998;
8181

8282
enum SmtpCommand {
@@ -672,7 +672,7 @@ void QueueCommand (SmtpCommand type, string command, CancellationToken cancellat
672672
struct QueueResults
673673
{
674674
public readonly int RecipientsAccepted;
675-
public Exception? FirstException;
675+
public readonly Exception? FirstException;
676676

677677
public QueueResults (int recipientsAccepted, Exception? firstException)
678678
{

MailKit/Net/Smtp/SmtpStream.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,6 @@ public override async Task WriteAsync (byte[] buffer, int offset, int count, Can
927927
ValidateArguments (buffer, offset, count);
928928

929929
try {
930-
var network = NetworkStream.Get (Stream);
931930
int index = offset;
932931
int left = count;
933932

0 commit comments

Comments
 (0)