Skip to content

Commit e294419

Browse files
lipchevangularsen
andauthored
Fix nullability indicators for the Parse/TryParse overloads (#1446)
(UnitParser, QuantityParser, Quantity) fixes #1445 --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent abbbf5f commit e294419

File tree

131 files changed

+546
-510
lines changed

Some content is hidden

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

131 files changed

+546
-510
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ private void GenerateStaticParseMethods()
517517
/// <example>
518518
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
519519
/// </example>
520-
public static bool TryParse(string? str, out {_quantity.Name} result)
520+
public static bool TryParse([NotNullWhen(true)]string? str, out {_quantity.Name} result)
521521
{{
522522
return TryParse(str, null, out result);
523523
}}
@@ -532,7 +532,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
532532
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
533533
/// </example>
534534
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
535-
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
535+
public static bool TryParse([NotNullWhen(true)]string? str, IFormatProvider? provider, out {_quantity.Name} result)
536536
{{
537537
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
538538
str,
@@ -571,7 +571,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
571571
}}
572572
573573
/// <inheritdoc cref=""TryParseUnit(string,IFormatProvider,out UnitsNet.Units.{_unitEnumName})""/>
574-
public static bool TryParseUnit(string str, out {_unitEnumName} unit)
574+
public static bool TryParseUnit([NotNullWhen(true)]string? str, out {_unitEnumName} unit)
575575
{{
576576
return TryParseUnit(str, null, out unit);
577577
}}
@@ -586,7 +586,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
586586
/// Length.TryParseUnit(""m"", CultureInfo.GetCultureInfo(""en-US""));
587587
/// </example>
588588
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
589-
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
589+
public static bool TryParseUnit([NotNullWhen(true)]string? str, IFormatProvider? provider, out {_unitEnumName} unit)
590590
{{
591591
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
592592
}}

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static bool TryFrom(double value, Enum? unit, [NotNullWhen(true)] out IQu
100100
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
101101
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
102102
/// <returns>The parsed quantity.</returns>
103-
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity)
103+
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, [NotNullWhen(true)] string? quantityString, [NotNullWhen(true)] out IQuantity? quantity)
104104
{
105105
quantity = default(IQuantity);
106106

UnitsNet.Tests/QuantityParserTests.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System.Globalization;
45
using UnitsNet.Tests.CustomQuantities;
6+
using UnitsNet.Units;
57
using Xunit;
68

79
namespace UnitsNet.Tests
@@ -87,5 +89,24 @@ public void TryParse_MappedCustomUnit()
8789
Assert.Equal(1, q.Value);
8890
}
8991

92+
[Fact]
93+
public void TryParse_NullString_Returns_False()
94+
{
95+
QuantityParser quantityParser = UnitsNetSetup.Default.QuantityParser;
96+
97+
var success = quantityParser.TryParse<Mass, MassUnit>(null, null, Mass.From, out Mass _);
98+
99+
Assert.False(success);
100+
}
101+
102+
[Fact]
103+
public void TryParse_WithInvalidValue_Returns_False()
104+
{
105+
QuantityParser quantityParser = UnitsNetSetup.Default.QuantityParser;
106+
107+
var success = quantityParser.TryParse<Mass, MassUnit>("XX kg", CultureInfo.InvariantCulture, Mass.From, out Mass _);
108+
109+
Assert.False(success);
110+
}
90111
}
91112
}

UnitsNet.Tests/UnitParserTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,20 @@ public void Parse_MappedCustomUnit()
137137

138138
Assert.Equal(HowMuchUnit.Some, parsedUnit);
139139
}
140+
141+
[Fact]
142+
public void TryParse_WithNullAbbreviation_ReturnsFalse()
143+
{
144+
UnitParser unitParser = UnitsNetSetup.Default.UnitParser;
145+
Assert.Multiple(() =>
146+
{
147+
var success = unitParser.TryParse(null, out LengthUnit unit);
148+
Assert.False(success);
149+
}, () =>
150+
{
151+
var success = unitParser.TryParse(null, typeof(LengthUnit), out Enum? _);
152+
Assert.False(success);
153+
});
154+
}
140155
}
141156
}

UnitsNet/CustomCode/QuantityParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public bool TryParse<TQuantity, TUnitType>(string? str,
133133
/// <exception cref="ArgumentNullException">The string was null.</exception>
134134
/// <exception cref="FormatException">Failed to parse quantity.</exception>
135135
[SuppressMessage("ReSharper", "UseStringInterpolation")]
136-
public bool TryParse<TQuantity, TUnitType>(string str,
136+
public bool TryParse<TQuantity, TUnitType>(string? str,
137137
IFormatProvider? formatProvider,
138138
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate,
139139
out IQuantity? result)

UnitsNet/CustomCode/UnitParser.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public TUnitType Parse<TUnitType>(string unitAbbreviation, IFormatProvider? form
6262
/// <returns>Unit enum value, such as <see cref="MassUnit.Kilogram" />.</returns>
6363
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
6464
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception>
65-
public Enum Parse(string? unitAbbreviation, Type unitType, IFormatProvider? formatProvider = null)
65+
public Enum Parse(string unitAbbreviation, Type unitType, IFormatProvider? formatProvider = null)
6666
{
6767
if (unitAbbreviation == null) throw new ArgumentNullException(nameof(unitAbbreviation));
6868
unitAbbreviation = unitAbbreviation.Trim();
@@ -138,7 +138,7 @@ internal static string NormalizeUnitString(string unitAbbreviation)
138138
/// <param name="unit">The unit enum value as out result.</param>
139139
/// <typeparam name="TUnitType">Type of unit enum.</typeparam>
140140
/// <returns>True if successful.</returns>
141-
public bool TryParse<TUnitType>(string unitAbbreviation, out TUnitType unit) where TUnitType : struct, Enum
141+
public bool TryParse<TUnitType>([NotNullWhen(true)]string? unitAbbreviation, out TUnitType unit) where TUnitType : struct, Enum
142142
{
143143
return TryParse(unitAbbreviation, null, out unit);
144144
}
@@ -151,7 +151,7 @@ public bool TryParse<TUnitType>(string unitAbbreviation, out TUnitType unit) whe
151151
/// <param name="unit">The unit enum value as out result.</param>
152152
/// <typeparam name="TUnitType">Type of unit enum.</typeparam>
153153
/// <returns>True if successful.</returns>
154-
public bool TryParse<TUnitType>(string? unitAbbreviation, IFormatProvider? formatProvider, out TUnitType unit) where TUnitType : struct, Enum
154+
public bool TryParse<TUnitType>([NotNullWhen(true)]string? unitAbbreviation, IFormatProvider? formatProvider, out TUnitType unit) where TUnitType : struct, Enum
155155
{
156156
unit = default;
157157

@@ -169,7 +169,7 @@ public bool TryParse<TUnitType>(string? unitAbbreviation, IFormatProvider? forma
169169
/// <param name="unitType">Type of unit enum.</param>
170170
/// <param name="unit">The unit enum value as out result.</param>
171171
/// <returns>True if successful.</returns>
172-
public bool TryParse(string unitAbbreviation, Type unitType, [NotNullWhen(true)] out Enum? unit)
172+
public bool TryParse([NotNullWhen(true)] string? unitAbbreviation, Type unitType, [NotNullWhen(true)] out Enum? unit)
173173
{
174174
return TryParse(unitAbbreviation, unitType, null, out unit);
175175
}
@@ -182,7 +182,7 @@ public bool TryParse(string unitAbbreviation, Type unitType, [NotNullWhen(true)]
182182
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
183183
/// <param name="unit">The unit enum value as out result.</param>
184184
/// <returns>True if successful.</returns>
185-
public bool TryParse(string? unitAbbreviation, Type unitType, IFormatProvider? formatProvider, [NotNullWhen(true)] out Enum? unit)
185+
public bool TryParse([NotNullWhen(true)] string? unitAbbreviation, Type unitType, IFormatProvider? formatProvider, [NotNullWhen(true)] out Enum? unit)
186186
{
187187
if (unitAbbreviation == null)
188188
{

UnitsNet/GeneratedCode/Quantities/AbsorbedDoseOfIonizingRadiation.g.cs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)