Skip to content

Commit b4ac9df

Browse files
Nullability cleanup (2023-11-26)
1 parent a572db1 commit b4ac9df

23 files changed

+94
-70
lines changed

src/BenchmarkDotNet/Analysers/BaselineCustomAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override IEnumerable<Conclusion> AnalyseSummary(Summary summary)
2525

2626
foreach (var benchmarkCase in summary.BenchmarksCases)
2727
{
28-
string logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
28+
string? logicalGroupKey = summary.GetLogicalGroupKey(benchmarkCase);
2929
var baseline = summary.GetBaseline(logicalGroupKey);
3030
if (BaselineCustomColumn.ResultsAreInvalid(summary, benchmarkCase, baseline) == false)
3131
continue;

src/BenchmarkDotNet/Analysers/OutliersAnalyser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ string Format(int n, string verb)
5353
return $"{n} {words} {verb}";
5454
}
5555

56-
var rangeMessages = new List<string> { GetRangeMessage(lowerOutliers, cultureInfo), GetRangeMessage(upperOutliers, cultureInfo) };
56+
var rangeMessages = new List<string?> { GetRangeMessage(lowerOutliers, cultureInfo), GetRangeMessage(upperOutliers, cultureInfo) };
5757
rangeMessages.RemoveAll(string.IsNullOrEmpty);
5858
string rangeMessage = rangeMessages.Any()
5959
? " (" + string.Join(", ", rangeMessages) + ")"

src/BenchmarkDotNet/Columns/BaselineCustomColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract string GetValue(Summary summary, BenchmarkCase benchmarkCase, St
4343
public override string ToString() => ColumnName;
4444
public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) => false;
4545

46-
internal static bool ResultsAreInvalid(Summary summary, BenchmarkCase benchmarkCase, BenchmarkCase baseline)
46+
internal static bool ResultsAreInvalid(Summary summary, BenchmarkCase benchmarkCase, BenchmarkCase? baseline)
4747
{
4848
return baseline == null ||
4949
summary[baseline] == null ||

src/BenchmarkDotNet/Columns/TagColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public TagColumn(string columnName, Func<string, string> getTag)
1919
}
2020

2121
public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) => false;
22-
public string GetValue(Summary summary, BenchmarkCase benchmarkCase) => getTag(benchmarkCase.Descriptor.WorkloadMethod.Name);
22+
public string GetValue(Summary summary, BenchmarkCase benchmarkCase) => getTag(benchmarkCase.Descriptor?.WorkloadMethod?.Name ?? "");
2323

2424
public bool IsAvailable(Summary summary) => true;
2525
public bool AlwaysShow => true;

src/BenchmarkDotNet/Configs/ImmutableConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ internal ImmutableConfig(
119119

120120
public bool HasExtraStatsDiagnoser() => HasMemoryDiagnoser() || HasThreadingDiagnoser() || HasExceptionDiagnoser();
121121

122-
public IDiagnoser GetCompositeDiagnoser(BenchmarkCase benchmarkCase, RunMode runMode)
122+
public IDiagnoser? GetCompositeDiagnoser(BenchmarkCase benchmarkCase, RunMode runMode)
123123
{
124124
var diagnosersForGivenMode = diagnosers.Where(diagnoser => diagnoser.GetRunMode(benchmarkCase) == runMode).ToImmutableHashSet();
125125

src/BenchmarkDotNet/Diagnosers/DiagnoserActionParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace BenchmarkDotNet.Diagnosers
66
{
77
public class DiagnoserActionParameters
88
{
9-
public DiagnoserActionParameters(Process process, BenchmarkCase benchmarkCase, BenchmarkId benchmarkId)
9+
public DiagnoserActionParameters(Process? process, BenchmarkCase benchmarkCase, BenchmarkId benchmarkId)
1010
{
1111
Process = process;
1212
BenchmarkCase = benchmarkCase;

src/BenchmarkDotNet/Exporters/Json/SimpleJson.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable disable
12
// ReSharper disable All
23
//-----------------------------------------------------------------------
34
// <copyright file="SimpleJson.cs" company="The Outercurve Foundation">

src/BenchmarkDotNet/Extensions/CommonExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ public static string GetColumnTitle(this IColumn column, SummaryStyle style)
3232
}
3333
}
3434

35-
public static bool IsNullOrEmpty<T>(this IReadOnlyCollection<T> value) => value == null || value.Count == 0;
35+
public static bool IsNullOrEmpty<T>(this IReadOnlyCollection<T>? value) => value == null || value.Count == 0;
3636
public static bool IsEmpty<T>(this IReadOnlyCollection<T> value) => value.Count == 0;
3737
public static bool IsEmpty<T>(this IEnumerable<T> value) => !value.Any();
3838

39+
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> values) => values.Where(value => value != null).Cast<T>();
40+
3941
public static void AddRange<T>(this HashSet<T> hashSet, IEnumerable<T> collection)
4042
{
4143
foreach (var item in collection)
4244
hashSet.Add(item);
4345
}
4446

4547
#if NETSTANDARD2_0
46-
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
48+
public static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
4749
=> dictionary.TryGetValue(key, out var value) ? value : default;
4850
#endif
4951

@@ -97,15 +99,17 @@ internal static string DeleteFileIfExists(this string filePath)
9799

98100
internal static string EnsureFolderExists(this string filePath)
99101
{
100-
string directoryPath = Path.GetDirectoryName(filePath);
102+
string? directoryPath = Path.GetDirectoryName(filePath);
103+
if (directoryPath == null)
104+
throw new ArgumentException($"Can't get directory path from '{filePath}'");
101105

102106
if (!Directory.Exists(directoryPath))
103107
Directory.CreateDirectory(directoryPath);
104108

105109
return filePath;
106110
}
107111

108-
internal static bool IsNotNullButDoesNotExist(this FileSystemInfo fileInfo)
112+
internal static bool IsNotNullButDoesNotExist(this FileSystemInfo? fileInfo)
109113
=> fileInfo != null && !fileInfo.Exists;
110114
}
111115
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using JetBrains.Annotations;
3+
4+
namespace BenchmarkDotNet.Helpers;
5+
6+
internal static class Assertion
7+
{
8+
[AssertionMethod]
9+
public static void NotNull(string name, object? value)
10+
{
11+
if (value == null)
12+
throw new ArgumentNullException(name, $"{name} can't be null");
13+
}
14+
}

src/BenchmarkDotNet/Helpers/DirtyAssemblyResolveHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class DirtyAssemblyResolveHelper : IDisposable
2828
/// "the handler is invoked whenever the runtime fails to bind to an assembly by name."
2929
/// </summary>
3030
/// <returns>not null when we find it manually, null when can't help</returns>
31-
private Assembly HelpTheFrameworkToResolveTheAssembly(object sender, ResolveEventArgs args)
31+
private Assembly? HelpTheFrameworkToResolveTheAssembly(object sender, ResolveEventArgs args)
3232
{
3333
var fullName = new AssemblyName(args.Name);
3434
string simpleName = fullName.Name;

0 commit comments

Comments
 (0)