Skip to content

Commit 708b8bd

Browse files
committed
chore: enable nullable annotations for part of files
1 parent cb5d392 commit 708b8bd

35 files changed

+288
-168
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using BenchmarkDotNet.Configs;
33

4+
#nullable enable
5+
46
namespace BenchmarkDotNet.Attributes
57
{
68
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly)]
79
public class ConfigAttribute : Attribute, IConfigSource
810
{
911
public IConfig Config { get; }
1012

11-
public ConfigAttribute(Type type) => Config = (IConfig)Activator.CreateInstance(type);
13+
public ConfigAttribute(Type type) => Config = (IConfig)Activator.CreateInstance(type)!;
1214
}
1315
}

src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using BenchmarkDotNet.Jobs;
66
using JetBrains.Annotations;
77

8+
#nullable enable
9+
810
namespace BenchmarkDotNet.Attributes
911
{
1012
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
@@ -18,7 +20,7 @@ public SimpleJobAttribute(
1820
int warmupCount = DefaultValue,
1921
int iterationCount = DefaultValue,
2022
int invocationCount = DefaultValue,
21-
string? id = null,
23+
string id = "",
2224
bool baseline = false
2325
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, null, baseline)) { }
2426

@@ -29,7 +31,7 @@ public SimpleJobAttribute(
2931
int warmupCount = DefaultValue,
3032
int iterationCount = DefaultValue,
3133
int invocationCount = DefaultValue,
32-
string? id = null,
34+
string id = "",
3335
bool baseline = false
3436
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, runStrategy, baseline)) { }
3537

@@ -40,7 +42,7 @@ public SimpleJobAttribute(
4042
int warmupCount = DefaultValue,
4143
int iterationCount = DefaultValue,
4244
int invocationCount = DefaultValue,
43-
string? id = null,
45+
string id = "",
4446
bool baseline = false
4547
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, null, baseline, runtimeMoniker)) { }
4648

@@ -52,11 +54,11 @@ public SimpleJobAttribute(
5254
int warmupCount = DefaultValue,
5355
int iterationCount = DefaultValue,
5456
int invocationCount = DefaultValue,
55-
string? id = null,
57+
string id = "",
5658
bool baseline = false
5759
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, runStrategy, baseline, runtimeMoniker)) { }
5860

59-
private static Job CreateJob(string? id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy,
61+
private static Job CreateJob(string id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy,
6062
bool baseline, RuntimeMoniker runtimeMoniker = RuntimeMoniker.HostProcess)
6163
{
6264
var job = new Job(id);

src/BenchmarkDotNet/Code/ArrayParam.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
using BenchmarkDotNet.Helpers;
66
using JetBrains.Annotations;
77

8+
#nullable enable
9+
810
namespace BenchmarkDotNet.Code
911
{
1012
internal static class ArrayParam
1113
{
1214
private static (string BaseElementTypeRepr, string InnerDimensions) GetDisplayString(Type arrayType)
1315
{
14-
var elemType = arrayType.GetElementType();
16+
var elemType = arrayType.GetElementType()!;
1517

1618
if (elemType.IsArray)
1719
{

src/BenchmarkDotNet/Code/CodeGenerator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using BenchmarkDotNet.Running;
1818
using RunMode = BenchmarkDotNet.Jobs.RunMode;
1919

20+
#nullable enable
21+
2022
namespace BenchmarkDotNet.Code
2123
{
2224
internal static class CodeGenerator
@@ -80,7 +82,7 @@ private static void AddNonEmptyUnique(HashSet<string> items, string value)
8082

8183
private static (bool, string) GetShadowCopySettings()
8284
{
83-
string benchmarkDotNetLocation = Path.GetDirectoryName(typeof(CodeGenerator).GetTypeInfo().Assembly.Location);
85+
string benchmarkDotNetLocation = Path.GetDirectoryName(typeof(CodeGenerator).GetTypeInfo().Assembly.Location)!;
8486

8587
if (benchmarkDotNetLocation != null && benchmarkDotNetLocation.IndexOf("LINQPAD", StringComparison.OrdinalIgnoreCase) >= 0)
8688
{
@@ -164,13 +166,13 @@ private static string GetDeclareArgumentFields(BenchmarkCase benchmarkCase)
164166
=> string.Join(
165167
Environment.NewLine,
166168
benchmarkCase.Descriptor.WorkloadMethod.GetParameters()
167-
.Select((parameter, index) => $"private {GetFieldType(parameter.ParameterType, benchmarkCase.Parameters.GetArgument(parameter.Name)).GetCorrectCSharpTypeName()} __argField{index};"));
169+
.Select((parameter, index) => $"private {GetFieldType(parameter.ParameterType, benchmarkCase.Parameters.GetArgument(parameter.Name!)).GetCorrectCSharpTypeName()} __argField{index};"));
168170

169171
private static string GetInitializeArgumentFields(BenchmarkCase benchmarkCase)
170172
=> string.Join(
171173
Environment.NewLine,
172174
benchmarkCase.Descriptor.WorkloadMethod.GetParameters()
173-
.Select((parameter, index) => $"this.__argField{index} = {benchmarkCase.Parameters.GetArgument(parameter.Name).ToSourceCode()};")); // we init the fields in ctor to provoke all possible allocations and overhead of other type
175+
.Select((parameter, index) => $"this.__argField{index} = {benchmarkCase.Parameters.GetArgument(parameter.Name!).ToSourceCode()};")); // we init the fields in ctor to provoke all possible allocations and overhead of other type
174176

175177
private static string GetLoadArguments(BenchmarkCase benchmarkCase)
176178
=> string.Join(
@@ -200,7 +202,7 @@ private static string GetExtraAttributes(BuildPartition buildPartition)
200202

201203
private static string GetEngineFactoryTypeName(BenchmarkCase benchmarkCase)
202204
{
203-
var factory = benchmarkCase.Job.ResolveValue(InfrastructureMode.EngineFactoryCharacteristic, InfrastructureResolver.Instance);
205+
var factory = benchmarkCase.Job.ResolveValue(InfrastructureMode.EngineFactoryCharacteristic, InfrastructureResolver.Instance)!;
204206
var factoryType = factory.GetType();
205207

206208
if (!factoryType.GetTypeInfo().DeclaredConstructors.Any(ctor => ctor.IsPublic && !ctor.GetParameters().Any()))

src/BenchmarkDotNet/Code/DeclarationsProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using BenchmarkDotNet.Extensions;
44
using BenchmarkDotNet.Running;
55

6+
#nullable enable
7+
68
namespace BenchmarkDotNet.Code
79
{
810
internal abstract class DeclarationsProvider
@@ -29,9 +31,9 @@ internal abstract class DeclarationsProvider
2931
public abstract string GetWorkloadMethodCall(string passArguments);
3032

3133
protected static string GetMethodPrefix(MethodInfo method)
32-
=> method.IsStatic ? method.DeclaringType.GetCorrectCSharpTypeName() : "base";
34+
=> method.IsStatic ? method.DeclaringType!.GetCorrectCSharpTypeName() : "base";
3335

34-
private string GetMethodName(MethodInfo method)
36+
private string GetMethodName(MethodInfo? method)
3537
{
3638
if (method == null)
3739
{

src/BenchmarkDotNet/Columns/BaselineAllocationRatioColumn.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using BenchmarkDotNet.Diagnosers;
5+
using BenchmarkDotNet.Extensions;
56
using BenchmarkDotNet.Mathematics;
67
using BenchmarkDotNet.Reports;
78
using BenchmarkDotNet.Running;
89

10+
#nullable enable
11+
912
namespace BenchmarkDotNet.Columns
1013
{
1114
public class BaselineAllocationRatioColumn : BaselineCustomColumn
@@ -28,7 +31,7 @@ public override string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
2831
return "NA";
2932

3033
var cultureInfo = summary.GetCultureInfo();
31-
var ratioStyle = summary?.Style?.RatioStyle ?? RatioStyle.Value;
34+
var ratioStyle = summary.Style.RatioStyle;
3235

3336
bool advancedPrecision = IsNonBaselinesPrecise(summary, baselineMetrics, benchmarkCase);
3437
switch (ratioStyle)
@@ -56,14 +59,15 @@ public override string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
5659

5760
private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<string, Metric> baselineMetric, BenchmarkCase benchmarkCase)
5861
{
59-
string logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
62+
var logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
6063
var nonBaselines = summary.GetNonBaselines(logicalGroupKey);
61-
return nonBaselines.Any(c => GetAllocationRatio(summary[c].Metrics, baselineMetric) is > 0 and < 0.01);
64+
65+
return nonBaselines.Any(c => GetAllocationRatio(summary[c]!.Metrics, baselineMetric) is > 0 and < 0.01);
6266
}
6367

6468
private static double? GetAllocationRatio(
65-
IReadOnlyDictionary<string, Metric>? current,
66-
IReadOnlyDictionary<string, Metric>? baseline)
69+
IReadOnlyDictionary<string, Metric> current,
70+
IReadOnlyDictionary<string, Metric> baseline)
6771
{
6872
double? currentBytes = GetAllocatedBytes(current);
6973
double? baselineBytes = GetAllocatedBytes(baseline);
@@ -77,9 +81,9 @@ private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<s
7781
return currentBytes / baselineBytes;
7882
}
7983

80-
private static double? GetAllocatedBytes(IReadOnlyDictionary<string, Metric>? metrics)
84+
private static double? GetAllocatedBytes(IReadOnlyDictionary<string, Metric> metrics)
8185
{
82-
var metric = metrics?.Values.FirstOrDefault(m => m.Descriptor is AllocatedMemoryMetricDescriptor);
86+
var metric = metrics.Values.FirstOrDefault(m => m.Descriptor is AllocatedMemoryMetricDescriptor);
8387
return metric?.Value;
8488
}
8589

src/BenchmarkDotNet/Columns/BaselineCustomColumn.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
23
using BenchmarkDotNet.Mathematics;
34
using BenchmarkDotNet.Reports;
45
using BenchmarkDotNet.Running;
56
using JetBrains.Annotations;
67

8+
#nullable enable
9+
710
namespace BenchmarkDotNet.Columns
811
{
912
public abstract class BaselineCustomColumn : IColumn
@@ -13,17 +16,20 @@ public abstract class BaselineCustomColumn : IColumn
1316

1417
public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
1518
{
16-
string logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
19+
var logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
1720
var baseline = summary.GetBaseline(logicalGroupKey);
1821
bool isBaseline = summary.IsBaseline(benchmarkCase);
1922

2023
if (ResultsAreInvalid(summary, benchmarkCase, baseline))
2124
return MetricColumn.UnknownRepresentation;
2225

23-
var baselineStat = summary[baseline].ResultStatistics;
24-
var baselineMetrics = summary[baseline].Metrics;
25-
var currentStat = summary[benchmarkCase].ResultStatistics;
26-
var currentMetrics = summary[benchmarkCase].Metrics;
26+
var baselineReport = summary[baseline]!;
27+
var baselineStat = baselineReport.ResultStatistics!;
28+
var baselineMetrics = baselineReport.Metrics;
29+
30+
var benchmarkCaseReport = summary[benchmarkCase]!;
31+
var currentStat = benchmarkCaseReport.ResultStatistics!;
32+
var currentMetrics = benchmarkCaseReport.Metrics;
2733

2834
return GetValue(summary, benchmarkCase, baselineStat, baselineMetrics, currentStat, currentMetrics, isBaseline);
2935
}
@@ -43,14 +49,12 @@ public abstract string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
4349
public override string ToString() => ColumnName;
4450
public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) => false;
4551

46-
internal static bool ResultsAreInvalid(Summary summary, BenchmarkCase benchmarkCase, BenchmarkCase? baseline)
52+
internal static bool ResultsAreInvalid(Summary summary, BenchmarkCase benchmarkCase, [NotNullWhen(false)] BenchmarkCase? baseline)
4753
{
4854
return baseline == null ||
49-
summary[baseline] == null ||
50-
summary[baseline].ResultStatistics == null ||
51-
!summary[baseline].ResultStatistics.CanBeInverted() ||
52-
summary[benchmarkCase] == null ||
53-
summary[benchmarkCase].ResultStatistics == null;
55+
summary[baseline]?.ResultStatistics == null ||
56+
!summary[baseline]!.ResultStatistics!.CanBeInverted() ||
57+
summary[benchmarkCase]?.ResultStatistics == null;
5458
}
5559
}
5660
}

src/BenchmarkDotNet/Columns/BaselineRatioColumn.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using BenchmarkDotNet.Reports;
66
using BenchmarkDotNet.Running;
77

8+
#nullable enable
9+
810
namespace BenchmarkDotNet.Columns
911
{
1012
public class BaselineRatioColumn : BaselineCustomColumn
@@ -52,7 +54,7 @@ public override string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
5254
var invertedRatio = GetRatioStatistics(baseline, current);
5355

5456
var cultureInfo = summary.GetCultureInfo();
55-
var ratioStyle = summary?.Style?.RatioStyle ?? RatioStyle.Value;
57+
var ratioStyle = summary.Style.RatioStyle;
5658

5759
switch (Metric)
5860
{
@@ -112,9 +114,9 @@ public override string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
112114

113115
private static bool IsNonBaselinesPrecise(Summary summary, Statistics baselineStat, BenchmarkCase benchmarkCase)
114116
{
115-
string logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
117+
var logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
116118
var nonBaselines = summary.GetNonBaselines(logicalGroupKey);
117-
return nonBaselines.Any(x => GetRatioStatistics(summary[x].ResultStatistics, baselineStat)?.Mean < 0.01);
119+
return nonBaselines.Any(x => GetRatioStatistics(summary[x]?.ResultStatistics, baselineStat)?.Mean < 0.01);
118120
}
119121

120122
private static Statistics? GetRatioStatistics(Statistics? current, Statistics? baseline)

src/BenchmarkDotNet/Columns/MetricColumn.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Perfolizer.Metrology;
77
using Pragmastat.Metrology;
88

9+
#nullable enable
10+
911
namespace BenchmarkDotNet.Columns
1012
{
1113
public class MetricColumn : IColumn
@@ -35,7 +37,7 @@ public bool IsAvailable(Summary summary) => summary.Reports.Any(report =>
3537

3638
public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style)
3739
{
38-
if (!summary.HasReport(benchmarkCase) || !summary[benchmarkCase].Metrics.TryGetValue(descriptor.Id, out Metric metric))
40+
if (!summary.HasReport(benchmarkCase) || !summary[benchmarkCase]!.Metrics.TryGetValue(descriptor.Id, out var metric))
3941
return "NA";
4042
if (double.IsNaN(metric.Value))
4143
return UnknownRepresentation;

src/BenchmarkDotNet/Columns/RankColumn.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Linq;
3+
using BenchmarkDotNet.Extensions;
34
using BenchmarkDotNet.Mathematics;
45
using BenchmarkDotNet.Reports;
56
using BenchmarkDotNet.Running;
67
using JetBrains.Annotations;
78

9+
#nullable enable
10+
811
namespace BenchmarkDotNet.Columns
912
{
1013
public class RankColumn : IColumn
@@ -24,13 +27,14 @@ public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
2427
{
2528
var logicalGroup = summary
2629
.GetLogicalGroupForBenchmark(benchmarkCase)
27-
.Where(b => summary[b].ResultStatistics != null)
30+
.Where(b => summary[b]?.ResultStatistics != null)
31+
.WhereNotNull()
2832
.ToArray();
2933
int index = Array.IndexOf(logicalGroup, benchmarkCase);
3034
if (index == -1)
3135
return MetricColumn.UnknownRepresentation;
3236

33-
var ranks = RankHelper.GetRanks(logicalGroup.Select(b => summary[b].ResultStatistics).ToArray());
37+
var ranks = RankHelper.GetRanks(logicalGroup.Select(b => summary[b]!.ResultStatistics!).ToArray());
3438
int rank = ranks[index];
3539
return numeralSystem.ToPresentation(rank);
3640
}
@@ -42,7 +46,7 @@ public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
4246
public bool IsNumeric => true;
4347
public UnitType UnitType => UnitType.Dimensionless;
4448
public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style) => GetValue(summary, benchmarkCase);
45-
public int PriorityInCategory => (int) numeralSystem;
49+
public int PriorityInCategory => (int)numeralSystem;
4650
public override string ToString() => ColumnName;
4751
public string Legend => $"Relative position of current benchmark mean among all benchmarks ({numeralSystem} style)";
4852
}

0 commit comments

Comments
 (0)