Skip to content

Commit b9936a2

Browse files
committed
enable nullability annotations
1 parent 0259a36 commit b9936a2

File tree

6 files changed

+50
-30
lines changed

6 files changed

+50
-30
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi.Build/MSBuildExePath.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ string path
6161
RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled
6262
);
6363

64-
private static string GetSdkBasePath(out string sdkVersion)
64+
private static string? GetSdkBasePath(out string? sdkVersion)
6565
{
6666
sdkVersion = default;
6767

@@ -78,7 +78,7 @@ private static string GetSdkBasePath(out string sdkVersion)
7878
return null;
7979
}
8080

81-
private static string GetMSBuildExePath(out string sdkVersion)
81+
private static string GetMSBuildExePath(out string? sdkVersion)
8282
{
8383
#if true
8484
sdkVersion = default;
@@ -134,7 +134,7 @@ string msbuildPath
134134
#endif
135135
}
136136

137-
public static void EnsureSetEnvVar(ILogger logger = null)
137+
public static void EnsureSetEnvVar(ILogger? logger = null)
138138
{
139139
const string MSBUILD_EXE_PATH = nameof(MSBUILD_EXE_PATH);
140140

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi.Build/ProjectBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public static class ProjectBuilder {
2121
public class Options {
2222
public const string DefaultConfiguration = "Debug";
2323

24-
public string Configuration { get; init; } = DefaultConfiguration;
25-
public string TargetFramework { get; init; } = null;
26-
// public string OS { get; init; }
27-
public string RuntimeIdentifier { get; init; } = null;
28-
public string[] TargetsToBuild { get; init; } = new[] { "Restore", "Build" };
24+
public string? Configuration { get; init; } = DefaultConfiguration;
25+
public string? TargetFramework { get; init; } = null;
26+
// public string? OS { get; init; }
27+
public string? RuntimeIdentifier { get; init; } = null;
28+
public string[]? TargetsToBuild { get; init; } = new[] { "Restore", "Build" };
2929
public LoggerVerbosity LoggerVerbosity { get; init; } = LoggerVerbosity.Minimal;
3030
}
3131

3232
public static IEnumerable<FileInfo> Build(
3333
FileInfo projectFile,
34-
Options options = null,
35-
Microsoft.Extensions.Logging.ILogger logger = null
34+
Options? options = null,
35+
Microsoft.Extensions.Logging.ILogger? logger = null
3636
)
3737
{
3838
if (projectFile is null)
@@ -56,7 +56,7 @@ public static IEnumerable<FileInfo> Build(
5656
logger?.LogDebug("Build requested");
5757
logger?.LogDebug(" project: {ProjectFile}", projectFile);
5858

59-
logger?.LogDebug(" targets: {Targets}", string.Join(";", options.TargetsToBuild));
59+
logger?.LogDebug(" targets: {Targets}", string.Join(";", options.TargetsToBuild ?? Array.Empty<string>()));
6060

6161
logger?.LogDebug(" global properties:");
6262

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SPDX-License-Identifier: MIT
1111
<ToolCommandName>list-api</ToolCommandName>
1212
<VersionPrefix>1.1.0</VersionPrefix>
1313
<VersionSuffix></VersionSuffix>
14+
<Nullable>enable</Nullable>
1415
<EnableFeature_BuildProjectFile Condition=" '$(Configuration)' != 'Release' ">true</EnableFeature_BuildProjectFile>
1516
<GeneratePackageReadmeFile>false</GeneratePackageReadmeFile>
1617
<DefineConstants

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/Program.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Smdn.Reflection.ReverseGenerating.ListApi;
1313

1414
internal class Program {
15-
internal static string LoggerCategoryName => typeof(Program).Assembly.GetName().Name;
15+
internal static string LoggerCategoryName => typeof(Program).Assembly.GetName().Name!;
1616

1717
private static int Main(string[] args)
1818
{
@@ -30,13 +30,23 @@ private static int Main(string[] args)
3030
.UseDefaults()
3131
.UseExceptionHandler(
3232
onException: static (ex, context) => {
33+
Exception? _ex = ex;
34+
3335
if (ex is TargetInvocationException exTargetInvocation)
34-
ex = exTargetInvocation.InnerException;
36+
_ex = exTargetInvocation.InnerException;
37+
38+
switch (_ex) {
39+
case InvalidCommandOperationException exInvalidCommandOperation:
40+
Console.Error.WriteLine(exInvalidCommandOperation.Message);
41+
break;
42+
43+
case CommandOperationNotSupportedException exCommandOperationNotSupported:
44+
Console.Error.WriteLine(exCommandOperationNotSupported.Message);
45+
break;
3546

36-
switch (ex) {
37-
case InvalidCommandOperationException exInvalidCommandOperation: Console.Error.WriteLine(ex.Message); break;
38-
case CommandOperationNotSupportedException exCommandOperationNotSupported: Console.Error.WriteLine(ex.Message); break;
39-
default: Console.Error.WriteLine(ex); break;
47+
default:
48+
Console.Error.WriteLine(_ex);
49+
break;
4050
}
4151
},
4252
errorExitCode: -1

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/ProjectFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Smdn.Reflection.ReverseGenerating.ListApi;
1414
public static class ProjectFinder {
1515
public static FileInfo FindSingleProjectOrSolution(
1616
DirectoryInfo directory,
17-
ILogger logger = null
17+
ILogger? logger = null
1818
)
1919
{
2020
logger?.LogDebug("finding project or solution from directory '{Directory}'", directory.FullName);

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/RootCommandImplementation.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using System.CommandLine.Builder;
99
using System.CommandLine.Invocation;
1010
using System.CommandLine.Parsing;
11+
#if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES
12+
using System.Diagnostics.CodeAnalysis;
13+
#endif
1114
using System.IO;
1215
using System.Linq;
1316
using System.Reflection;
@@ -39,22 +42,22 @@ public class RootCommandImplementation {
3942
Arity = ArgumentArity.ExactlyOne,
4043
};
4144

42-
private static readonly Option<string> optionConfiguration = new(
45+
private static readonly Option<string?> optionConfiguration = new(
4346
aliases: new[] { "-c", "--configuration" },
4447
description: "The 'build configuration' option passed to `Build` target when the project will be built.",
4548
getDefaultValue: static () => DefaultBuildConfiguration
4649
);
47-
private static readonly Option<string> optionTargetFramework = new(
50+
private static readonly Option<string?> optionTargetFramework = new(
4851
aliases: new[] { "-f", "--framework" },
4952
description: "The 'target framework' option passed to `Build` target when the project will be built.",
5053
getDefaultValue: static () => null
5154
);
52-
private static readonly Option<string> optionRuntimeIdentifier = new(
55+
private static readonly Option<string?> optionRuntimeIdentifier = new(
5356
aliases: new[] { "-r", "--runtime" },
5457
description: "The 'target runtime' option passed to `Build` target when the project will be built.",
5558
getDefaultValue: static () => null
5659
);
57-
private static readonly Option<string> optionOS = new(
60+
private static readonly Option<string?> optionOS = new(
5861
aliases: new[] { "--os" },
5962
description: "The 'target operating system' option passed to `Build` target when the project will be built.",
6063
getDefaultValue: static () => null
@@ -90,10 +93,10 @@ public class RootCommandImplementation {
9093
getDefaultValue: static () => false
9194
);
9295

93-
private readonly IServiceProvider serviceProvider;
94-
private readonly Microsoft.Extensions.Logging.ILogger logger;
96+
private readonly IServiceProvider? serviceProvider;
97+
private readonly Microsoft.Extensions.Logging.ILogger? logger;
9598

96-
public RootCommandImplementation(IServiceProvider serviceProvider = null)
99+
public RootCommandImplementation(IServiceProvider? serviceProvider = null)
97100
{
98101
this.serviceProvider = serviceProvider;
99102
this.logger = serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger(Program.LoggerCategoryName);
@@ -236,7 +239,7 @@ public IEnumerable<string> GetOutputFilePaths(string[] args)
236239
logger: logger,
237240
context: out var context,
238241
actionWithLoadedAssembly: static (assm, outdir) => GetOutputFilePathOf(assm, outdir)
239-
);
242+
)!;
240243

241244
// wait for the context to be collected
242245
if (context is null)
@@ -263,7 +266,7 @@ private static string GetOutputFilePathOf(Assembly assm, DirectoryInfo outputDir
263266

264267
static string GetOutputFileName(Assembly a)
265268
{
266-
var prefix = a.GetName().Name;
269+
var prefix = a.GetName().Name!;
267270
var targetFramework = a.GetAssemblyMetadataAttributeValue<TargetFrameworkAttribute, string>();
268271

269272
if (targetFramework is null)
@@ -280,7 +283,13 @@ static string GetOutputFileName(Assembly a)
280283
return $"{prefix}-{targetFramework}";
281284
}
282285

283-
static bool TryParseFrameworkName(string name, out FrameworkName frameworkName)
286+
static bool TryParseFrameworkName(
287+
string name,
288+
#if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES
289+
[NotNullWhen(true)]
290+
#endif
291+
out FrameworkName? frameworkName
292+
)
284293
{
285294
frameworkName = default;
286295

@@ -304,7 +313,7 @@ public IEnumerable<FileInfo> GetInputAssemblyFiles(string[] args)
304313
private IEnumerable<FileInfo> GetInputAssemblyFiles(ParseResult parseResult)
305314
{
306315
var input = parseResult.ValueForArgument(argumentInput);
307-
FileInfo inputFile = null;
316+
FileInfo inputFile;
308317

309318
if (input is null)
310319
throw new CommandOperationNotSupportedException("input file or directory not specified");
@@ -330,7 +339,7 @@ private IEnumerable<FileInfo> GetInputAssemblyFiles(ParseResult parseResult)
330339

331340
logger?.LogDebug("input file: '{InputFilePath}'", inputFile.FullName);
332341

333-
IEnumerable<FileInfo> inputAssemblyFiles = null;
342+
IEnumerable<FileInfo> inputAssemblyFiles = Enumerable.Empty<FileInfo>();
334343

335344
if (
336345
string.Equals(".dll", inputFile.Extension, StringComparison.OrdinalIgnoreCase) ||

0 commit comments

Comments
 (0)