Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullability In BenchmarkDotNet project #2419

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Analysers/Conclusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Conclusion CreateWarning(string analyserId, string message, Benchm
public static Conclusion CreateError(string analyserId, string message, BenchmarkReport? report = null, bool mergeable = true)
=> new Conclusion(analyserId, ConclusionKind.Error, message, report, mergeable);

public bool Equals(Conclusion other)
public bool Equals(Conclusion? other)
{
if (ReferenceEquals(null, other))
return false;
Expand All @@ -49,7 +49,7 @@ public bool Equals(Conclusion other)
return string.Equals(AnalyserId, other.AnalyserId) && Kind == other.Kind && string.Equals(Message, other.Message);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static bool CheckZeroMeasurementOneSample(double[] results, double thresh
/// Checks distribution against Zero Measurement hypothesis in case of two samples
/// </summary>
/// <returns>True if measurement is ZeroMeasurement</returns>
public static bool CheckZeroMeasurementTwoSamples(double[] workload, double[] overhead, Threshold threshold = null)
public static bool CheckZeroMeasurementTwoSamples(double[] workload, double[] overhead, Threshold? threshold = null)
{
if (workload.Length < 3 || overhead.Length < 3)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BenchmarkDotNet.Attributes.Filters
{
public class AotFilterAttribute : FilterConfigBaseAttribute
{
public AotFilterAttribute(string reason = null)
public AotFilterAttribute(string? reason = null)
: base(new SimpleFilter(benchmark => !benchmark.GetRuntime().IsAOT))
{
}
Expand Down
10 changes: 5 additions & 5 deletions src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SimpleJobAttribute : JobConfigBaseAttribute
int warmupCount = DefaultValue,
int iterationCount = DefaultValue,
int invocationCount = DefaultValue,
string id = null,
string? id = null,
bool baseline = false
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, null, baseline)) { }

Expand All @@ -29,7 +29,7 @@ public class SimpleJobAttribute : JobConfigBaseAttribute
int warmupCount = DefaultValue,
int iterationCount = DefaultValue,
int invocationCount = DefaultValue,
string id = null,
string? id = null,
bool baseline = false
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, runStrategy, baseline)) { }

Expand All @@ -40,7 +40,7 @@ public class SimpleJobAttribute : JobConfigBaseAttribute
int warmupCount = DefaultValue,
int iterationCount = DefaultValue,
int invocationCount = DefaultValue,
string id = null,
string? id = null,
bool baseline = false
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, null, baseline, runtimeMoniker)) { }

Expand All @@ -52,11 +52,11 @@ public class SimpleJobAttribute : JobConfigBaseAttribute
int warmupCount = DefaultValue,
int iterationCount = DefaultValue,
int invocationCount = DefaultValue,
string id = null,
string? id = null,
bool baseline = false
) : base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, runStrategy, baseline, runtimeMoniker)) { }

private static Job CreateJob(string id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy,
private static Job CreateJob(string? id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy,
bool baseline, RuntimeMoniker runtimeMoniker = RuntimeMoniker.HostProcess)
{
var job = new Job(id);
Expand Down
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected CharacteristicObject()
sharedValues = new Dictionary<Characteristic, object>();
}

protected CharacteristicObject(string id) : this()
protected CharacteristicObject(string? id) : this()
{
if (!string.IsNullOrEmpty(id))
{
Expand Down Expand Up @@ -98,7 +98,7 @@ private static void AssertIsAssignable(Characteristic characteristic, object val

#region Properties

private CharacteristicObject Owner { get; set; }
private CharacteristicObject? Owner { get; set; }

protected CharacteristicObject OwnerOrSelf => Owner ?? this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class CharacteristicObject<T> : CharacteristicObject
{
protected CharacteristicObject() { }

protected CharacteristicObject(string id) : base(id) { }
protected CharacteristicObject(string? id) : base(id) { }

public new T Apply(CharacteristicObject other) => (T)ApplyCore(other);

Expand Down
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Characteristics/Characteristic`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Characteristic<[DynamicallyAccessedMembers(CharacteristicObject.Cha
internal Characteristic(
string id,
Type declaringType,
Func<CharacteristicObject, T, T> resolver,
Func<CharacteristicObject, T, T>? resolver,
T fallbackValue,
bool ignoreOnApply,
bool dontShowInSummary = false)
Expand All @@ -18,7 +18,7 @@ public class Characteristic<[DynamicallyAccessedMembers(CharacteristicObject.Cha
FallbackValue = fallbackValue;
}

private Func<CharacteristicObject, T, T> Resolver { get; }
private Func<CharacteristicObject, T, T>? Resolver { get; }

public T FallbackValue { get; }

Expand Down
17 changes: 10 additions & 7 deletions src/BenchmarkDotNet/Code/ArrayParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace BenchmarkDotNet.Code
internal static class ArrayParam
{
public static string GetDisplayString(Array array)
=> $"{array.GetType().GetElementType().GetDisplayName()}[{array.Length}]";
=> $"{array.GetType().GetElementType()?.GetDisplayName()}[{array.Length}]";
}

public class ArrayParam<T> : IParam
{
private readonly T[] array;
private readonly Func<T, string> toSourceCode;
private readonly Func<T, string>? toSourceCode;

private ArrayParam(T[] array, Func<T, string> toSourceCode = null)
private ArrayParam(T[] array, Func<T, string>? toSourceCode = null)
{
this.array = array;
this.toSourceCode = toSourceCode;
Expand All @@ -45,19 +45,22 @@ public string ToSourceCode()
/// </param>
[PublicAPI] public static ArrayParam<T> ForComplexTypes(T[] array, Func<T, string> toSourceCode) => new ArrayParam<T>(array, toSourceCode);

internal static IParam FromObject(object array)
internal static IParam? FromObject(object array)
{
var type = array.GetType();
if (!type.IsArray)
throw new InvalidOperationException("The argument must be an array");
if (!SourceCodeHelper.IsCompilationTimeConstant(type.GetElementType()))
var elementType = type.GetElementType();
if (elementType == null)
throw new InvalidOperationException("Failed to determine type of array elements");
if (!SourceCodeHelper.IsCompilationTimeConstant(elementType))
throw new InvalidOperationException("The argument must be an array of primitives");

var arrayParamType = typeof(ArrayParam<>).MakeGenericType(type.GetElementType());
var arrayParamType = typeof(ArrayParam<>).MakeGenericType(elementType);

var methodInfo = arrayParamType.GetMethod(nameof(ForPrimitives), BindingFlags.Public | BindingFlags.Static)
?? throw new InvalidOperationException($"{nameof(ForPrimitives)} not found");
return (IParam)methodInfo.Invoke(null, new[]{ array});
return (IParam?)methodInfo.Invoke(null, new[]{ array});
}
}
}
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Code/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal static string Generate(BuildPartition buildPartition)

private static void AddNonEmptyUnique(HashSet<string> items, string value)
{
if (!string.IsNullOrEmpty(value) && !items.Contains(value))
if (!string.IsNullOrEmpty(value))
items.Add(value);
}

Expand Down Expand Up @@ -296,7 +296,7 @@ public SmartStringBuilder(string text)
builder = new StringBuilder(text);
}

public SmartStringBuilder Replace(string oldValue, string newValue)
public SmartStringBuilder Replace(string oldValue, string? newValue)
{
if (originalText.Contains(oldValue))
builder.Replace(oldValue, newValue);
Expand Down
10 changes: 5 additions & 5 deletions src/BenchmarkDotNet/Jobs/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public sealed class Job : JobMode<Job>
public static readonly Job InProcess = new Job(nameof(InProcess), InfrastructureMode.InProcess);
public static readonly Job InProcessDontLogOutput = new Job(nameof(InProcessDontLogOutput), InfrastructureMode.InProcessDontLogOutput);

public Job() : this((string)null) { }
public Job() : this((string?)null) { }

public Job(string id) : base(id)
public Job(string? id) : base(id)
{
EnvironmentCharacteristic[this] = new EnvironmentMode();
RunCharacteristic[this] = new RunMode();
Expand All @@ -41,20 +41,20 @@ public Job(string id) : base(id)
MetaCharacteristic[this] = new MetaMode();
}

public Job(CharacteristicObject other) : this((string)null, other)
public Job(CharacteristicObject other) : this((string?)null, other)
{
}

public Job(params CharacteristicObject[] others) : this(null, others)
{
}

public Job(string id, CharacteristicObject other) : this(id)
public Job(string? id, CharacteristicObject other) : this(id)
{
Apply(other);
}

public Job(string id, params CharacteristicObject[] others) : this(id)
public Job(string? id, params CharacteristicObject[] others) : this(id)
{
Apply(others);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Jobs/JobMode`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class JobMode<T> : CharacteristicObject<T> where T : JobMode<T>,

protected JobMode() { }

protected JobMode(string id) : base(id) { }
protected JobMode(string? id) : base(id) { }

[PublicAPI] public Job Job => OwnerOrSelf as Job;
}
Expand Down