Skip to content

Commit e0b6a2c

Browse files
authored
refactor: enable nullable ref type checking (#425)
1 parent db2b121 commit e0b6a2c

31 files changed

+98
-64
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<WarningsNotAsErrors>$(WarningsNotAsErrors);1591</WarningsNotAsErrors>
3333
<LangVersion>9.0</LangVersion>
3434
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
35-
<Nullable>annotations</Nullable>
35+
<Nullable>enable</Nullable>
3636
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)src\StrongName.snk</AssemblyOriginatorKeyFile>
3737
<SignAssembly>true</SignAssembly>
3838

src/CommandLineUtils/Attributes/AllowedValuesAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ or StringComparison.InvariantCultureIgnoreCase
6666
}
6767

6868
/// <inheritdoc />
69-
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
69+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
7070
{
7171
if (value is string str && _allowedValues.Any(t => str.Equals(t, Comparer)))
7272
{
7373
return ValidationResult.Success;
7474
}
7575

76-
return new ValidationResult(FormatErrorMessage(value as string));
76+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
7777
}
7878
}
7979
}

src/CommandLineUtils/Attributes/FilePathExistsAttributeBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ internal FilePathExistsAttributeBase(FilePathType filePathType)
2727
}
2828

2929
/// <inheritdoc />
30-
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
30+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
3131
{
3232
if (value is not string path || path.Length == 0 || path.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
3333
{
34-
return new ValidationResult(FormatErrorMessage(value as string));
34+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
3535
}
3636

3737
if (!Path.IsPathRooted(path)
@@ -50,7 +50,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
5050
return ValidationResult.Success;
5151
}
5252

53-
return new ValidationResult(FormatErrorMessage(value as string));
53+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
5454
}
5555

5656
private static string GetDefaultErrorMessage(FilePathType filePathType)

src/CommandLineUtils/Attributes/FilePathNotExistsAttributeBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ internal FilePathNotExistsAttributeBase(FilePathType filePathType)
2727
}
2828

2929
/// <inheritdoc />
30-
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
30+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
3131
{
3232
if (value is not string path || path.Length == 0 || path.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
3333
{
34-
return new ValidationResult(FormatErrorMessage(value as string));
34+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
3535
}
3636

3737
if (!Path.IsPathRooted(path)
@@ -55,7 +55,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
5555
return ValidationResult.Success;
5656
}
5757

58-
return new ValidationResult(FormatErrorMessage(value as string));
58+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
5959
}
6060

6161
private static string GetDefaultErrorMessage(FilePathType filePathType)

src/CommandLineUtils/Attributes/LegalFilePathAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public LegalFilePathAttribute()
2323
}
2424

2525
/// <inheritdoc />
26-
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
26+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
2727
{
2828
if (value is string path)
2929
{
@@ -37,7 +37,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
3737
}
3838
}
3939

40-
return new ValidationResult(FormatErrorMessage(value as string));
40+
return new ValidationResult(FormatErrorMessage(value?.ToString() ?? string.Empty));
4141
}
4242
}
4343
}

src/CommandLineUtils/CommandArgument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public IReadOnlyList<string?> Values
7878
/// <summary>
7979
/// Defines the underlying type of the argument for the help-text-generator
8080
/// </summary>
81-
internal Type UnderlyingType { get; set; }
81+
internal Type? UnderlyingType { get; set; }
8282

8383
/// <summary>
8484
/// Try to add a value to this argument.

src/CommandLineUtils/CommandArgument{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public IReadOnlyList<T> ParsedValues
5252
((IInternalCommandParamOfT)this).Parse(CultureInfo.CurrentCulture);
5353
}
5454

55-
if (_parsedValues.Count == 0 && _hasDefaultValue)
55+
if (_parsedValues.Count == 0 && _hasDefaultValue && DefaultValue != null)
5656
{
57-
return new[] { DefaultValue };
57+
return new T[] { DefaultValue };
5858
}
5959

6060
return _parsedValues;
@@ -85,7 +85,7 @@ void IInternalCommandParamOfT.Parse(CultureInfo culture)
8585
}
8686
}
8787

88-
void SetBaseDefaultValue(T value)
88+
void SetBaseDefaultValue(T? value)
8989
{
9090
if (!ReflectionHelper.IsSpecialValueTupleType(typeof(T), out _))
9191
{

src/CommandLineUtils/CommandLineApplication.Validation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Func<ValidationResult, int> ValidationErrorHandler
3232
/// Validates arguments and options.
3333
/// </summary>
3434
/// <returns>The first validation result that is not <see cref="ValidationResult.Success"/> if there is an error.</returns>
35-
public ValidationResult GetValidationResult()
35+
public ValidationResult? GetValidationResult()
3636
{
3737
if (Parent != null)
3838
{

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ public async Task<int> ExecuteAsync(string[] args, CancellationToken cancellatio
863863
}
864864

865865
var validationResult = command.GetValidationResult();
866-
if (validationResult != ValidationResult.Success)
866+
if (validationResult != null)
867867
{
868868
return command.ValidationErrorHandler(validationResult);
869869
}

src/CommandLineUtils/CommandLineApplication{T}.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using McMaster.Extensions.CommandLineUtils.Abstractions;
67
using McMaster.Extensions.CommandLineUtils.Conventions;
78
using McMaster.Extensions.CommandLineUtils.HelpText;
@@ -15,6 +16,7 @@ namespace McMaster.Extensions.CommandLineUtils
1516
public class CommandLineApplication<TModel> : CommandLineApplication, IModelAccessor
1617
where TModel : class
1718
{
19+
[AllowNull] // this is initialized in common method called in all constructors
1820
private Lazy<TModel> _lazy;
1921
private Func<TModel> _modelFactory = DefaultModelFactory;
2022

0 commit comments

Comments
 (0)