Skip to content

Commit 44ad090

Browse files
lduchosalclaude
andcommitted
refactor: enable nullable, WarningLevel 5 and EnforceCodeStyleInBuild on all projects
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 595447d commit 44ad090

File tree

71 files changed

+997
-970
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+997
-970
lines changed

src/ConsoleApplication/ConsoleApplication.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1313
<PublishAot>true</PublishAot>
1414
<IsAotCompatible>true</IsAotCompatible>
15+
<NoWarn>SA1010</NoWarn>
16+
<Nullable>enable</Nullable>
17+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
18+
<WarningLevel>5</WarningLevel>
19+
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
1520
</PropertyGroup>
1621

1722
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
18-
<NoWarn />
19-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
20-
</PropertyGroup>
21-
22-
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
23-
<NoWarn />
24-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
23+
<DefineConstants>$(DefineConstants)TRACE</DefineConstants>
2524
</PropertyGroup>
2625

2726
<ItemGroup>

src/ConsoleApplication/Program.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace System.Net;
66

77
using Gnu.Getopt;
88
using System.Collections.Generic;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
1011
using System.Numerics;
1112

@@ -59,7 +60,7 @@ public static class Program
5960
}),
6061
new ArgParsed('C', (ac, arg) =>
6162
{
62-
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
63+
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
6364
{
6465
Console.WriteLine($"Unable to parse ipnetwork {arg}", arg);
6566
ac.Action = Action.Usage;
@@ -71,7 +72,7 @@ public static class Program
7172
}),
7273
new ArgParsed('o', (ac, arg) =>
7374
{
74-
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
75+
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
7576
{
7677
Console.WriteLine("Unable to parse ipnetwork {0}", arg);
7778
ac.Action = Action.Usage;
@@ -83,7 +84,7 @@ public static class Program
8384
}),
8485
new ArgParsed('S', (ac, arg) =>
8586
{
86-
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
87+
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
8788
{
8889
Console.WriteLine("Unable to parse ipnetwork {0}", arg);
8990
ac.Action = Action.Usage;
@@ -156,6 +157,11 @@ private static void ListIPAddress(ProgramContext ac)
156157

157158
private static void SubtractNetwork(ProgramContext ac)
158159
{
160+
if (ac.SubtractNetwork is null)
161+
{
162+
return;
163+
}
164+
159165
foreach (IPNetwork2 ipnetwork in ac.Networks)
160166
{
161167
foreach (IPNetwork2 subtracted in ipnetwork.Subtract(ac.SubtractNetwork))
@@ -164,9 +170,14 @@ private static void SubtractNetwork(ProgramContext ac)
164170
}
165171
}
166172
}
167-
173+
168174
private static void ContainNetwork(ProgramContext ac)
169175
{
176+
if (ac.ContainNetwork is null)
177+
{
178+
return;
179+
}
180+
170181
foreach (IPNetwork2 ipnetwork in ac.Networks)
171182
{
172183
bool contain = ac.ContainNetwork.Contains(ipnetwork);
@@ -176,6 +187,11 @@ private static void ContainNetwork(ProgramContext ac)
176187

177188
private static void OverlapNetwork(ProgramContext ac)
178189
{
190+
if (ac.OverlapNetwork is null)
191+
{
192+
return;
193+
}
194+
179195
foreach (IPNetwork2 ipnetwork in ac.Networks)
180196
{
181197
bool overlap = ac.OverlapNetwork.Overlap(ipnetwork);
@@ -185,19 +201,21 @@ private static void OverlapNetwork(ProgramContext ac)
185201

186202
private static void WideSupernetNetworks(ProgramContext ac)
187203
{
188-
if (!IPNetwork2.TryWideSubnet(ac.Networks, out IPNetwork2 widesubnet))
204+
if (!IPNetwork2.TryWideSubnet(ac.Networks, out IPNetwork2? widesubnet))
189205
{
190206
Console.WriteLine("Unable to wide subnet these networks");
207+
return;
191208
}
192209

193210
PrintNetwork(ac, widesubnet);
194211
}
195212

196213
private static void SupernetNetworks(ProgramContext ac)
197214
{
198-
if (!IPNetwork2.TrySupernet(ac.Networks, out IPNetwork2[] supernet))
215+
if (!IPNetwork2.TrySupernet(ac.Networks, out IPNetwork2[]? supernet))
199216
{
200217
Console.WriteLine("Unable to supernet these networks");
218+
return;
201219
}
202220

203221
PrintNetworks(ac, supernet, supernet.Length);
@@ -210,7 +228,8 @@ private static void SubnetNetworks(ProgramContext ac)
210228
{
211229
i++;
212230
int networkLength = ac.Networks.Length;
213-
if (!ipnetwork.TrySubnet(ac.SubnetCidr, out IPNetworkCollection ipnetworks))
231+
if (!ipnetwork.TrySubnet(ac.SubnetCidr, out IPNetworkCollection? ipnetworks)
232+
|| ipnetworks is null)
214233
{
215234
Console.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", ipnetwork, ac.SubnetCidr);
216235
PrintSeparator(networkLength, i);
@@ -319,8 +338,8 @@ private static ProgramContext ParseArgs(string[] args)
319338

320339
while ((c = g.getopt()) != -1)
321340
{
322-
string optArg = g.Optarg;
323-
Args[c].Run(ac, optArg);
341+
string? optArg = g.Optarg;
342+
Args[c].Run(ac, optArg ?? string.Empty);
324343
}
325344

326345
var ipnetworks = new List<string>();
@@ -373,7 +392,7 @@ private static void ParseIPNetworks(ProgramContext ac)
373392
var ipnetworks = new List<IPNetwork2>();
374393
foreach (string ips in ac.NetworksString)
375394
{
376-
if (!TryParseIPNetwork(ips, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
395+
if (!TryParseIPNetwork(ips, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
377396
{
378397
Console.WriteLine("Unable to parse ipnetwork {0}", ips);
379398
continue;
@@ -385,9 +404,9 @@ private static void ParseIPNetworks(ProgramContext ac)
385404
ac.Networks = ipnetworks.ToArray();
386405
}
387406

388-
private static bool TryParseIPNetwork(string ip, CidrParse cidrParse, byte cidr, out IPNetwork2 ipn)
407+
private static bool TryParseIPNetwork(string ip, CidrParse cidrParse, byte cidr, [NotNullWhen(true)] out IPNetwork2? ipn)
389408
{
390-
IPNetwork2 ipnetwork = null;
409+
IPNetwork2? ipnetwork = null;
391410
switch (cidrParse)
392411
{
393412
case CidrParse.Default when !IPNetwork2.TryParse(ip, out ipnetwork):
@@ -406,6 +425,12 @@ private static bool TryParseIPNetwork(string ip, CidrParse cidrParse, byte cidr,
406425
}
407426
}
408427

428+
if (ipnetwork is null)
429+
{
430+
ipn = null;
431+
return false;
432+
}
433+
409434
ipn = ipnetwork;
410435
return true;
411436
}

src/ConsoleApplication/ProgramContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ public class ProgramContext
6767
/// <summary>
6868
/// Gets or sets a value indicating whether gets or sets the ContainNetwork.
6969
/// </summary>
70-
public IPNetwork2 ContainNetwork { get; set; }
70+
public IPNetwork2? ContainNetwork { get; set; }
7171

7272
/// <summary>
7373
/// Gets or sets a value indicating whether gets or sets the OverlapNetwork.
7474
/// </summary>
75-
public IPNetwork2 OverlapNetwork { get; set; }
75+
public IPNetwork2? OverlapNetwork { get; set; }
7676

7777
/// <summary>
7878
/// Gets or sets a value indicating whether gets or sets the SubtractNetwork.
7979
/// </summary>
80-
public IPNetwork2 SubtractNetwork { get; set; }
80+
public IPNetwork2? SubtractNetwork { get; set; }
8181

8282
/// <summary>
8383
/// Gets or sets a value indicating whether gets or sets the Action.
@@ -92,10 +92,10 @@ public class ProgramContext
9292
/// <summary>
9393
/// Gets or sets a value indicating whether gets or sets the NetworksString.
9494
/// </summary>
95-
public string[] NetworksString { get; set; }
95+
public string[] NetworksString { get; set; } = [];
9696

9797
/// <summary>
9898
/// Gets or sets a value indicating whether gets or sets the Networks.
9999
/// </summary>
100-
public IPNetwork2[] Networks { get; set; }
100+
public IPNetwork2[] Networks { get; set; } = [];
101101
}

src/TestProject/CidrClassFullIpv4UnitTest.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,81 @@ public class CidrClassFullIpv4UnitTest
1616
[TestMethod]
1717
public void TestTryGuessCidrNull()
1818
{
19-
var cidrguess = new CidrClassFull();
19+
var cidrguess = new CidrClassFull();
2020

21-
bool parsed = cidrguess.TryGuessCidr(null, out byte cidr);
21+
bool parsed = cidrguess.TryGuessCidr(null, out byte cidr);
2222

23-
Assert.IsFalse(parsed, "parsed");
24-
Assert.AreEqual(0, cidr, "cidr");
25-
}
23+
Assert.IsFalse(parsed, "parsed");
24+
Assert.AreEqual(0, cidr, "cidr");
25+
}
2626

2727
/// <summary>
2828
/// Test.
2929
/// </summary>
3030
[TestMethod]
3131
public void TestTryGuessCidrA()
3232
{
33-
var cidrguess = new CidrClassFull();
33+
var cidrguess = new CidrClassFull();
3434

35-
bool parsed = cidrguess.TryGuessCidr("10.0.0.0", out byte cidr);
35+
bool parsed = cidrguess.TryGuessCidr("10.0.0.0", out byte cidr);
3636

37-
Assert.IsTrue(parsed, "parsed");
38-
Assert.AreEqual(8, cidr, "cidr");
39-
}
37+
Assert.IsTrue(parsed, "parsed");
38+
Assert.AreEqual(8, cidr, "cidr");
39+
}
4040

4141
/// <summary>
4242
/// Test.
4343
/// </summary>
4444
[TestMethod]
4545
public void TestTryGuessCidrB()
4646
{
47-
var cidrguess = new CidrClassFull();
47+
var cidrguess = new CidrClassFull();
4848

49-
bool parsed = cidrguess.TryGuessCidr("172.0.0.0", out byte cidr);
49+
bool parsed = cidrguess.TryGuessCidr("172.0.0.0", out byte cidr);
5050

51-
Assert.IsTrue(parsed, "parsed");
52-
Assert.AreEqual(16, cidr, "cidr");
53-
}
51+
Assert.IsTrue(parsed, "parsed");
52+
Assert.AreEqual(16, cidr, "cidr");
53+
}
5454

5555
/// <summary>
5656
/// Test.
5757
/// </summary>
5858
[TestMethod]
5959
public void TestTryGuessCidrC()
6060
{
61-
var cidrguess = new CidrClassFull();
61+
var cidrguess = new CidrClassFull();
6262

63-
bool parsed = cidrguess.TryGuessCidr("192.0.0.0", out byte cidr);
63+
bool parsed = cidrguess.TryGuessCidr("192.0.0.0", out byte cidr);
6464

65-
Assert.IsTrue(parsed, "parsed");
66-
Assert.AreEqual(24, cidr, "cidr");
67-
}
65+
Assert.IsTrue(parsed, "parsed");
66+
Assert.AreEqual(24, cidr, "cidr");
67+
}
6868

6969
/// <summary>
7070
/// Test.
7171
/// </summary>
7272
[TestMethod]
7373
public void TestTryGuessCidrD()
7474
{
75-
var cidrguess = new CidrClassFull();
75+
var cidrguess = new CidrClassFull();
7676

77-
bool parsed = cidrguess.TryGuessCidr("224.0.0.0", out byte cidr);
77+
bool parsed = cidrguess.TryGuessCidr("224.0.0.0", out byte cidr);
7878

79-
Assert.IsTrue(parsed, "parsed");
80-
Assert.AreEqual(24, cidr, "cidr");
81-
}
79+
Assert.IsTrue(parsed, "parsed");
80+
Assert.AreEqual(24, cidr, "cidr");
81+
}
8282

8383
/// <summary>
8484
/// Test.
8585
/// </summary>
8686
[TestMethod]
8787
public void TestTryGuessCidrE()
8888
{
89-
var cidrguess = new CidrClassFull();
89+
var cidrguess = new CidrClassFull();
9090

91-
bool parsed = cidrguess.TryGuessCidr("240.0.0.0", out byte cidr);
91+
bool parsed = cidrguess.TryGuessCidr("240.0.0.0", out byte cidr);
9292

93-
Assert.IsTrue(parsed, "parsed");
94-
Assert.AreEqual(24, cidr, "cidr");
95-
}
93+
Assert.IsTrue(parsed, "parsed");
94+
Assert.AreEqual(24, cidr, "cidr");
95+
}
9696
}

src/TestProject/CidrClassFullIpv6UnitTest.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@ public class CidrClassFullIpv6UnitTest
1616
[TestMethod]
1717
public void TestIpV6TryGuessCidrNull()
1818
{
19-
var cidrguess = new CidrClassFull();
19+
var cidrguess = new CidrClassFull();
2020

21-
bool parsed = cidrguess.TryGuessCidr(null, out byte cidr);
21+
bool parsed = cidrguess.TryGuessCidr(null, out byte cidr);
2222

23-
Assert.IsFalse(parsed, "parsed");
24-
Assert.AreEqual(0, cidr, "cidr");
25-
}
23+
Assert.IsFalse(parsed, "parsed");
24+
Assert.AreEqual(0, cidr, "cidr");
25+
}
2626

2727
/// <summary>
2828
/// Test.
2929
/// </summary>
3030
[TestMethod]
3131
public void TestIpV6TryGuessCidr1()
3232
{
33-
var cidrguess = new CidrClassFull();
33+
var cidrguess = new CidrClassFull();
3434

35-
bool parsed = cidrguess.TryGuessCidr("::", out byte cidr);
35+
bool parsed = cidrguess.TryGuessCidr("::", out byte cidr);
3636

37-
Assert.IsTrue(parsed, "parsed");
38-
Assert.AreEqual(64, cidr, "cidr");
39-
}
37+
Assert.IsTrue(parsed, "parsed");
38+
Assert.AreEqual(64, cidr, "cidr");
39+
}
4040

4141
/// <summary>
4242
/// Test.
4343
/// </summary>
4444
[TestMethod]
4545
public void TestIpV6TryGuessCidr2()
4646
{
47-
var cidrguess = new CidrClassFull();
47+
var cidrguess = new CidrClassFull();
4848

49-
bool parsed = cidrguess.TryGuessCidr("2001:0db8::", out byte cidr);
49+
bool parsed = cidrguess.TryGuessCidr("2001:0db8::", out byte cidr);
5050

51-
Assert.IsTrue(parsed, "parsed");
52-
Assert.AreEqual(64, cidr, "cidr");
53-
}
51+
Assert.IsTrue(parsed, "parsed");
52+
Assert.AreEqual(64, cidr, "cidr");
53+
}
5454
}

0 commit comments

Comments
 (0)