Skip to content

Commit

Permalink
Fixed nullability warnings for some files from BenchmarkDotNet project
Browse files Browse the repository at this point in the history
  • Loading branch information
alinasmirnova authored and AndreyAkinshin committed Aug 29, 2023
1 parent 3860e4a commit 2a8bab5
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Analysers/Conclusion.cs
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
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
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
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
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
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
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
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});
}
}
}
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Code/CodeGenerator.cs
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
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
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

0 comments on commit 2a8bab5

Please sign in to comment.