Skip to content

Commit 4e93b69

Browse files
authored
chore: Enable nullable annotation on test projects (#3009)
* chore: enable nullable annotation on test projects * chore: add condition to skip overwrite nullable setting * chore: fix remaining nullable errors * chore: modify code based on comment * chore: simplify nullable relating code * chore: modify nullable annotation * chore: modify code based on review comment * chore: adjust spacing style * chore: modify fields modifier and remove default! * chore: add additional attribute definitions for roslyn toolchain * chore: add type ignore settings
1 parent be37238 commit 4e93b69

File tree

79 files changed

+391
-307
lines changed

Some content is hidden

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

79 files changed

+391
-307
lines changed

build/common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingStyle.ruleset</CodeAnalysisRuleSet>
2424
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2525
<WarningsNotAsErrors>NU1900</WarningsNotAsErrors>
26-
<Nullable>annotations</Nullable>
26+
<Nullable Condition="'$(Nullable)' == ''">annotations</Nullable>
2727
<!-- Suppress warning for nuget package used in old (unsupported) tfm. -->
2828
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
2929
</PropertyGroup>

src/BenchmarkDotNet/Disassemblers/DataContracts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ internal SourceCode() { }
3232

3333
public sealed class Sharp : SourceCode
3434
{
35-
public string Text { get; set; } = default!;
36-
public string FilePath { get; set; } = default!;
35+
public required string Text { get; set; }
36+
public required string FilePath { get; set; }
3737
public int LineNumber { get; set; }
3838
}
3939

src/BenchmarkDotNet/Templates/BenchmarkProgram.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,31 @@ namespace BenchmarkDotNet.Autogenerated
133133
}
134134
#endif // NETFRAMEWORK
135135

136-
$DerivedTypes$
136+
$DerivedTypes$
137137
}
138+
139+
#if !NET7_0_OR_GREATER
140+
namespace System.Runtime.CompilerServices
141+
{
142+
[global::System.AttributeUsage(global::System.AttributeTargets.All, AllowMultiple = true, Inherited = false)]
143+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
144+
internal sealed class CompilerFeatureRequiredAttribute : global::System.Attribute
145+
{
146+
public CompilerFeatureRequiredAttribute(global::System.String featureName)
147+
{
148+
FeatureName = featureName;
149+
}
150+
151+
public global::System.String FeatureName { get; }
152+
public global::System.Boolean IsOptional { get; set; }
153+
public const global::System.String RefStructs = nameof(RefStructs);
154+
public const global::System.String RequiredMembers = nameof(RequiredMembers);
155+
}
156+
157+
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Field | global::System.AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
158+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
159+
internal sealed class RequiredMemberAttribute : global::System.Attribute
160+
{
161+
}
162+
}
163+
#endif

tests/BenchmarkDotNet.Analyzers.Tests/BenchmarkDotNet.Analyzers.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<AssemblyName>BenchmarkDotNet.Analyzers.Tests</AssemblyName>
55
<TargetFrameworks>net462;net8.0;net10.0</TargetFrameworks>
6-
<Nullable>enable</Nullable>
76
<PreserveCompilationContext>true</PreserveCompilationContext>
87
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
98
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

tests/BenchmarkDotNet.Exporters.Plotting.Tests/ScottPlotExporterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ public class Invalid_TwoMethodBaselines
271271
{
272272
[Benchmark(Baseline = true)] public void Foo() { }
273273
[Benchmark(Baseline = true)] public void Bar() { }
274-
}
274+
}
275275
#pragma warning restore BDN1107
276-
276+
277277
[RankColumn, LogicalGroupColumn, BaselineColumn]
278278
[SimpleJob(id: "Job1", baseline: true), SimpleJob(id: "Job2", baseline: true)]
279279
public class Invalid_TwoJobBaselines
@@ -286,7 +286,7 @@ [Benchmark] public void Bar() { }
286286

287287
public class Escape_ParamsAndArguments
288288
{
289-
[Params("\t", "\n")] public string StringParam;
289+
[Params("\t", "\n")] public required string StringParam;
290290

291291
[Arguments('\t')]
292292
[Arguments('\n')]

tests/BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks/MultipleFrameworksTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
using BenchmarkDotNet.Extensions;
55
using BenchmarkDotNet.Jobs;
66
using Xunit;
7+
using Xunit.Abstractions;
78

89
namespace BenchmarkDotNet.IntegrationTests.ManualRunning
910
{
1011
public class MultipleFrameworksTest : BenchmarkTestExecutor
1112
{
1213
private const string TfmEnvVarName = "TfmEnvVarName";
1314

15+
public MultipleFrameworksTest(ITestOutputHelper output) : base(output)
16+
{
17+
}
18+
1419
[Theory]
1520
[InlineData(RuntimeMoniker.Net461)]
1621
[InlineData(RuntimeMoniker.Net48)]

tests/BenchmarkDotNet.IntegrationTests.ManualRunning/ExpectedBenchmarkResultsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,5 @@ public class EmptyVoidPointer
262262

263263
public class EmptyClass
264264
{
265-
[Benchmark] public object Class() => default;
265+
[Benchmark] public object? Class() => default;
266266
}

tests/BenchmarkDotNet.IntegrationTests.ManualRunning/MsBuildArgumentTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
using BenchmarkDotNet.Environments;
55
using BenchmarkDotNet.Jobs;
66
using Xunit;
7+
using Xunit.Abstractions;
78

89
namespace BenchmarkDotNet.IntegrationTests.ManualRunning
910
{
1011
public class MsBuildArgumentTests : BenchmarkTestExecutor
1112
{
1213
private const string CustomPropEnvVarName = "CustomPropEnvVarName";
1314

15+
public MsBuildArgumentTests(ITestOutputHelper output) : base(output)
16+
{
17+
}
18+
1419
[Theory]
1520
[InlineData(true)]
1621
[InlineData(false)]

tests/BenchmarkDotNet.IntegrationTests/ArgumentsTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ public void Simple(in bool boolean, in int number)
206206
public class WithComplexTypesReturnedFromSources
207207
{
208208
[ParamsSource(nameof(DictionaryAsParam))]
209-
public Dictionary<int, string> DictionaryParamInstance;
209+
public required Dictionary<int, string> DictionaryParamInstance;
210210

211211
[ParamsSource(nameof(SameButStatic))]
212-
public Dictionary<int, string> DictionaryParamStatic;
212+
public required Dictionary<int, string> DictionaryParamStatic;
213213

214214
[Benchmark]
215215
[ArgumentsSource(nameof(NonPrimitive))]
@@ -1072,10 +1072,10 @@ public class ParamsSourcePointingToAnotherClass
10721072
public int ParamTwo { get; set; }
10731073

10741074
[ParamsSource(typeof(ExternalClassWithParamsSource), nameof(ExternalClassWithParamsSource.NonPrimitiveTypeMethod))]
1075-
public Version ParamThree { get; set; }
1075+
public required Version ParamThree { get; set; }
10761076

10771077
[ParamsSource(typeof(ExternalClassWithParamsSource), nameof(ExternalClassWithParamsSource.NonPrimitiveTypeProperty))]
1078-
public Version ParamFour { get; set; }
1078+
public required Version ParamFour { get; set; }
10791079

10801080
[Benchmark]
10811081
public void Test()
@@ -1146,7 +1146,7 @@ public void Test(string first, string second)
11461146
public class Perf_Regex_Industry_RustLang_Sherlock
11471147
{
11481148
[Params(@"[""'][^""']{0,30}[?!.][""']")]
1149-
public string Pattern { get; set; }
1149+
public required string Pattern { get; set; }
11501150

11511151
[Benchmark]
11521152
public int Consume() => Pattern.Length;
@@ -1164,7 +1164,7 @@ public IEnumerable<Disposable> GetDisposables()
11641164
}
11651165

11661166
[ParamsSource(nameof(GetDisposables))]
1167-
public Disposable used;
1167+
public required Disposable used;
11681168

11691169
[Benchmark]
11701170
public void CheckDisposed()
@@ -1202,7 +1202,7 @@ public Disposable(int id)
12021202
}
12031203
}
12041204

1205-
private Reports.Summary CanExecute<T>(IToolchain toolchain, Func<IConfig, IConfig> furtherConfigure = null)
1205+
private Reports.Summary CanExecute<T>(IToolchain toolchain, Func<IConfig, IConfig>? furtherConfigure = null)
12061206
{
12071207
var config = CreateSimpleConfig(job: Job.Dry.WithToolchain(toolchain));
12081208

tests/BenchmarkDotNet.IntegrationTests/AsyncBenchmarksTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal class ValueTaskSource<T> : IValueTaskSource<T>, IValueTaskSource
1515
void IValueTaskSource.GetResult(short token) => _core.GetResult(token);
1616
ValueTaskSourceStatus IValueTaskSource<T>.GetStatus(short token) => _core.GetStatus(token);
1717
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) => _core.GetStatus(token);
18-
void IValueTaskSource<T>.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
19-
void IValueTaskSource.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
18+
void IValueTaskSource<T>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
19+
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
2020
public void Reset() => _core.Reset();
2121
public short Token => _core.Version;
2222
public void SetResult(T result) => _core.SetResult(result);
@@ -32,8 +32,8 @@ internal class ValueTaskSourceCallbackOnly<T> : IValueTaskSource<T>, IValueTaskS
3232
// Always return pending state so OnCompleted will be called.
3333
ValueTaskSourceStatus IValueTaskSource<T>.GetStatus(short token) => ValueTaskSourceStatus.Pending;
3434
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) => ValueTaskSourceStatus.Pending;
35-
void IValueTaskSource<T>.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
36-
void IValueTaskSource.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
35+
void IValueTaskSource<T>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
36+
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _core.OnCompleted(continuation, state, token, flags);
3737
public void Reset() => _core.Reset();
3838
public short Token => _core.Version;
3939
public void SetResult(T result) => _core.SetResult(result);
@@ -49,13 +49,13 @@ public void TaskReturningMethodsAreAwaited()
4949
var summary = CanExecute<TaskDelayMethods>();
5050

5151
foreach (var report in summary.Reports)
52-
foreach (var measurement in report.AllMeasurements)
53-
{
54-
double actual = measurement.Nanoseconds;
55-
const double minExpected = TaskDelayMethods.NanosecondsDelay - TaskDelayMethods.MaxTaskDelayResolutionInNanoseconds;
56-
string name = report.BenchmarkCase.Descriptor.GetFilterName();
57-
Assert.True(actual > minExpected, $"{name} has not been awaited, took {actual}ns, while it should take more than {minExpected}ns");
58-
}
52+
foreach (var measurement in report.AllMeasurements)
53+
{
54+
double actual = measurement.Nanoseconds;
55+
const double minExpected = TaskDelayMethods.NanosecondsDelay - TaskDelayMethods.MaxTaskDelayResolutionInNanoseconds;
56+
string name = report.BenchmarkCase.Descriptor.GetFilterName();
57+
Assert.True(actual > minExpected, $"{name} has not been awaited, took {actual}ns, while it should take more than {minExpected}ns");
58+
}
5959
}
6060

6161
[Fact]

0 commit comments

Comments
 (0)