diff --git a/src/BenchmarkDotNet/Analysers/Conclusion.cs b/src/BenchmarkDotNet/Analysers/Conclusion.cs index 10a9f3eeb0..0916bf9beb 100644 --- a/src/BenchmarkDotNet/Analysers/Conclusion.cs +++ b/src/BenchmarkDotNet/Analysers/Conclusion.cs @@ -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; @@ -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; diff --git a/src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs b/src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs index d94087239c..5e3f650167 100644 --- a/src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs +++ b/src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs @@ -20,7 +20,7 @@ public static bool CheckZeroMeasurementOneSample(double[] results, double thresh /// Checks distribution against Zero Measurement hypothesis in case of two samples /// /// True if measurement is ZeroMeasurement - 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; diff --git a/src/BenchmarkDotNet/Attributes/Filters/AotFilterAttribute.cs b/src/BenchmarkDotNet/Attributes/Filters/AotFilterAttribute.cs index 846fef2b8e..da0f610714 100644 --- a/src/BenchmarkDotNet/Attributes/Filters/AotFilterAttribute.cs +++ b/src/BenchmarkDotNet/Attributes/Filters/AotFilterAttribute.cs @@ -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)) { } diff --git a/src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs b/src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs index e0b80cf482..7a7052483c 100644 --- a/src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs +++ b/src/BenchmarkDotNet/Attributes/Jobs/SimpleJobAttribute.cs @@ -18,7 +18,7 @@ public SimpleJobAttribute( 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)) { } @@ -29,7 +29,7 @@ public SimpleJobAttribute( 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)) { } @@ -40,7 +40,7 @@ public SimpleJobAttribute( 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)) { } @@ -52,11 +52,11 @@ public SimpleJobAttribute( 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); diff --git a/src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs b/src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs index b01f39e0fe..a6d7b37b81 100644 --- a/src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs +++ b/src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs @@ -44,7 +44,7 @@ protected CharacteristicObject() sharedValues = new Dictionary(); } - protected CharacteristicObject(string id) : this() + protected CharacteristicObject(string? id) : this() { if (!string.IsNullOrEmpty(id)) { @@ -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; diff --git a/src/BenchmarkDotNet/Characteristics/CharacteristicObject`1.cs b/src/BenchmarkDotNet/Characteristics/CharacteristicObject`1.cs index a5959e1b62..e69167dc7b 100644 --- a/src/BenchmarkDotNet/Characteristics/CharacteristicObject`1.cs +++ b/src/BenchmarkDotNet/Characteristics/CharacteristicObject`1.cs @@ -8,7 +8,7 @@ public abstract class CharacteristicObject : CharacteristicObject { protected CharacteristicObject() { } - protected CharacteristicObject(string id) : base(id) { } + protected CharacteristicObject(string? id) : base(id) { } public new T Apply(CharacteristicObject other) => (T)ApplyCore(other); diff --git a/src/BenchmarkDotNet/Characteristics/Characteristic`1.cs b/src/BenchmarkDotNet/Characteristics/Characteristic`1.cs index 2cc61832a2..a5bc131817 100644 --- a/src/BenchmarkDotNet/Characteristics/Characteristic`1.cs +++ b/src/BenchmarkDotNet/Characteristics/Characteristic`1.cs @@ -8,7 +8,7 @@ public class Characteristic<[DynamicallyAccessedMembers(CharacteristicObject.Cha internal Characteristic( string id, Type declaringType, - Func resolver, + Func? resolver, T fallbackValue, bool ignoreOnApply, bool dontShowInSummary = false) @@ -18,7 +18,7 @@ internal Characteristic( FallbackValue = fallbackValue; } - private Func Resolver { get; } + private Func? Resolver { get; } public T FallbackValue { get; } diff --git a/src/BenchmarkDotNet/Code/ArrayParam.cs b/src/BenchmarkDotNet/Code/ArrayParam.cs index 1319067533..536f126f21 100644 --- a/src/BenchmarkDotNet/Code/ArrayParam.cs +++ b/src/BenchmarkDotNet/Code/ArrayParam.cs @@ -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 : IParam { private readonly T[] array; - private readonly Func toSourceCode; + private readonly Func? toSourceCode; - private ArrayParam(T[] array, Func toSourceCode = null) + private ArrayParam(T[] array, Func? toSourceCode = null) { this.array = array; this.toSourceCode = toSourceCode; @@ -45,19 +45,22 @@ public string ToSourceCode() /// [PublicAPI] public static ArrayParam ForComplexTypes(T[] array, Func toSourceCode) => new ArrayParam(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}); } } } \ No newline at end of file diff --git a/src/BenchmarkDotNet/Code/CodeGenerator.cs b/src/BenchmarkDotNet/Code/CodeGenerator.cs index 52ae0e5478..005564b77c 100644 --- a/src/BenchmarkDotNet/Code/CodeGenerator.cs +++ b/src/BenchmarkDotNet/Code/CodeGenerator.cs @@ -93,7 +93,7 @@ internal static string Generate(BuildPartition buildPartition) private static void AddNonEmptyUnique(HashSet items, string value) { - if (!string.IsNullOrEmpty(value) && !items.Contains(value)) + if (!string.IsNullOrEmpty(value)) items.Add(value); } @@ -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); diff --git a/src/BenchmarkDotNet/Jobs/Job.cs b/src/BenchmarkDotNet/Jobs/Job.cs index eb73a1b6d7..0985b5e578 100644 --- a/src/BenchmarkDotNet/Jobs/Job.cs +++ b/src/BenchmarkDotNet/Jobs/Job.cs @@ -30,9 +30,9 @@ public sealed class Job : JobMode 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(); @@ -41,7 +41,7 @@ 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) { } @@ -49,12 +49,12 @@ 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); } diff --git a/src/BenchmarkDotNet/Jobs/JobMode`1.cs b/src/BenchmarkDotNet/Jobs/JobMode`1.cs index 5ffb4c07d8..61a51a8bd8 100644 --- a/src/BenchmarkDotNet/Jobs/JobMode`1.cs +++ b/src/BenchmarkDotNet/Jobs/JobMode`1.cs @@ -9,7 +9,7 @@ namespace BenchmarkDotNet.Jobs protected JobMode() { } - protected JobMode(string id) : base(id) { } + protected JobMode(string? id) : base(id) { } [PublicAPI] public Job Job => OwnerOrSelf as Job; }