Skip to content

Commit 3531c12

Browse files
Port JetBrains' nullability annotations and clean-up code. (#2018)
* Make a helper method specific to .NET Standard 2.0. * Enable nullable annotations and remove the JetBrains attributes. * Update packages and use the SDK's analyzers. * Use newer platform detection APIs when available. * `IsMacOSX` -> `IsMacOS` * `IsiOS` -> `IsIOS`
1 parent 5ce28b2 commit 3531c12

File tree

71 files changed

+208
-246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+208
-246
lines changed

build/common.props

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<ComVisible>false</ComVisible>
1717

1818
<UseSharedCompilation>false</UseSharedCompilation>
19+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1920
<SuppressNETCoreSdkPreviewMessage>True</SuppressNETCoreSdkPreviewMessage>
2021
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingStyle.ruleset</CodeAnalysisRuleSet>
2122
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -57,7 +58,7 @@
5758
<PackageVersion>$(Major).$(Minor).$(Revision).$(BuildNumber)$(PrereleaseLabel)</PackageVersion>
5859
</PropertyGroup>
5960
<ItemGroup>
60-
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
61+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2">
6162
<PrivateAssets>all</PrivateAssets>
6263
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
6364
</PackageReference>
@@ -67,8 +68,5 @@
6768
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
6869
<PrivateAssets>all</PrivateAssets>
6970
</PackageReference>
70-
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
71-
<PrivateAssets>all</PrivateAssets>
72-
</PackageReference>
7371
</ItemGroup>
7472
</Project>

samples/BenchmarkDotNet.Samples/BenchmarkDotNet.Samples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
</PropertyGroup>
1515
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
1616
<Reference Include="System.Reflection" />
17+
<PackageReference Include="System.Memory" Version="4.5.5" />
1718
</ItemGroup>
1819
<ItemGroup>
1920
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2021
<PackageReference Include="System.Drawing.Common" Version="4.7.2" />
21-
<PackageReference Include="System.Memory" Version="4.5.5" />
2222
</ItemGroup>
2323
<ItemGroup>
2424
<ProjectReference Include="..\..\src\BenchmarkDotNet\BenchmarkDotNet.csproj" />

src/BenchmarkDotNet/Analysers/AnalyserBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public IEnumerable<Conclusion> Analyse(Summary summary)
2121
[PublicAPI] protected virtual IEnumerable<Conclusion> AnalyseSummary(Summary summary) => Enumerable.Empty<Conclusion>();
2222
[PublicAPI] protected virtual IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report, Summary summary) => Enumerable.Empty<Conclusion>();
2323

24-
protected Conclusion CreateHint(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
24+
protected Conclusion CreateHint(string message, BenchmarkReport? report = null, bool mergeable = true)
2525
=> Conclusion.CreateHint(Id, message, report, mergeable);
26-
protected Conclusion CreateWarning(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
26+
protected Conclusion CreateWarning(string message, BenchmarkReport? report = null, bool mergeable = true)
2727
=> Conclusion.CreateWarning(Id, message, report, mergeable);
28-
protected Conclusion CreateError(string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
28+
protected Conclusion CreateError(string message, BenchmarkReport? report = null, bool mergeable = true)
2929
=> Conclusion.CreateError(Id, message, report, mergeable);
3030
}
3131
}

src/BenchmarkDotNet/Analysers/Conclusion.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@ namespace BenchmarkDotNet.Analysers
77
// TODO: Find a better name
88
public sealed class Conclusion : IEquatable<Conclusion>
99
{
10-
[NotNull]
1110
public string AnalyserId { get; }
1211

1312
public ConclusionKind Kind { get; }
1413

1514
public bool Mergeable { get; }
1615

17-
[NotNull]
1816
public string Message { get; }
1917

20-
[CanBeNull]
21-
public BenchmarkReport Report { get; }
18+
public BenchmarkReport? Report { get; }
2219

23-
private Conclusion([NotNull] string analyserId,
20+
private Conclusion(string analyserId,
2421
ConclusionKind kind,
25-
[NotNull] string message,
26-
[CanBeNull] BenchmarkReport report,
22+
string message,
23+
BenchmarkReport? report,
2724
bool mergeable)
2825
{
2926
AnalyserId = analyserId;
@@ -33,13 +30,13 @@ private Conclusion([NotNull] string analyserId,
3330
Mergeable = mergeable;
3431
}
3532

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

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

42-
public static Conclusion CreateError(string analyserId, string message, [CanBeNull] BenchmarkReport report = null, bool mergeable = true)
39+
public static Conclusion CreateError(string analyserId, string message, BenchmarkReport? report = null, bool mergeable = true)
4340
=> new Conclusion(analyserId, ConclusionKind.Error, message, report, mergeable);
4441

4542
public bool Equals(Conclusion other)

src/BenchmarkDotNet/Analysers/MultimodalDistributionAnalyzer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ protected override IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report,
3030
yield return Create("can have several modes", mValue, report, summary.GetCultureInfo());
3131
}
3232

33-
[NotNull]
34-
private Conclusion Create([NotNull] string kind, double mValue, [CanBeNull] BenchmarkReport report, CultureInfo cultureInfo)
33+
private Conclusion Create(string kind, double mValue, BenchmarkReport? report, CultureInfo cultureInfo)
3534
=> CreateWarning($"It seems that the distribution {kind} (mValue = {mValue.ToString("0.##", cultureInfo)})", report);
3635
}
3736
}

src/BenchmarkDotNet/Analysers/OutliersAnalyser.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report,
5555
/// <param name="upperOutliers">All upper outliers</param>
5656
/// <param name="cultureInfo">CultureInfo</param>
5757
/// <returns>The message</returns>
58-
[PublicAPI, NotNull, Pure]
58+
[PublicAPI, Pure]
5959
public static string GetMessage(double[] actualOutliers, double[] allOutliers, double[] lowerOutliers, double[] upperOutliers, CultureInfo cultureInfo)
6060
{
6161
if (allOutliers.Length == 0)
@@ -80,8 +80,7 @@ string Format(int n, string verb)
8080
return Format(actualOutliers.Length, "removed") + ", " + Format(allOutliers.Length, "detected") + rangeMessage;
8181
}
8282

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

src/BenchmarkDotNet/BenchmarkDotNet.csproj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<AssemblyTitle>BenchmarkDotNet</AssemblyTitle>
55
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
<Nullable>annotations</Nullable>
78
<NoWarn>$(NoWarn);1701;1702;1705;1591;3005;NU1702;CS3001;CS3003</NoWarn>
89
<AssemblyName>BenchmarkDotNet</AssemblyName>
910
<PackageId>BenchmarkDotNet</PackageId>
@@ -22,16 +23,18 @@
2223
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.2.332302" />
2324
<PackageReference Include="Perfolizer" Version="0.2.1" />
2425
<PackageReference Include="System.Management" Version="6.0.0" />
25-
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
26-
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
27-
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
2826
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" />
2927
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.0.2" PrivateAssets="contentfiles;analyzers" />
3028
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
3129
</ItemGroup>
32-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
33-
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
30+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
3431
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
32+
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
33+
</ItemGroup>
34+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
35+
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
36+
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
37+
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
3538
</ItemGroup>
3639
<ItemGroup Condition="'$(OS)' == 'Windows_NT' AND '$(UseMonoRuntime)' != 'true' ">
3740
<ProjectReference Include="..\BenchmarkDotNet.Disassembler.x64\BenchmarkDotNet.Disassembler.x64.csproj">
@@ -48,7 +51,7 @@
4851
<ItemGroup>
4952
<ProjectReference Include="..\BenchmarkDotNet.Annotations\BenchmarkDotNet.Annotations.csproj" />
5053
</ItemGroup>
51-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
54+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
5255
<Compile Include="..\BenchmarkDotNet.Annotations\Attributes\DynamicallyAccessedMembersAttribute.cs" Link="Properties\DynamicallyAccessedMembersAttribute.cs" />
5356
<Compile Include="..\BenchmarkDotNet.Annotations\Attributes\DynamicallyAccessedMemberTypes.cs" Link="Properties\DynamicallyAccessedMemberTypes.cs" />
5457
</ItemGroup>

src/BenchmarkDotNet/Characteristics/CharacteristicObject.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Reflection;
66
using JetBrains.Annotations;
77

8-
using NotNullAttribute = JetBrains.Annotations.NotNullAttribute;
9-
108
namespace BenchmarkDotNet.Characteristics
119
{
1210
// TODO: better naming.
@@ -343,8 +341,8 @@ protected CharacteristicObject ApplyCore(CharacteristicObject other) =>
343341
GetCharacteristicsToApply(other));
344342

345343
private CharacteristicObject ApplyCore(
346-
[CanBeNull] CharacteristicObject other,
347-
[NotNull] IEnumerable<Characteristic> characteristicsToApply)
344+
CharacteristicObject? other,
345+
IEnumerable<Characteristic> characteristicsToApply)
348346
{
349347
AssertNotFrozen();
350348

src/BenchmarkDotNet/Columns/BaselineAllocationRatioColumn.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<s
6363
}
6464

6565
private static double? GetAllocationRatio(
66-
[CanBeNull] IReadOnlyDictionary<string, Metric> current,
67-
[CanBeNull] IReadOnlyDictionary<string, Metric> baseline)
66+
IReadOnlyDictionary<string, Metric>? current,
67+
IReadOnlyDictionary<string, Metric>? baseline)
6868
{
6969
double? currentBytes = GetAllocatedBytes(current);
7070
double? baselineBytes = GetAllocatedBytes(baseline);
@@ -78,7 +78,7 @@ private static bool IsNonBaselinesPrecise(Summary summary, IReadOnlyDictionary<s
7878
return currentBytes / baselineBytes;
7979
}
8080

81-
private static double? GetAllocatedBytes([CanBeNull] IReadOnlyDictionary<string, Metric> metrics)
81+
private static double? GetAllocatedBytes(IReadOnlyDictionary<string, Metric>? metrics)
8282
{
8383
var metric = metrics?.Values.FirstOrDefault(m => m.Descriptor is AllocatedMemoryMetricDescriptor);
8484
return metric?.Value;

src/BenchmarkDotNet/Columns/BaselineRatioColumn.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ private static bool IsNonBaselinesPrecise(Summary summary, Statistics baselineSt
118118
return nonBaselines.Any(x => GetRatioStatistics(summary[x].ResultStatistics, baselineStat)?.Mean < 0.01);
119119
}
120120

121-
[CanBeNull]
122-
private static Statistics GetRatioStatistics([CanBeNull] Statistics current, [CanBeNull] Statistics baseline)
121+
private static Statistics? GetRatioStatistics(Statistics? current, Statistics? baseline)
123122
{
124123
if (current == null || current.N < 1)
125124
return null;

0 commit comments

Comments
 (0)