Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable reference types for some header related classes #1068

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions MimeKit/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
// THE SOFTWARE.
//

#nullable enable

using System;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

using MimeKit.Utils;
Expand All @@ -53,7 +56,7 @@

readonly byte[] rawField;
bool explicitRawValue;
string textValue;
string? textValue;
byte[] rawValue;

/// <summary>
Expand Down Expand Up @@ -678,7 +681,7 @@
int count = 0;

while (index < rawValue.Length) {
ReceivedTokenValue token = null;
ReceivedTokenValue? token = null;
int startIndex = index;

if (!ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false) || index >= rawValue.Length) {
Expand Down Expand Up @@ -1443,6 +1446,7 @@
/// <para>-or-</para>
/// <para><paramref name="value"/> is <see langword="null"/>.</para>
/// </exception>
[MemberNotNull (nameof (rawValue))]

Check failure on line 1449 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, Debug)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1449 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, Release)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1449 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, Release)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1449 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, Debug)

'MemberNotNullAttribute' is inaccessible due to its protection level
public void SetValue (FormatOptions format, Encoding encoding, string value)
{
if (format is null)
Expand Down Expand Up @@ -1483,6 +1487,7 @@
/// <para>-or-</para>
/// <para><paramref name="value"/> is <see langword="null"/>.</para>
/// </exception>
[MemberNotNull (nameof (rawValue))]

Check failure on line 1490 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, Debug)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1490 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, Release)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1490 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, Release)

'MemberNotNullAttribute' is inaccessible due to its protection level

Check failure on line 1490 in MimeKit/Header.cs

View workflow job for this annotation

GitHub Actions / ci (windows-latest, Debug)

'MemberNotNullAttribute' is inaccessible due to its protection level
public void SetValue (Encoding encoding, string value)
{
SetValue (FormatOptions.Default, encoding, value);
Expand Down Expand Up @@ -1575,7 +1580,7 @@
OnChanged ();
}

internal event EventHandler Changed;
internal event EventHandler? Changed;

void OnChanged ()
{
Expand Down Expand Up @@ -1655,7 +1660,7 @@
return c.IsBlank ();
}

internal static unsafe bool TryParse (ParserOptions options, byte* input, int length, bool strict, out Header header)
internal static unsafe bool TryParse (ParserOptions options, byte* input, int length, bool strict, [NotNullWhen (true)] out Header? header)
{
byte* inend = input + length;
byte* start = input;
Expand Down Expand Up @@ -1749,7 +1754,7 @@
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, int length, out Header header)
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, int length, [NotNullWhen (true)] out Header? header)
{
ParseUtils.ValidateArguments (options, buffer, startIndex, length);

Expand Down Expand Up @@ -1779,7 +1784,7 @@
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
public static bool TryParse (byte[] buffer, int startIndex, int length, out Header header)
public static bool TryParse (byte[] buffer, int startIndex, int length, [NotNullWhen (true)] out Header? header)
{
return TryParse (ParserOptions.Default, buffer, startIndex, length, out header);
}
Expand All @@ -1803,7 +1808,7 @@
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, out Header header)
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, [NotNullWhen (true)] out Header? header)
{
ParseUtils.ValidateArguments (options, buffer, startIndex);

Expand Down Expand Up @@ -1832,7 +1837,7 @@
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
public static bool TryParse (byte[] buffer, int startIndex, out Header header)
public static bool TryParse (byte[] buffer, int startIndex, [NotNullWhen (true)] out Header? header)
{
return TryParse (ParserOptions.Default, buffer, startIndex, out header);
}
Expand All @@ -1852,7 +1857,7 @@
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <see langword="null"/>.</para>
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, out Header header)
public static bool TryParse (ParserOptions options, byte[] buffer, [NotNullWhen (true)] out Header? header)
{
return TryParse (options, buffer, 0, out header);
}
Expand All @@ -1869,7 +1874,7 @@
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <see langword="null"/>.
/// </exception>
public static bool TryParse (byte[] buffer, out Header header)
public static bool TryParse (byte[] buffer, [NotNullWhen (true)] out Header? header)
{
return TryParse (ParserOptions.Default, buffer, out header);
}
Expand All @@ -1889,7 +1894,7 @@
/// <para>-or-</para>
/// <para><paramref name="text"/> is <see langword="null"/>.</para>
/// </exception>
public static bool TryParse (ParserOptions options, string text, out Header header)
public static bool TryParse (ParserOptions options, string text, [NotNullWhen (true)] out Header? header)
{
ParseUtils.ValidateArguments (options, text);

Expand All @@ -1914,7 +1919,7 @@
/// <exception cref="System.ArgumentNullException">
/// <paramref name="text"/> is <see langword="null"/>.
/// </exception>
public static bool TryParse (string text, out Header header)
public static bool TryParse (string text, [NotNullWhen (true)] out Header? header)
{
return TryParse (ParserOptions.Default, text, out header);
}
Expand Down
2 changes: 2 additions & 0 deletions MimeKit/HeaderId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// THE SOFTWARE.
//

#nullable enable

using System;
using System.Collections.Generic;

Expand Down
19 changes: 11 additions & 8 deletions MimeKit/HeaderList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
// THE SOFTWARE.
//

#nullable enable

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

using MimeKit.IO;
using MimeKit.Utils;
Expand Down Expand Up @@ -594,7 +597,7 @@ public void Replace (string field, string value)
/// <exception cref="System.ArgumentNullException">
/// <paramref name="value"/> is <see langword="null"/>.
/// </exception>
public string this [HeaderId id] {
public string? this [HeaderId id] {
get {
if (id == HeaderId.Unknown)
throw new ArgumentOutOfRangeException (nameof (id));
Expand Down Expand Up @@ -634,7 +637,7 @@ public string this [HeaderId id] {
/// <para>-or-</para>
/// <para><paramref name="value"/> is <see langword="null"/>.</para>
/// </exception>
public string this [string field] {
public string? this [string field] {
get {
if (field is null)
throw new ArgumentNullException (nameof (field));
Expand Down Expand Up @@ -1205,24 +1208,24 @@ IEnumerator IEnumerable.GetEnumerator ()

#endregion

internal event EventHandler<HeaderListChangedEventArgs> Changed;
internal event EventHandler<HeaderListChangedEventArgs>? Changed;

void HeaderChanged (object sender, EventArgs args)
void HeaderChanged (object? sender, EventArgs args)
{
OnChanged ((Header) sender, HeaderListChangedAction.Changed);
OnChanged (sender as Header, HeaderListChangedAction.Changed);
}

void OnChanged (Header header, HeaderListChangedAction action)
void OnChanged (Header? header, HeaderListChangedAction action)
{
Changed?.Invoke (this, new HeaderListChangedEventArgs (header, action));
}

internal bool TryGetHeader (HeaderId id, out Header header)
internal bool TryGetHeader (HeaderId id, [NotNullWhen (true)] out Header? header)
{
return table.TryGetValue (id.ToHeaderName (), out header);
}

internal bool TryGetHeader (string field, out Header header)
internal bool TryGetHeader (string field, [NotNullWhen (true)] out Header? header)
{
return table.TryGetValue (field, out header);
}
Expand Down
6 changes: 4 additions & 2 deletions MimeKit/HeaderListChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// THE SOFTWARE.
//

#nullable enable

using System;

namespace MimeKit {
Expand Down Expand Up @@ -57,7 +59,7 @@ public enum HeaderListChangedAction {

class HeaderListChangedEventArgs : EventArgs
{
internal HeaderListChangedEventArgs (Header header, HeaderListChangedAction action)
internal HeaderListChangedEventArgs (Header? header, HeaderListChangedAction action)
{
Header = header;
Action = action;
Expand All @@ -67,7 +69,7 @@ public HeaderListChangedAction Action {
get; private set;
}

public Header Header {
public Header? Header {
get; private set;
}
}
Expand Down
6 changes: 4 additions & 2 deletions MimeKit/HeaderListCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// THE SOFTWARE.
//

#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -236,14 +238,14 @@ IEnumerator IEnumerable.GetEnumerator ()
return GetEnumerator ();
}

internal event EventHandler Changed;
internal event EventHandler? Changed;

void OnChanged ()
{
Changed?.Invoke (this, EventArgs.Empty);
}

void OnGroupChanged (object sender, HeaderListChangedEventArgs e)
void OnGroupChanged (object? sender, HeaderListChangedEventArgs e)
{
OnChanged ();
}
Expand Down
Loading