Skip to content

Commit 17d2895

Browse files
committed
Fix a few nullability warnings when compiling against modern .NET
1 parent 6039eb6 commit 17d2895

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

fNbt/NullableSupport.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal sealed class NullableSupport {
99
}
1010
}
1111

12+
#if !NETCOREAPP
1213
// Copied from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
1314
// and changed from public to internal.
1415
namespace System.Diagnostics.CodeAnalysis
@@ -28,3 +29,4 @@ internal sealed class DoesNotReturnIfAttribute : Attribute
2829
public bool ParameterValue { get; }
2930
}
3031
}
32+
#endif

fNbt/Tags/NbtCompound.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override NbtTag? this[string tagName] {
9595
/// <exception cref="InvalidCastException"> If tag could not be cast to the desired tag. </exception>
9696
public T? Get<T>(string tagName) where T : NbtTag {
9797
if (tagName == null) throw new ArgumentNullException(nameof(tagName));
98-
NbtTag result;
98+
NbtTag? result;
9999
if (tags.TryGetValue(tagName, out result)) {
100100
return (T)result;
101101
}
@@ -110,7 +110,7 @@ public override NbtTag? this[string tagName] {
110110
/// <exception cref="InvalidCastException"> If tag could not be cast to the desired tag. </exception>
111111
public NbtTag? Get(string tagName) {
112112
if (tagName == null) throw new ArgumentNullException(nameof(tagName));
113-
NbtTag result;
113+
NbtTag? result;
114114
if (tags.TryGetValue(tagName, out result)) {
115115
return result;
116116
}
@@ -128,7 +128,7 @@ public override NbtTag? this[string tagName] {
128128
/// <exception cref="InvalidCastException"> If tag could not be cast to the desired tag. </exception>
129129
public bool TryGet<T>(string tagName, out T? result) where T : NbtTag {
130130
if (tagName == null) throw new ArgumentNullException(nameof(tagName));
131-
NbtTag tempResult;
131+
NbtTag? tempResult;
132132
if (tags.TryGetValue(tagName, out tempResult)) {
133133
result = (T)tempResult;
134134
return true;
@@ -147,7 +147,7 @@ public bool TryGet<T>(string tagName, out T? result) where T : NbtTag {
147147
/// <exception cref="ArgumentNullException"> <paramref name="tagName"/> is <c>null</c>. </exception>
148148
public bool TryGet(string tagName, out NbtTag? result) {
149149
if (tagName == null) throw new ArgumentNullException(nameof(tagName));
150-
NbtTag tempResult;
150+
NbtTag? tempResult;
151151
if (tags.TryGetValue(tagName, out tempResult)) {
152152
result = tempResult;
153153
return true;
@@ -188,7 +188,7 @@ public bool Contains(string tagName) {
188188
/// <exception cref="ArgumentNullException"> <paramref name="tagName"/> is <c>null</c>. </exception>
189189
public bool Remove(string tagName) {
190190
if (tagName == null) throw new ArgumentNullException(nameof(tagName));
191-
NbtTag tag;
191+
NbtTag? tag;
192192
if (!tags.TryGetValue(tagName, out tag)) {
193193
return false;
194194
}
@@ -467,7 +467,7 @@ public void CopyTo(NbtTag[] array, int arrayIndex) {
467467
public bool Remove(NbtTag tag) {
468468
if (tag == null) throw new ArgumentNullException(nameof(tag));
469469
if (tag.Name == null) throw new ArgumentException("Trying to remove an unnamed tag.");
470-
NbtTag maybeItem;
470+
NbtTag? maybeItem;
471471
if (tags.TryGetValue(tag.Name, out maybeItem)) {
472472
if (maybeItem == tag && tags.Remove(tag.Name)) {
473473
tag.Parent = null;

fNbt/Tags/NbtList.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -481,35 +481,35 @@ bool ICollection<NbtTag>.IsReadOnly {
481481

482482
#region Implementation of IList and ICollection
483483

484-
void IList.Remove(object value) {
485-
Remove((NbtTag)value);
484+
void IList.Remove(object? value) {
485+
Remove((NbtTag)value!);
486486
}
487487

488488

489-
object IList.this[int tagIndex] {
489+
object? IList.this[int tagIndex] {
490490
get { return tags[tagIndex]; }
491-
set { this[tagIndex] = (NbtTag)value; }
491+
set { this[tagIndex] = (NbtTag)value!; }
492492
}
493493

494494

495-
int IList.Add(object value) {
496-
Add((NbtTag)value);
495+
int IList.Add(object? value) {
496+
Add((NbtTag)value!);
497497
return (tags.Count - 1);
498498
}
499499

500500

501-
bool IList.Contains(object value) {
502-
return tags.Contains((NbtTag)value);
501+
bool IList.Contains(object? value) {
502+
return tags.Contains((NbtTag)value!);
503503
}
504504

505505

506-
int IList.IndexOf(object value) {
507-
return tags.IndexOf((NbtTag)value);
506+
int IList.IndexOf(object? value) {
507+
return tags.IndexOf((NbtTag)value!);
508508
}
509509

510510

511-
void IList.Insert(int index, object value) {
512-
Insert(index, (NbtTag)value);
511+
void IList.Insert(int index, object? value) {
512+
Insert(index, (NbtTag)value!);
513513
}
514514

515515

fNbt/Tags/NbtTag.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public string Path {
8181

8282

8383
#region Shortcuts
84+
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
8485

8586
/// <summary> Gets or sets the tag with the specified name. May return <c>null</c>. </summary>
8687
/// <returns> The tag with the specified key. Null if tag with the given name was not found. </returns>
@@ -287,7 +288,7 @@ public string StringValue {
287288
}
288289
}
289290
}
290-
291+
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
291292
#endregion
292293

293294

0 commit comments

Comments
 (0)