Skip to content

Commit e930ee3

Browse files
committed
chore: enable nullable
1 parent a2871df commit e930ee3

File tree

103 files changed

+659
-391
lines changed

Some content is hidden

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

103 files changed

+659
-391
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
#nullable enable
4+
35
namespace BenchmarkDotNet.Attributes
46
{
57
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
@@ -8,9 +10,9 @@ public class ParamsAttribute : PriorityAttribute
810
public object?[] Values { get; protected set; }
911

1012
// CLS-Compliant Code requires a constructor without an array in the argument list
11-
public ParamsAttribute() => Values = new object[0];
13+
public ParamsAttribute() => Values = [];
1214

1315
public ParamsAttribute(params object?[]? values)
14-
=> Values = values ?? new object?[] { null }; // when users do Params(null) they mean one, null argument
16+
=> Values = values ?? [null]; // when users do Params(null) they mean one, null argument
1517
}
1618
}

src/BenchmarkDotNet.TestAdapter/VSTestEventProcessor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using System.Text;
1616
using System.Threading;
1717

18+
#nullable enable
19+
1820
namespace BenchmarkDotNet.TestAdapter
1921
{
2022
/// <summary>
@@ -81,7 +83,7 @@ public override void OnBuildComplete(BuildPartition buildPartition, BuildResult
8183

8284
if (buildResult.GenerateException != null)
8385
testResult.ErrorMessage = $"// Generate Exception: {buildResult.GenerateException.Message}";
84-
else if (!buildResult.IsBuildSuccess && buildResult.TryToExplainFailureReason(out string reason))
86+
else if (!buildResult.IsBuildSuccess && buildResult.TryToExplainFailureReason(out string? reason))
8587
testResult.ErrorMessage = $"// Build Error: {reason}";
8688
else if (buildResult.ErrorMessage != null)
8789
testResult.ErrorMessage = $"// Build Error: {buildResult.ErrorMessage}";

src/BenchmarkDotNet/Columns/LogicalGroupColumn.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using BenchmarkDotNet.Running;
33
using JetBrains.Annotations;
44

5+
#nullable enable
6+
57
namespace BenchmarkDotNet.Columns
68
{
79
public class LogicalGroupColumn : IColumn
@@ -11,7 +13,7 @@ public class LogicalGroupColumn : IColumn
1113
public string Id => nameof(LogicalGroupColumn);
1214
public string ColumnName => Column.LogicalGroup;
1315

14-
public string GetValue(Summary summary, BenchmarkCase benchmarkCase) => summary.GetLogicalGroupKey(benchmarkCase);
16+
public string GetValue(Summary summary, BenchmarkCase benchmarkCase) => summary.GetLogicalGroupKey(benchmarkCase) ?? "";
1517
public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style) => GetValue(summary, benchmarkCase);
1618
public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) => false;
1719
public bool IsAvailable(Summary summary) => true;

src/BenchmarkDotNet/Detectors/Cpu/HardwareIntrinsics.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Runtime.Intrinsics.Arm;
1010
#endif
1111

12+
#nullable enable
13+
1214
namespace BenchmarkDotNet.Detectors.Cpu
1315
{
1416
// based on https://github.com/dotnet/runtime/tree/v10.0.0-rc.1.25451.107/src/coreclr/tools/Common/JitInterface/ThunkGenerator/InstructionSetDesc.txt
@@ -281,10 +283,10 @@ static IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
281283

282284
private static bool GetIsSupported([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] string typeName)
283285
{
284-
Type type = Type.GetType(typeName);
286+
Type type = Type.GetType(typeName)!;
285287
if (type == null) return false;
286288

287-
return (bool)type.GetProperty("IsSupported", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).GetValue(null, null);
289+
return (bool)type.GetProperty("IsSupported", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)!.GetValue(null, null)!;
288290
}
289291
}
290292
}

src/BenchmarkDotNet/Detectors/Cpu/Linux/LinuxCpuInfoParser.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace BenchmarkDotNet.Detectors.Cpu.Linux;
1212

13+
#nullable enable
14+
1315
internal static class LinuxCpuInfoParser
1416
{
1517
private static class ProcCpu
@@ -42,27 +44,27 @@ internal static CpuInfo Parse(string cpuInfo, string lscpu)
4244
var logicalCores = SectionsHelper.ParseSections(cpuInfo, ':');
4345
foreach (var logicalCore in logicalCores)
4446
{
45-
if (logicalCore.TryGetValue(ProcCpu.PhysicalId, out string physicalId) &&
46-
logicalCore.TryGetValue(ProcCpu.CpuCores, out string cpuCoresValue) &&
47+
if (logicalCore.TryGetValue(ProcCpu.PhysicalId, out var physicalId) &&
48+
logicalCore.TryGetValue(ProcCpu.CpuCores, out var cpuCoresValue) &&
4749
int.TryParse(cpuCoresValue, out int cpuCoreCount) &&
4850
cpuCoreCount > 0)
4951
processorsToPhysicalCoreCount[physicalId] = cpuCoreCount;
5052

51-
if (logicalCore.TryGetValue(ProcCpu.ModelName, out string modelName))
53+
if (logicalCore.TryGetValue(ProcCpu.ModelName, out var modelName))
5254
{
5355
processorModelNames.Add(modelName);
5456
logicalCoreCount++;
5557
}
5658

57-
if (logicalCore.TryGetValue(ProcCpu.MaxFrequency, out string maxCpuFreqValue) &&
59+
if (logicalCore.TryGetValue(ProcCpu.MaxFrequency, out var maxCpuFreqValue) &&
5860
Frequency.TryParseMHz(maxCpuFreqValue.Replace(',', '.'), out Frequency maxCpuFreq)
5961
&& maxCpuFreq > 0)
6062
{
6163
maxFrequency = Math.Max(maxFrequency, maxCpuFreq.ToMHz());
6264
}
6365

64-
bool nominalFrequencyHasValue = logicalCore.TryGetValue(ProcCpu.NominalFrequency, out string nominalFreqValue);
65-
bool nominalFrequencyBackupHasValue = logicalCore.TryGetValue(ProcCpu.NominalFrequencyBackup, out string nominalFreqBackupValue);
66+
bool nominalFrequencyHasValue = logicalCore.TryGetValue(ProcCpu.NominalFrequency, out var nominalFreqValue);
67+
bool nominalFrequencyBackupHasValue = logicalCore.TryGetValue(ProcCpu.NominalFrequencyBackup, out var nominalFreqBackupValue);
6668

6769
double nominalCpuFreq = 0.0;
6870
double nominalCpuBackupFreq = 0.0;
@@ -115,7 +117,7 @@ internal static CpuInfo Parse(string cpuInfo, string lscpu)
115117
}
116118
}
117119

118-
string processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
120+
string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
119121
int? physicalProcessorCount = processorsToPhysicalCoreCount.Count > 0 ? processorsToPhysicalCoreCount.Count : null;
120122
int? physicalCoreCount = processorsToPhysicalCoreCount.Count > 0 ? processorsToPhysicalCoreCount.Values.Sum() : coresPerSocket;
121123

src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Perfolizer.Horology;
99
using Perfolizer.Models;
1010

11+
#nullable enable
12+
1113
namespace BenchmarkDotNet.Detectors.Cpu.Windows;
1214

1315
internal class MosCpuDetector : ICpuDetector
@@ -33,7 +35,7 @@ public bool IsApplicable() => OsDetector.IsWindows() &&
3335
{
3436
foreach (var moProcessor in mosProcessor.Get().Cast<ManagementObject>())
3537
{
36-
string name = moProcessor[WmicCpuInfoKeyNames.Name]?.ToString();
38+
string? name = moProcessor[WmicCpuInfoKeyNames.Name]?.ToString();
3739
if (name.IsNotBlank())
3840
{
3941
processorModelNames.Add(name);
@@ -51,7 +53,7 @@ public bool IsApplicable() => OsDetector.IsWindows() &&
5153
}
5254
}
5355

54-
string processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
56+
string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
5557
Frequency? maxFrequencyActual = maxFrequency > 0 && processorsCount > 0
5658
? Frequency.FromMHz(maxFrequency)
5759
: null;

src/BenchmarkDotNet/Detectors/Cpu/Windows/PowershellWmiCpuInfoParser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace BenchmarkDotNet.Detectors.Cpu.Windows;
99

10+
#nullable enable
11+
1012
internal static class PowershellWmiCpuInfoParser
1113
{
1214
internal static CpuInfo Parse(string powershellWmiOutput)
@@ -22,23 +24,23 @@ internal static CpuInfo Parse(string powershellWmiOutput)
2224
List<Dictionary<string, string>> processors = SectionsHelper.ParseSectionsForPowershellWmi(powershellWmiOutput, ':');
2325
foreach (Dictionary<string, string> processor in processors)
2426
{
25-
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfCores, out string numberOfCoresValue) &&
27+
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfCores, out string? numberOfCoresValue) &&
2628
int.TryParse(numberOfCoresValue, out int numberOfCores) &&
2729
numberOfCores > 0)
2830
physicalCoreCount += numberOfCores;
2931

30-
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfLogicalProcessors, out string numberOfLogicalValue) &&
32+
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfLogicalProcessors, out string? numberOfLogicalValue) &&
3133
int.TryParse(numberOfLogicalValue, out int numberOfLogical) &&
3234
numberOfLogical > 0)
3335
logicalCoreCount += numberOfLogical;
3436

35-
if (processor.TryGetValue(WmicCpuInfoKeyNames.Name, out string name))
37+
if (processor.TryGetValue(WmicCpuInfoKeyNames.Name, out string? name))
3638
{
3739
processorModelNames.Add(name);
3840
processorCount++;
3941
}
4042

41-
if (processor.TryGetValue(WmicCpuInfoKeyNames.MaxClockSpeed, out string frequencyValue)
43+
if (processor.TryGetValue(WmicCpuInfoKeyNames.MaxClockSpeed, out string? frequencyValue)
4244
&& double.TryParse(frequencyValue, out double frequency)
4345
&& frequency > 0)
4446
{

src/BenchmarkDotNet/Detectors/OsDetector.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
1111
using System.Runtime.Versioning;
1212

13+
#nullable enable
14+
1315
namespace BenchmarkDotNet.Detectors;
1416

1517
public class OsDetector
@@ -41,7 +43,7 @@ private static OsInfo ResolveOs()
4143
{
4244
try
4345
{
44-
string version = LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
46+
string? version = LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
4547
bool wsl = IsUnderWsl();
4648
return new OsInfo
4749
{

src/BenchmarkDotNet/Diagnosers/DiagnosersLoader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using BenchmarkDotNet.Loggers;
1010
using BenchmarkDotNet.Portability;
1111

12+
#nullable enable
13+
1214
namespace BenchmarkDotNet.Diagnosers
1315
{
1416
internal static class DiagnosersLoader
@@ -95,6 +97,6 @@ private static IDiagnoser[] LoadWindowsDiagnosers()
9597
}
9698

9799
private static IDiagnoser CreateDiagnoser(Assembly loadedAssembly, string typeName)
98-
=> (IDiagnoser)Activator.CreateInstance(loadedAssembly.GetType(typeName));
100+
=> (IDiagnoser)Activator.CreateInstance(loadedAssembly.GetType(typeName)!)!;
99101
}
100102
}

src/BenchmarkDotNet/Diagnosers/EventPipeProfiler.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using BenchmarkDotNet.Validators;
1919
using Microsoft.Diagnostics.NETCore.Client;
2020

21+
#nullable enable
22+
2123
namespace BenchmarkDotNet.Diagnosers
2224
{
2325
public class EventPipeProfiler : IProfiler
@@ -28,11 +30,11 @@ public class EventPipeProfiler : IProfiler
2830
private readonly ImmutableHashSet<EventPipeProvider> eventPipeProviders;
2931
private readonly bool performExtraBenchmarksRun;
3032

31-
private Task collectingTask;
33+
private Task collectingTask = default!;
3234

3335
// parameterless constructor required by DiagnosersLoader to support creating this profiler via console line args
3436
// we use performExtraBenchmarksRun = false for better first user experience
35-
public EventPipeProfiler() :this(profile: EventPipeProfile.CpuSampling, performExtraBenchmarksRun: false) { }
37+
public EventPipeProfiler() : this(profile: EventPipeProfile.CpuSampling, performExtraBenchmarksRun: false) { }
3638

3739
/// <summary>
3840
/// Creates a new instance of EventPipeProfiler
@@ -60,7 +62,7 @@ public IEnumerable<ValidationError> Validate(ValidationParameters validationPara
6062
{
6163
foreach (var benchmark in validationParameters.Benchmarks)
6264
{
63-
var runtime = benchmark.Job.ResolveValue(EnvironmentMode.RuntimeCharacteristic, EnvironmentResolver.Instance);
65+
var runtime = benchmark.Job.ResolveValue(EnvironmentMode.RuntimeCharacteristic, EnvironmentResolver.Instance)!;
6466

6567
if (runtime.RuntimeMoniker < RuntimeMoniker.NetCoreApp30)
6668
{
@@ -127,7 +129,7 @@ public void DisplayResults(ILogger resultLogger)
127129
resultLogger.WriteLineInfo(benchmarkToTraceFile.Values.First());
128130
}
129131

130-
internal static ImmutableHashSet<EventPipeProvider> MapToProviders(EventPipeProfile profile, IReadOnlyCollection<EventPipeProvider> providers)
132+
internal static ImmutableHashSet<EventPipeProvider> MapToProviders(EventPipeProfile profile, IReadOnlyCollection<EventPipeProvider>? providers)
131133
{
132134
var uniqueProviders = ImmutableHashSet.CreateBuilder<EventPipeProvider>(EventPipeProviderEqualityComparer.Instance);
133135

@@ -154,7 +156,16 @@ private sealed class EventPipeProviderEqualityComparer : IEqualityComparer<Event
154156
{
155157
internal static readonly IEqualityComparer<EventPipeProvider> Instance = new EventPipeProviderEqualityComparer();
156158

157-
public bool Equals(EventPipeProvider x, EventPipeProvider y) => x.Name.Equals(y.Name);
159+
public bool Equals(EventPipeProvider? x, EventPipeProvider? y)
160+
{
161+
if (ReferenceEquals(x, y))
162+
return true;
163+
164+
if (x is null || y is null)
165+
return false;
166+
167+
return x.Name.Equals(y.Name);
168+
}
158169

159170
public int GetHashCode(EventPipeProvider obj) => obj.Name.GetHashCode();
160171
}

0 commit comments

Comments
 (0)