Skip to content

Commit f7ac673

Browse files
committed
Ensure non-nullability, validate encoding, and clean up nullable qualifiers
* `SerializerBase.SerializationContext`: Ensure it is initialized and non-null by design * `ServiceProvider.GetService(...)` returns a non-null instance or throws an exception if the service is unavailable. * `EncodingProvider.DecoderDelegate` throws for invalid encoding argument * `EncoderDelegate GetEncoderFor` throws for invalid encoding argument * Remove unneccessary conditional access qualifier according to nullable reference types' annotations
1 parent 4f031e5 commit f7ac673

28 files changed

+86
-87
lines changed

Ical.Net.Tests/EncodingProviderTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#nullable enable
77
using System;
8+
using System.Runtime.Serialization;
89
using Ical.Net.Serialization;
910
using NUnit.Framework;
1011
using EncodingProvider = Ical.Net.Serialization.EncodingProvider;
@@ -33,7 +34,7 @@ public void Encode_ShouldBeNull_WhenInvalidEncodingIsProvided()
3334
const string encoding = "Invalid-Encoding";
3435
var data = "Hello"u8.ToArray();
3536

36-
Assert.That(GetEncodingProvider().Encode(encoding, data), Is.Null);
37+
Assert.That(() => GetEncodingProvider().Encode(encoding, data), Throws.TypeOf<SerializationException>());
3738
}
3839

3940
[Test]
@@ -53,7 +54,7 @@ public void Decode_ShouldBeNull_WhenInvalidEncodingIsProvided()
5354
const string encoding = "Invalid-Encoding";
5455
const string data = "Hello";
5556

56-
Assert.That(GetEncodingProvider().DecodeString(encoding, data), Is.Null);
57+
Assert.That(() => GetEncodingProvider().DecodeString(encoding, data), Throws.TypeOf<SerializationException>());
5758
}
5859

5960
[Test]

Ical.Net/Calendar.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public override int GetHashCode()
121121
{
122122
unchecked
123123
{
124-
var hashCode = Name?.GetHashCode() ?? 0;
124+
var hashCode = Name.GetHashCode();
125125
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(UniqueComponents);
126126
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(Events);
127127
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(Todos);
@@ -290,8 +290,6 @@ public virtual void MergeWith(IMergeable obj)
290290
return;
291291
}
292292

293-
Name ??= c.Name;
294-
295293
Method = c.Method;
296294
Version = c.Version;
297295
ProductId = c.ProductId;

Ical.Net/CalendarComponents/Alarm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void AddRepeatedItems(List<AlarmOccurrence> occurrences)
172172
for (var i = 0; i < len; i++)
173173
{
174174
var ao = occurrences[i];
175-
if (ao?.DateTime == null || ao.Component == null)
175+
if (ao.DateTime == null || ao.Component == null)
176176
{
177177
continue;
178178
}

Ical.Net/CalendarComponents/Todo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual int PercentComplete
8585
public virtual IList<string> Resources
8686
{
8787
get => Properties.GetMany<string>("RESOURCES");
88-
set => Properties.Set("RESOURCES", value ?? new List<string>());
88+
set => Properties.Set("RESOURCES", value);
8989
}
9090

9191
/// <summary>

Ical.Net/CalendarComponents/VTimeZone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public override int GetHashCode()
383383
{
384384
unchecked
385385
{
386-
var hashCode = Name?.GetHashCode() ?? 0;
386+
var hashCode = Name.GetHashCode();
387387
hashCode = (hashCode * 397) ^ (TzId?.GetHashCode() ?? 0);
388388
hashCode = (hashCode * 397) ^ (Url?.GetHashCode() ?? 0);
389389
return hashCode;

Ical.Net/Collections/Proxies/GroupedValueListProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void IterateValues(Func<IValueObject<TOriginalValue>, int, int, bool> ac
6464
foreach (var obj in _realObject)
6565
{
6666
// Get the number of items of the target value i this object
67-
var count = obj.Values?.OfType<TNewValue>().Count() ?? 0;
67+
var count = obj.Values.OfType<TNewValue>().Count();
6868

6969
// Perform some action on this item
7070
if (!action(obj, i, count))

Ical.Net/DataTypes/Attachment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public override int GetHashCode()
113113
unchecked
114114
{
115115
var hashCode = Uri?.GetHashCode() ?? 0;
116-
hashCode = (hashCode * 397) ^ (CollectionHelpers.GetHashCode(Data));
117-
hashCode = (hashCode * 397) ^ (ValueEncoding?.GetHashCode() ?? 0);
116+
hashCode = (hashCode * 397) ^ (Data != null ? CollectionHelpers.GetHashCode(Data) : 0);
117+
hashCode = (hashCode * 397) ^ (ValueEncoding.GetHashCode());
118118
return hashCode;
119119
}
120120
}

Ical.Net/DataTypes/Occurrence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override int GetHashCode()
4141
{
4242
unchecked
4343
{
44-
return ((Period?.GetHashCode() ?? 0) * 397) ^ (Source?.GetHashCode() ?? 0);
44+
return ((Period.GetHashCode()) * 397) ^ (Source.GetHashCode());
4545
}
4646
}
4747

Ical.Net/DataTypes/PeriodList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void Insert(int index, Period item)
125125
}
126126

127127
/// <inheritdoc/>
128-
public void RemoveAt(int index) => Periods?.RemoveAt(index);
128+
public void RemoveAt(int index) => Periods.RemoveAt(index);
129129

130130
/// <summary>
131131
/// Adds a <see cref="Period"/> to the list if it does not already exist.<br/>
@@ -159,7 +159,7 @@ public void Add(CalDateTime dt)
159159
public bool Contains(Period item) => Periods.Contains(item);
160160

161161
/// <inheritdoc/>
162-
public void CopyTo(Period[] array, int arrayIndex) => Periods?.CopyTo(array, arrayIndex);
162+
public void CopyTo(Period[] array, int arrayIndex) => Periods.CopyTo(array, arrayIndex);
163163

164164
/// <inheritdoc/>
165165
public IEnumerator<Period> GetEnumerator() => Periods.GetEnumerator();

Ical.Net/DataTypes/Trigger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public override int GetHashCode()
119119
{
120120
var hashCode = _mDateTime?.GetHashCode() ?? 0;
121121
hashCode = (hashCode * 397) ^ _mDuration.GetHashCode();
122-
hashCode = (hashCode * 397) ^ _mRelated?.GetHashCode() ?? 0;
122+
hashCode = (hashCode * 397) ^ _mRelated.GetHashCode();
123123
return hashCode;
124124
}
125125
}

0 commit comments

Comments
 (0)