Skip to content

Port JetBrains' nullability annotations and clean-up code. #2018

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

Merged
merged 16 commits into from
Dec 5, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ComVisible>false</ComVisible>

<UseSharedCompilation>false</UseSharedCompilation>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<SuppressNETCoreSdkPreviewMessage>True</SuppressNETCoreSdkPreviewMessage>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingStyle.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -57,7 +58,7 @@
<PackageVersion>$(Major).$(Minor).$(Revision).$(BuildNumber)$(PrereleaseLabel)</PackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand All @@ -67,8 +68,5 @@
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
<Reference Include="System.Reflection" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Drawing.Common" Version="4.7.2" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\BenchmarkDotNet\BenchmarkDotNet.csproj" />
Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet/Analysers/AnalyserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public IEnumerable<Conclusion> Analyse(Summary summary)
[PublicAPI] protected virtual IEnumerable<Conclusion> AnalyseSummary(Summary summary) => Enumerable.Empty<Conclusion>();
[PublicAPI] protected virtual IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report, Summary summary) => Enumerable.Empty<Conclusion>();

protected Conclusion CreateHint(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
protected Conclusion CreateHint(string message, BenchmarkReport? report = null, bool mergeable = true)
=> Conclusion.CreateHint(Id, message, report, mergeable);
protected Conclusion CreateWarning(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
protected Conclusion CreateWarning(string message, BenchmarkReport? report = null, bool mergeable = true)
=> Conclusion.CreateWarning(Id, message, report, mergeable);
protected Conclusion CreateError(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
protected Conclusion CreateError(string message, BenchmarkReport? report = null, bool mergeable = true)
=> Conclusion.CreateError(Id, message, report, mergeable);
}
}
17 changes: 7 additions & 10 deletions src/BenchmarkDotNet/Analysers/Conclusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ namespace BenchmarkDotNet.Analysers
// TODO: Find a better name
public sealed class Conclusion : IEquatable<Conclusion>
{
[NotNull]
public string AnalyserId { get; }

public ConclusionKind Kind { get; }

public bool Mergeable { get; }

[NotNull]
public string Message { get; }

[CanBeNull]
public BenchmarkReport Report { get; }
public BenchmarkReport? Report { get; }

private Conclusion([NotNull] string analyserId,
private Conclusion(string analyserId,
ConclusionKind kind,
[NotNull] string message,
[CanBeNull] BenchmarkReport report,
string message,
BenchmarkReport? report,
bool mergeable)
{
AnalyserId = analyserId;
Expand All @@ -33,13 +30,13 @@ private Conclusion([NotNull] string analyserId,
Mergeable = mergeable;
}

public static Conclusion CreateHint(string analyserId, string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
public static Conclusion CreateHint(string analyserId, string message, BenchmarkReport? report = null, bool mergeable = true)
=> new Conclusion(analyserId, ConclusionKind.Hint, message, report, mergeable);

public static Conclusion CreateWarning(string analyserId, string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
public static Conclusion CreateWarning(string analyserId, string message, BenchmarkReport? report = null, bool mergeable = true)
=> new Conclusion(analyserId, ConclusionKind.Warning, message, report, mergeable);

public static Conclusion CreateError(string analyserId, string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ protected override IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report,
yield return Create("can have several modes", mValue, report, summary.GetCultureInfo());
}

[NotNull]
private Conclusion Create([NotNull] string kind, double mValue, [CanBeNull] BenchmarkReport report, CultureInfo cultureInfo)
private Conclusion Create(string kind, double mValue, BenchmarkReport? report, CultureInfo cultureInfo)
=> CreateWarning($"It seems that the distribution {kind} (mValue = {mValue.ToString("0.##", cultureInfo)})", report);
}
}
5 changes: 2 additions & 3 deletions src/BenchmarkDotNet/Analysers/OutliersAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report,
/// <param name="upperOutliers">All upper outliers</param>
/// <param name="cultureInfo">CultureInfo</param>
/// <returns>The message</returns>
[PublicAPI, NotNull, Pure]
[PublicAPI, Pure]
public static string GetMessage(double[] actualOutliers, double[] allOutliers, double[] lowerOutliers, double[] upperOutliers, CultureInfo cultureInfo)
{
if (allOutliers.Length == 0)
Expand All @@ -80,8 +80,7 @@ string Format(int n, string verb)
return Format(actualOutliers.Length, "removed") + ", " + Format(allOutliers.Length, "detected") + rangeMessage;
}

[CanBeNull]
private static string GetRangeMessage([NotNull] double[] values, CultureInfo cultureInfo)
private static string? GetRangeMessage(double[] values, CultureInfo cultureInfo)
{
string Format(double value) => TimeInterval.FromNanoseconds(value).ToString(cultureInfo, "N2");

Expand Down
15 changes: 9 additions & 6 deletions src/BenchmarkDotNet/BenchmarkDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AssemblyTitle>BenchmarkDotNet</AssemblyTitle>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>annotations</Nullable>
<NoWarn>$(NoWarn);1701;1702;1705;1591;3005;NU1702;CS3001;CS3003</NoWarn>
<AssemblyName>BenchmarkDotNet</AssemblyName>
<PackageId>BenchmarkDotNet</PackageId>
Expand All @@ -22,16 +23,18 @@
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.2.332302" />
<PackageReference Include="Perfolizer" Version="0.2.1" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.0.2" PrivateAssets="contentfiles;analyzers" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
<ItemGroup Condition="'$(OS)' == 'Windows_NT' AND '$(UseMonoRuntime)' != 'true' ">
<ProjectReference Include="..\BenchmarkDotNet.Disassembler.x64\BenchmarkDotNet.Disassembler.x64.csproj">
Expand All @@ -48,7 +51,7 @@
<ItemGroup>
<ProjectReference Include="..\BenchmarkDotNet.Annotations\BenchmarkDotNet.Annotations.csproj" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
<Compile Include="..\BenchmarkDotNet.Annotations\Attributes\DynamicallyAccessedMembersAttribute.cs" Link="Properties\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="..\BenchmarkDotNet.Annotations\Attributes\DynamicallyAccessedMemberTypes.cs" Link="Properties\DynamicallyAccessedMemberTypes.cs" />
</ItemGroup>
Expand Down
6 changes: 2 additions & 4 deletions src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Reflection;
using JetBrains.Annotations;

using NotNullAttribute = JetBrains.Annotations.NotNullAttribute;

namespace BenchmarkDotNet.Characteristics
{
// TODO: better naming.
Expand Down Expand Up @@ -343,8 +341,8 @@ protected CharacteristicObject ApplyCore(CharacteristicObject other) =>
GetCharacteristicsToApply(other));

private CharacteristicObject ApplyCore(
[CanBeNull] CharacteristicObject other,
[NotNull] IEnumerable<Characteristic> characteristicsToApply)
CharacteristicObject? other,
IEnumerable<Characteristic> characteristicsToApply)
{
AssertNotFrozen();

Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet/Columns/BaselineAllocationRatioColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<s
}

private static double? GetAllocationRatio(
[CanBeNull] IReadOnlyDictionary<string, Metric> current,
[CanBeNull] IReadOnlyDictionary<string, Metric> baseline)
IReadOnlyDictionary<string, Metric>? current,
IReadOnlyDictionary<string, Metric>? baseline)
{
double? currentBytes = GetAllocatedBytes(current);
double? baselineBytes = GetAllocatedBytes(baseline);
Expand All @@ -78,7 +78,7 @@ private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<s
return currentBytes / baselineBytes;
}

private static double? GetAllocatedBytes([CanBeNull] IReadOnlyDictionary<string, Metric> metrics)
private static double? GetAllocatedBytes(IReadOnlyDictionary<string, Metric>? metrics)
{
var metric = metrics?.Values.FirstOrDefault(m => m.Descriptor is AllocatedMemoryMetricDescriptor);
return metric?.Value;
Expand Down
3 changes: 1 addition & 2 deletions src/BenchmarkDotNet/Columns/BaselineRatioColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ private static bool IsNonBaselinesPrecise(Summary summary, Statistics baselineSt
return nonBaselines.Any(x => GetRatioStatistics(summary[x].ResultStatistics, baselineStat)?.Mean < 0.01);
}

[CanBeNull]
private static Statistics GetRatioStatistics([CanBeNull] Statistics current, [CanBeNull] Statistics baseline)
private static Statistics? GetRatioStatistics(Statistics? current, Statistics? baseline)
{
if (current == null || current.N < 1)
return null;
Expand Down
18 changes: 9 additions & 9 deletions src/BenchmarkDotNet/Columns/SizeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public SizeValue(long bytes, SizeUnit unit) : this(bytes * unit.ByteAmount) { }
[Pure] public static SizeValue operator *(SizeValue value, long k) => new SizeValue(value.Bytes * k);
[Pure] public static SizeValue operator *(long k, SizeValue value) => new SizeValue(value.Bytes * k);

[Pure, NotNull]
[Pure]
public string ToString(
[CanBeNull] CultureInfo cultureInfo,
[CanBeNull] string format = "0.##",
[CanBeNull] UnitPresentation unitPresentation = null)
CultureInfo? cultureInfo,
string? format = "0.##",
UnitPresentation? unitPresentation = null)
{
return ToString(null, cultureInfo, format, unitPresentation);
}

[Pure, NotNull]
[Pure]
public string ToString(
[CanBeNull] SizeUnit sizeUnit,
[CanBeNull] CultureInfo cultureInfo,
[CanBeNull] string format = "0.##",
[CanBeNull] UnitPresentation unitPresentation = null)
SizeUnit? sizeUnit,
CultureInfo? cultureInfo,
string? format = "0.##",
UnitPresentation? unitPresentation = null)
{
sizeUnit = sizeUnit ?? SizeUnit.GetBestSizeUnit(Bytes);
cultureInfo = cultureInfo ?? DefaultCultureInfo.Instance;
Expand Down
5 changes: 2 additions & 3 deletions src/BenchmarkDotNet/Configs/IConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IConfig
IEnumerable<BenchmarkLogicalGroupRule> GetLogicalGroupRules();
IEnumerable<IColumnHidingRule> GetColumnHidingRules();

[CanBeNull] IOrderer Orderer { get; }
IOrderer? Orderer { get; }

SummaryStyle SummaryStyle { get; }

Expand All @@ -40,8 +40,7 @@ public interface IConfig
/// </summary>
string ArtifactsPath { get; }

[CanBeNull]
CultureInfo CultureInfo { get; }
CultureInfo? CultureInfo { get; }

/// <summary>
/// a set of custom flags that can enable/disable various settings
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Configs/ImmutableConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal ImmutableConfig(
public string ArtifactsPath { get; }
public CultureInfo CultureInfo { get; }
public ConfigOptions Options { get; }
[NotNull] public IOrderer Orderer { get; }
public IOrderer Orderer { get; }
public SummaryStyle SummaryStyle { get; }
public TimeSpan BuildTimeout { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CorrectionsSuggester(IReadOnlyList<Type> types)
}
}

public string[] SuggestFor([NotNull] string userInput)
public string[] SuggestFor(string userInput)
{
if (userInput == null)
throw new ArgumentNullException(nameof(userInput));
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Disassemblers/DisassemblyDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public IEnumerable<ValidationError> Validate(ValidationParameters validationPara
private static bool ShouldUseMonoDisassembler(BenchmarkCase benchmarkCase)
=> benchmarkCase.Job.Environment.Runtime is MonoRuntime || RuntimeInformation.IsMono;

// when we add macOS support, RuntimeInformation.IsMacOSX() needs to be added here
// when we add macOS support, RuntimeInformation.IsMacOS() needs to be added here
private static bool ShouldUseClrMdDisassembler(BenchmarkCase benchmarkCase)
=> !ShouldUseMonoDisassembler(benchmarkCase) && (RuntimeInformation.IsWindows() || RuntimeInformation.IsLinux());

Expand Down
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Disassemblers/MonoDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static string GetLlvmFlag(Job job) =>

internal static class OutputParser
{
internal static DisassemblyResult Parse([ItemCanBeNull] IReadOnlyList<string> input, string methodName, string commandLine)
internal static DisassemblyResult Parse(IReadOnlyList<string?> input, string methodName, string commandLine)
{
var instructions = new List<MonoCode>();

Expand Down Expand Up @@ -101,7 +101,7 @@ internal static DisassemblyResult Parse([ItemCanBeNull] IReadOnlyList<string> in
};
}

private static DisassemblyResult CreateErrorResult([ItemCanBeNull] IReadOnlyList<string> input,
private static DisassemblyResult CreateErrorResult(IReadOnlyList<string?> input,
string methodName, string commandLine, string message)
{
return new DisassemblyResult
Expand Down
3 changes: 1 addition & 2 deletions src/BenchmarkDotNet/Engines/EnginePilotStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ internal class EnginePilotStage : EngineStage
public readonly struct PilotStageResult
{
public long PerfectInvocationCount { get; }
[NotNull]
public IReadOnlyList<Measurement> Measurements { get; }

public PilotStageResult(long perfectInvocationCount, [NotNull] List<Measurement> measurements)
public PilotStageResult(long perfectInvocationCount, List<Measurement> measurements)
{
PerfectInvocationCount = perfectInvocationCount;
Measurements = measurements;
Expand Down
12 changes: 2 additions & 10 deletions src/BenchmarkDotNet/Engines/IEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,28 @@
using BenchmarkDotNet.Characteristics;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using JetBrains.Annotations;
using NotNullAttribute = JetBrains.Annotations.NotNullAttribute;

namespace BenchmarkDotNet.Engines
{
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
public interface IEngine : IDisposable
{
[NotNull]
IHost Host { get; }

void WriteLine();

void WriteLine(string line);

[NotNull]
Job TargetJob { get; }

long OperationsPerInvoke { get; }

[CanBeNull]
Action GlobalSetupAction { get; }
Action? GlobalSetupAction { get; }

[CanBeNull]
Action GlobalCleanupAction { get; }
Action? GlobalCleanupAction { get; }

[NotNull]
Action<long> WorkloadAction { get; }

[NotNull]
Action<long> OverheadAction { get; }

IResolver Resolver { get; }
Expand Down
10 changes: 5 additions & 5 deletions src/BenchmarkDotNet/Engines/RunResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public struct RunResults
{
private readonly OutlierMode outlierMode;

[NotNull, PublicAPI]
[PublicAPI]
public IReadOnlyList<Measurement> EngineMeasurements { get; }

[CanBeNull, PublicAPI]
public IReadOnlyList<Measurement> Overhead
[PublicAPI]
public IReadOnlyList<Measurement>? Overhead
=> EngineMeasurements
.Where(m => m.Is(IterationMode.Overhead, IterationStage.Actual))
.ToArray();

[NotNull, PublicAPI]
[PublicAPI]
public IReadOnlyList<Measurement> Workload
=> EngineMeasurements
.Where(m => m.Is(IterationMode.Workload, IterationStage.Actual))
Expand All @@ -35,7 +35,7 @@ public IReadOnlyList<Measurement> Workload

public double ExceptionFrequency { get; }

public RunResults([NotNull] IReadOnlyList<Measurement> engineMeasurements,
public RunResults(IReadOnlyList<Measurement> engineMeasurements,
OutlierMode outlierMode,
GcStats gcStats,
ThreadingStats threadingStats,
Expand Down
Loading