Skip to content

Commit 1095172

Browse files
committed
NotNullIfNotNull is not necessary when key member is struct
1 parent 40fb2e0 commit 1095172

8 files changed

Lines changed: 19 additions & 15 deletions

samples/Basic.Samples/SmartEnums/SmartEnumDemos.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ private static void DemoForSmartEnum(ILogger logger)
3636
var productType = ProductType.Get("Groceries");
3737
logger.Information("Product type: {Type}", productType);
3838

39+
var productGroup = ProductGroup.Get(1);
40+
productGroup = (ProductGroup)1;
41+
3942
try
4043
{
4144
// Throws if the provided value is invalid/unknown

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,16 @@ private void GenerateConversionFromKeyType(KeyMemberState keyProperty)
917917
/// ").Append(_state.Settings.ConversionFromKeyMemberType == ConversionOperatorsGeneration.Implicit ? "Implicit" : "Explicit").Append(" conversion from the type ").AppendTypeForXmlComment(keyProperty).Append(@".
918918
/// </summary>
919919
/// <param name=""").AppendArgumentName(keyProperty.ArgumentName).Append(@""">Value to covert.</param>
920-
/// <returns>An instance of ").AppendTypeForXmlComment(_state).Append(@" if the <paramref name=""").AppendArgumentName(keyProperty.ArgumentName).Append(@"""/> is a known item.</returns>
921-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""").AppendArgumentName(keyProperty.ArgumentName).Append(@""")]
922-
public static ").AppendConversionOperator(_state.Settings.ConversionFromKeyMemberType).Append(" operator ").AppendTypeFullyQualifiedNullAnnotated(_state).Append("(").AppendTypeFullyQualifiedNullAnnotated(keyProperty).Append(" ").AppendEscaped(keyProperty.ArgumentName).Append(@")
920+
/// <returns>An instance of ").AppendTypeForXmlComment(_state).Append(@" if the <paramref name=""").AppendArgumentName(keyProperty.ArgumentName).Append(@"""/> is a known item.</returns>");
921+
922+
if (keyProperty.IsReferenceType)
923+
{
924+
_sb.Append(@"
925+
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""").AppendArgumentName(keyProperty.ArgumentName).Append(@""")]");
926+
}
927+
928+
_sb.Append(@"
929+
public static ").AppendConversionOperator(_state.Settings.ConversionFromKeyMemberType).Append(" operator ").AppendTypeFullyQualified(_state, nullable: keyProperty.IsReferenceType).Append("(").AppendTypeFullyQualifiedNullAnnotated(keyProperty).Append(" ").AppendEscaped(keyProperty.ArgumentName).Append(@")
923930
{
924931
return ").AppendTypeFullyQualified(_state).Append(".").Append(Constants.Methods.GET).Append("(").AppendEscaped(keyProperty.ArgumentName).Append(@");
925932
}");

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_generate_int_based_class_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ namespace Thinktecture.Tests
159159
/// </summary>
160160
/// <param name="key">Value to covert.</param>
161161
/// <returns>An instance of <see cref="TestEnum"/> if the <paramref name="key"/> is a known item.</returns>
162-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("key")]
163-
public static explicit operator global::Thinktecture.Tests.TestEnum?(int @key)
162+
public static explicit operator global::Thinktecture.Tests.TestEnum(int @key)
164163
{
165164
return global::Thinktecture.Tests.TestEnum.Get(@key);
166165
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_generate_int_based_class_with_ComparisonOperators_DefaultWithKeyTypeOverloads_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ namespace Thinktecture.Tests
159159
/// </summary>
160160
/// <param name="key">Value to covert.</param>
161161
/// <returns>An instance of <see cref="TestEnum"/> if the <paramref name="key"/> is a known item.</returns>
162-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("key")]
163-
public static explicit operator global::Thinktecture.Tests.TestEnum?(int @key)
162+
public static explicit operator global::Thinktecture.Tests.TestEnum(int @key)
164163
{
165164
return global::Thinktecture.Tests.TestEnum.Get(@key);
166165
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_generate_int_based_class_with_ObjectFactoryAttribute_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ namespace Thinktecture.Tests
159159
/// </summary>
160160
/// <param name="key">Value to covert.</param>
161161
/// <returns>An instance of <see cref="TestEnum"/> if the <paramref name="key"/> is a known item.</returns>
162-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("key")]
163-
public static explicit operator global::Thinktecture.Tests.TestEnum?(int @key)
162+
public static explicit operator global::Thinktecture.Tests.TestEnum(int @key)
164163
{
165164
return global::Thinktecture.Tests.TestEnum.Get(@key);
166165
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_generate_int_based_class_with_ObjectFactoryAttribute_and_UseForSerialization_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ namespace Thinktecture.Tests
159159
/// </summary>
160160
/// <param name="key">Value to covert.</param>
161161
/// <returns>An instance of <see cref="TestEnum"/> if the <paramref name="key"/> is a known item.</returns>
162-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("key")]
163-
public static explicit operator global::Thinktecture.Tests.TestEnum?(int @key)
162+
public static explicit operator global::Thinktecture.Tests.TestEnum(int @key)
164163
{
165164
return global::Thinktecture.Tests.TestEnum.Get(@key);
166165
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_handle_special_chars_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ namespace Thinktecture.Tests
158158
/// </summary>
159159
/// <param name="_1Key">Value to covert.</param>
160160
/// <returns>An instance of <see cref="_1TestEnum"/> if the <paramref name="_1Key"/> is a known item.</returns>
161-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("_1Key")]
162-
public static explicit operator global::Thinktecture.Tests._1TestEnum?(int @_1Key)
161+
public static explicit operator global::Thinktecture.Tests._1TestEnum(int @_1Key)
163162
{
164163
return global::Thinktecture.Tests._1TestEnum.Get(@_1Key);
165164
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_use_custom_SwitchMapStateParameterName_0.verified.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ namespace Thinktecture.Tests
158158
/// </summary>
159159
/// <param name="key">Value to covert.</param>
160160
/// <returns>An instance of <see cref="TestEnum"/> if the <paramref name="key"/> is a known item.</returns>
161-
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("key")]
162-
public static explicit operator global::Thinktecture.Tests.TestEnum?(int @key)
161+
public static explicit operator global::Thinktecture.Tests.TestEnum(int @key)
163162
{
164163
return global::Thinktecture.Tests.TestEnum.Get(@key);
165164
}

0 commit comments

Comments
 (0)