Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 3352e34

Browse files
author
Şafak Gür
committed
Enable NRT except string and URI guards
1 parent 6e32cf4 commit 3352e34

19 files changed

+393
-301
lines changed

src/Guard.Argument.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
namespace Dawn
1+
#nullable enable
2+
3+
namespace Dawn
24
{
35
using System;
6+
using System.ComponentModel;
47
using System.Diagnostics;
58
using System.Linq.Expressions;
69
using System.Runtime.CompilerServices;
@@ -35,7 +38,7 @@ public static partial class Guard
3538
[DebuggerStepThrough]
3639
[GuardFunction("Initialization", "ga", order: 1)]
3740
public static ArgumentInfo<T> Argument<T>(
38-
T value, [InvokerParameterName] string name = null, bool secure = false)
41+
T value, [InvokerParameterName] string? name = null, bool secure = false)
3942
=> new ArgumentInfo<T>(value, name, secure: secure);
4043

4144
/// <summary>
@@ -55,7 +58,7 @@ public static ArgumentInfo<T> Argument<T>(
5558
[GuardFunction("Initialization", order: 2)]
5659
public static ArgumentInfo<T> Argument<T>(Expression<Func<T>> e, bool secure = false)
5760
{
58-
if (e == null)
61+
if (e is null)
5962
throw new ArgumentNullException(nameof(e));
6063

6164
return e.Body is MemberExpression m
@@ -70,7 +73,7 @@ public static ArgumentInfo<T> Argument<T>(Expression<Func<T>> e, bool secure = f
7073
public static Exception Fail(Exception exception)
7174
{
7275
#if !NETSTANDARD1_0
73-
StackTrace stackTrace = null;
76+
StackTrace? stackTrace = null;
7477
for (var scope = Scope.Current; scope != null; scope = scope.Parent)
7578
{
7679
scope.ExceptionInterceptor?.Invoke(
@@ -95,7 +98,7 @@ public readonly partial struct ArgumentInfo<T>
9598
private static readonly string DefaultName = $"The {typeof(T)} argument";
9699

97100
/// <summary>The argument name.</summary>
98-
private readonly string name;
101+
private readonly string? name;
99102

100103
/// <summary>
101104
/// Initializes a new instance of the <see cref="ArgumentInfo{T} " /> struct.
@@ -113,7 +116,7 @@ public readonly partial struct ArgumentInfo<T>
113116
[DebuggerStepThrough]
114117
public ArgumentInfo(
115118
T value,
116-
[InvokerParameterName] string name,
119+
[InvokerParameterName] string? name,
117120
bool modified = false,
118121
bool secure = false)
119122
{
@@ -149,7 +152,7 @@ internal string DebuggerDisplay
149152
get
150153
{
151154
var name = this.name;
152-
var value = this.HasValue() ? this.Value.ToString() : "null";
155+
var value = this.Value?.ToString() ?? "null";
153156
var result = name is null ? value : $"{name}: {value}";
154157
return this.Secure ? $"[SECURE] {result}" : result;
155158
}
@@ -174,12 +177,13 @@ internal string DebuggerDisplay
174177
/// <c>true</c>, if <see cref="Value" /> is <c>null</c>; otherwise, <c>false</c>.
175178
/// </returns>
176179
[Obsolete("Use the HasValue method to check against null.")]
177-
public bool IsNull() => this.Value == null;
180+
[EditorBrowsable(EditorBrowsableState.Never)]
181+
public bool IsNull() => this.Value is null;
178182

179183
/// <summary>Returns the string representation of the argument value.</summary>
180184
/// <returns>String representation of the argument value.</returns>
181185
public override string ToString()
182-
=> this.HasValue() ? this.Value.ToString() : string.Empty;
186+
=> this.Value?.ToString() ?? string.Empty;
183187
}
184188
}
185189
}

src/Guard.Boolean.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Dawn
1+
#nullable enable
2+
3+
namespace Dawn
24
{
35
using System;
46
using System.Diagnostics;
@@ -18,7 +20,7 @@ public static partial class Guard
1820
[DebuggerStepThrough]
1921
[GuardFunction("Boolean", "gtrue")]
2022
public static ref readonly ArgumentInfo<bool> True(
21-
in this ArgumentInfo<bool> argument, string message = null)
23+
in this ArgumentInfo<bool> argument, string? message = null)
2224
{
2325
if (!argument.Value)
2426
{
@@ -40,9 +42,9 @@ public static ref readonly ArgumentInfo<bool> True(
4042
[DebuggerStepThrough]
4143
[GuardFunction("Boolean", "gtrue")]
4244
public static ref readonly ArgumentInfo<bool?> True(
43-
in this ArgumentInfo<bool?> argument, string message = null)
45+
in this ArgumentInfo<bool?> argument, string? message = null)
4446
{
45-
if (argument.HasValue() && !argument.Value.Value)
47+
if (argument.Value == false)
4648
{
4749
var m = message ?? Messages.True(argument);
4850
throw Fail(new ArgumentException(m, argument.Name));
@@ -62,7 +64,7 @@ public static ref readonly ArgumentInfo<bool> True(
6264
[DebuggerStepThrough]
6365
[GuardFunction("Boolean", "gfalse")]
6466
public static ref readonly ArgumentInfo<bool> False(
65-
in this ArgumentInfo<bool> argument, string message = null)
67+
in this ArgumentInfo<bool> argument, string? message = null)
6668
{
6769
if (argument.Value)
6870
{
@@ -84,9 +86,9 @@ public static ref readonly ArgumentInfo<bool> False(
8486
[DebuggerStepThrough]
8587
[GuardFunction("Boolean", "gfalse")]
8688
public static ref readonly ArgumentInfo<bool?> False(
87-
in this ArgumentInfo<bool?> argument, string message = null)
89+
in this ArgumentInfo<bool?> argument, string? message = null)
8890
{
89-
if (argument.HasValue() && argument.Value.Value)
91+
if (argument.GetValueOrDefault())
9092
{
9193
var m = message ?? Messages.False(argument);
9294
throw Fail(new ArgumentException(m, argument.Name));

src/Guard.Double.cs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
namespace Dawn
1+
#nullable enable
2+
3+
namespace Dawn
24
{
35
using System;
6+
using System.ComponentModel;
47
using System.Diagnostics;
58
using JetBrains.Annotations;
69

@@ -29,7 +32,7 @@ public static partial class Guard
2932
[DebuggerStepThrough]
3033
[GuardFunction("Double", "gnan")]
3134
public static ref readonly ArgumentInfo<double> NaN(
32-
in this ArgumentInfo<double> argument, Func<double, string> message = null)
35+
in this ArgumentInfo<double> argument, Func<double, string>? message = null)
3336
{
3437
if (!double.IsNaN(argument.Value))
3538
{
@@ -70,11 +73,11 @@ public static ref readonly ArgumentInfo<double> NaN(
7073
[DebuggerStepThrough]
7174
[GuardFunction("Double", "gnan")]
7275
public static ref readonly ArgumentInfo<double?> NaN(
73-
in this ArgumentInfo<double?> argument, Func<double?, string> message = null)
76+
in this ArgumentInfo<double?> argument, Func<double?, string>? message = null)
7477
{
7578
if (argument.HasValue())
7679
{
77-
var value = argument.Value.Value;
80+
var value = argument.GetValueOrDefault();
7881
if (!double.IsNaN(value))
7982
{
8083
var m = message?.Invoke(value) ?? Messages.NaN(argument);
@@ -108,7 +111,7 @@ public static ref readonly ArgumentInfo<double> NaN(
108111
[DebuggerStepThrough]
109112
[GuardFunction("Double", "gnnan")]
110113
public static ref readonly ArgumentInfo<double> NotNaN(
111-
in this ArgumentInfo<double> argument, string message = null)
114+
in this ArgumentInfo<double> argument, string? message = null)
112115
{
113116
if (double.IsNaN(argument.Value))
114117
{
@@ -142,11 +145,11 @@ public static ref readonly ArgumentInfo<double> NotNaN(
142145
[DebuggerStepThrough]
143146
[GuardFunction("Double", "gnnan")]
144147
public static ref readonly ArgumentInfo<double?> NotNaN(
145-
in this ArgumentInfo<double?> argument, string message = null)
148+
in this ArgumentInfo<double?> argument, string? message = null)
146149
{
147150
if (argument.HasValue())
148151
{
149-
var value = argument.Value.Value;
152+
var value = argument.GetValueOrDefault();
150153
if (double.IsNaN(value))
151154
{
152155
var m = message ?? Messages.NotNaN(argument);
@@ -182,7 +185,7 @@ public static ref readonly ArgumentInfo<double> NotNaN(
182185
[DebuggerStepThrough]
183186
[GuardFunction("Double", "ginf")]
184187
public static ref readonly ArgumentInfo<double> Infinity(
185-
in this ArgumentInfo<double> argument, Func<double, string> message = null)
188+
in this ArgumentInfo<double> argument, Func<double, string>? message = null)
186189
{
187190
if (!double.IsInfinity(argument.Value))
188191
{
@@ -226,11 +229,11 @@ public static ref readonly ArgumentInfo<double> Infinity(
226229
[DebuggerStepThrough]
227230
[GuardFunction("Double", "ginf")]
228231
public static ref readonly ArgumentInfo<double?> Infinity(
229-
in this ArgumentInfo<double?> argument, Func<double?, string> message = null)
232+
in this ArgumentInfo<double?> argument, Func<double?, string>? message = null)
230233
{
231234
if (argument.HasValue())
232235
{
233-
var value = argument.Value.Value;
236+
var value = argument.GetValueOrDefault();
234237
if (!double.IsInfinity(value))
235238
{
236239
var m = message?.Invoke(value) ?? Messages.Infinity(argument);
@@ -264,6 +267,7 @@ public static ref readonly ArgumentInfo<double> Infinity(
264267
[AssertionMethod]
265268
[DebuggerStepThrough]
266269
[Obsolete("Use the NotInfinity overload that accepts the message as a `Func<double, string>`.")]
270+
[EditorBrowsable(EditorBrowsableState.Never)]
267271
public static ref readonly ArgumentInfo<double> NotInfinity(
268272
in this ArgumentInfo<double> argument, string message)
269273
{
@@ -301,7 +305,7 @@ public static ref readonly ArgumentInfo<double> NotInfinity(
301305
[DebuggerStepThrough]
302306
[GuardFunction("Double", "gninf")]
303307
public static ref readonly ArgumentInfo<double> NotInfinity(
304-
in this ArgumentInfo<double> argument, Func<double, string> message = null)
308+
in this ArgumentInfo<double> argument, Func<double, string>? message = null)
305309
{
306310
if (double.IsInfinity(argument.Value))
307311
{
@@ -335,6 +339,7 @@ public static ref readonly ArgumentInfo<double> NotInfinity(
335339
[AssertionMethod]
336340
[DebuggerStepThrough]
337341
[Obsolete("Use the NotInfinity overload that accepts the message as a `Func<double?, string>`.")]
342+
[EditorBrowsable(EditorBrowsableState.Never)]
338343
public static ref readonly ArgumentInfo<double?> NotInfinity(
339344
in this ArgumentInfo<double?> argument, string message)
340345
{
@@ -378,11 +383,11 @@ public static ref readonly ArgumentInfo<double> NotInfinity(
378383
[DebuggerStepThrough]
379384
[GuardFunction("Double", "gninf")]
380385
public static ref readonly ArgumentInfo<double?> NotInfinity(
381-
in this ArgumentInfo<double?> argument, Func<double?, string> message = null)
386+
in this ArgumentInfo<double?> argument, Func<double?, string>? message = null)
382387
{
383388
if (argument.HasValue())
384389
{
385-
var value = argument.Value.Value;
390+
var value = argument.GetValueOrDefault();
386391
if (double.IsInfinity(value))
387392
{
388393
var m = message?.Invoke(value) ?? Messages.NotInfinity(argument);
@@ -417,7 +422,7 @@ public static ref readonly ArgumentInfo<double> NotInfinity(
417422
[DebuggerStepThrough]
418423
[GuardFunction("Double", "gposinf")]
419424
public static ref readonly ArgumentInfo<double> PositiveInfinity(
420-
in this ArgumentInfo<double> argument, Func<double, string> message = null)
425+
in this ArgumentInfo<double> argument, Func<double, string>? message = null)
421426
{
422427
if (!double.IsPositiveInfinity(argument.Value))
423428
{
@@ -458,11 +463,11 @@ public static ref readonly ArgumentInfo<double> PositiveInfinity(
458463
[DebuggerStepThrough]
459464
[GuardFunction("Double", "gposinf")]
460465
public static ref readonly ArgumentInfo<double?> PositiveInfinity(
461-
in this ArgumentInfo<double?> argument, Func<double?, string> message = null)
466+
in this ArgumentInfo<double?> argument, Func<double?, string>? message = null)
462467
{
463468
if (argument.HasValue())
464469
{
465-
var value = argument.Value.Value;
470+
var value = argument.GetValueOrDefault();
466471
if (!double.IsPositiveInfinity(value))
467472
{
468473
var m = message?.Invoke(value) ?? Messages.PositiveInfinity(argument);
@@ -496,7 +501,7 @@ public static ref readonly ArgumentInfo<double> PositiveInfinity(
496501
[DebuggerStepThrough]
497502
[GuardFunction("Double", "gnposinf")]
498503
public static ref readonly ArgumentInfo<double> NotPositiveInfinity(
499-
in this ArgumentInfo<double> argument, string message = null)
504+
in this ArgumentInfo<double> argument, string? message = null)
500505
{
501506
if (double.IsPositiveInfinity(argument.Value))
502507
{
@@ -530,11 +535,11 @@ public static ref readonly ArgumentInfo<double> NotPositiveInfinity(
530535
[DebuggerStepThrough]
531536
[GuardFunction("Double", "gnposinf")]
532537
public static ref readonly ArgumentInfo<double?> NotPositiveInfinity(
533-
in this ArgumentInfo<double?> argument, string message = null)
538+
in this ArgumentInfo<double?> argument, string? message = null)
534539
{
535540
if (argument.HasValue())
536541
{
537-
var value = argument.Value.Value;
542+
var value = argument.GetValueOrDefault();
538543
if (double.IsPositiveInfinity(value))
539544
{
540545
var m = message ?? Messages.NotPositiveInfinity(argument);
@@ -569,7 +574,7 @@ public static ref readonly ArgumentInfo<double> NotPositiveInfinity(
569574
[DebuggerStepThrough]
570575
[GuardFunction("Double", "gneginf")]
571576
public static ref readonly ArgumentInfo<double> NegativeInfinity(
572-
in this ArgumentInfo<double> argument, Func<double, string> message = null)
577+
in this ArgumentInfo<double> argument, Func<double, string>? message = null)
573578
{
574579
if (!double.IsNegativeInfinity(argument.Value))
575580
{
@@ -610,11 +615,11 @@ public static ref readonly ArgumentInfo<double> NegativeInfinity(
610615
[DebuggerStepThrough]
611616
[GuardFunction("Double", "gneginf")]
612617
public static ref readonly ArgumentInfo<double?> NegativeInfinity(
613-
in this ArgumentInfo<double?> argument, Func<double?, string> message = null)
618+
in this ArgumentInfo<double?> argument, Func<double?, string>? message = null)
614619
{
615620
if (argument.HasValue())
616621
{
617-
var value = argument.Value.Value;
622+
var value = argument.GetValueOrDefault();
618623
if (!double.IsNegativeInfinity(value))
619624
{
620625
var m = message?.Invoke(value) ?? Messages.NegativeInfinity(argument);
@@ -648,7 +653,7 @@ public static ref readonly ArgumentInfo<double> NegativeInfinity(
648653
[DebuggerStepThrough]
649654
[GuardFunction("Double", "gnneginf")]
650655
public static ref readonly ArgumentInfo<double> NotNegativeInfinity(
651-
in this ArgumentInfo<double> argument, string message = null)
656+
in this ArgumentInfo<double> argument, string? message = null)
652657
{
653658
if (double.IsNegativeInfinity(argument.Value))
654659
{
@@ -682,11 +687,11 @@ public static ref readonly ArgumentInfo<double> NotNegativeInfinity(
682687
[DebuggerStepThrough]
683688
[GuardFunction("Double", "gnneginf")]
684689
public static ref readonly ArgumentInfo<double?> NotNegativeInfinity(
685-
in this ArgumentInfo<double?> argument, string message = null)
690+
in this ArgumentInfo<double?> argument, string? message = null)
686691
{
687692
if (argument.HasValue())
688693
{
689-
var value = argument.Value.Value;
694+
var value = argument.GetValueOrDefault();
690695
if (double.IsNegativeInfinity(value))
691696
{
692697
var m = message ?? Messages.NotNegativeInfinity(argument);
@@ -722,7 +727,7 @@ public static ref readonly ArgumentInfo<double> Equal(
722727
in this ArgumentInfo<double> argument,
723728
double other,
724729
double delta,
725-
Func<double, double, string> message = null)
730+
Func<double, double, string>? message = null)
726731
{
727732
var diff = Math.Abs(argument.Value - other);
728733
if (diff > delta)
@@ -759,11 +764,11 @@ public static ref readonly ArgumentInfo<double> Equal(
759764
in this ArgumentInfo<double?> argument,
760765
double other,
761766
double delta,
762-
Func<double, double, string> message = null)
767+
Func<double, double, string>? message = null)
763768
{
764769
if (argument.HasValue())
765770
{
766-
var value = argument.Value.Value;
771+
var value = argument.GetValueOrDefault();
767772
var diff = Math.Abs(value - other);
768773
if (diff > delta)
769774
{
@@ -800,7 +805,7 @@ public static ref readonly ArgumentInfo<double> NotEqual(
800805
in this ArgumentInfo<double> argument,
801806
double other,
802807
double delta,
803-
Func<double, double, string> message = null)
808+
Func<double, double, string>? message = null)
804809
{
805810
var diff = Math.Abs(argument.Value - other);
806811
if (diff <= delta)
@@ -837,11 +842,11 @@ public static ref readonly ArgumentInfo<double> NotEqual(
837842
in this ArgumentInfo<double?> argument,
838843
double other,
839844
double delta,
840-
Func<double, double, string> message = null)
845+
Func<double, double, string>? message = null)
841846
{
842847
if (argument.HasValue())
843848
{
844-
var value = argument.Value.Value;
849+
var value = argument.GetValueOrDefault();
845850
var diff = Math.Abs(value - other);
846851
if (diff <= delta)
847852
{

0 commit comments

Comments
 (0)