From 95dd7ee0433bb978a4c7c4e83950217e98d37999 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Sun, 20 Oct 2024 20:43:22 +0100 Subject: [PATCH] Ignore obsolete usages in the enum extensions (#111) --- .../SourceGenerationHelper.cs | 11 +- .../Enums.cs | 1 + .../Enums.cs | 3 + .../InterceptorTests.cs | 2 + .../EnumGeneratorTests.cs | 92 ++++ .../InterceptorTests.cs | 88 ++++ ...ExtensionsForFlagsEnum_Params.verified.txt | 6 +- ...numExtensionsInChildNamespace.verified.txt | 6 +- ...umExtensionsInGlobalNamespace.verified.txt | 8 +- ...teEnumExtensionsInNestedClass.verified.txt | 6 +- ...eEnumExtensionsWithCustomName.verified.txt | 6 +- ...EnumExtensionsWithCustomNames.verified.txt | 6 +- ...ExtensionsWithCustomNamespace.verified.txt | 6 +- ...onsWithCustomNamespaceAndName.verified.txt | 6 +- ...ExtensionsWithSameDisplayName.verified.txt | 6 +- ...lEnumExtensionsWithCustomName.verified.txt | 6 +- ...ExtensionsWithCustomNamespace.verified.txt | 6 +- ...onsWithCustomNamespaceAndName.verified.txt | 6 +- ...ts.CanGenerateForExternalEnum.verified.txt | 6 +- ...nGenerateForExternalFlagsEnum.verified.txt | 6 +- ...erateForMultipleExternalEnums.verified.txt | 10 +- ...mespaceAndClassNameAreTheSame.verified.txt | 6 +- ...rObsoleteEnums_CS0612_Issue97.verified.txt | 492 ++++++++++++++++++ ...rObsoleteEnums_CS0618_Issue97.verified.txt | 492 ++++++++++++++++++ ...bsoleteMembers_CS0612_Issue97.verified.txt | 492 ++++++++++++++++++ ...bsoleteMembers_CS0618_Issue97.verified.txt | 492 ++++++++++++++++++ ...QuotesAndSlashesInDescription.verified.txt | 6 +- ...InterceptExternalEnumToString.verified.txt | 9 + ...ptorTests.CanInterceptHasFlag.verified.txt | 9 + ...nterceptMultipleEnumsToString.verified.txt | 18 + ...torTests.CanInterceptToString.verified.txt | 9 + ...InterceptToStringWhenCsharp11.verified.txt | 9 + ...nterceptableAttributeToString.verified.txt | 9 + ...InterceptEnumToStringInOldSDK.verified.txt | 6 +- ...ceptableAttributeEnumInOldSDK.verified.txt | 6 +- ...fObsoleteEnums_CS0612_Issue97.verified.txt | 492 ++++++++++++++++++ ...fObsoleteEnums_CS0618_Issue97.verified.txt | 492 ++++++++++++++++++ ...tEnumMarkedAsNotInterceptable.verified.txt | 13 + ...lEnumMarkedAsNotInterceptable.verified.txt | 13 + ...InterceptToStringWhenDisabled.verified.txt | 4 + ...nterceptToStringWhenOldCsharp.verified.txt | 4 + ...tTests.GeneratesEnumCorrectly.verified.txt | 4 + ...s.GeneratesFlagsEnumCorrectly.verified.txt | 4 + 43 files changed, 3353 insertions(+), 21 deletions(-) create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt create mode 100644 tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt diff --git a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs index 5d41efd..6bcf8ea 100644 --- a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs +++ b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs @@ -148,6 +148,8 @@ namespace ").Append(enumToGenerate.Namespace).Append(@" var fullyQualifiedName = $"global::{enumToGenerate.FullyQualifiedName}"; sb.Append(@" +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -887,7 +889,9 @@ public static string[] GetNames() sb.Append(@" }; } - }"); + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums"); if (!string.IsNullOrEmpty(enumToGenerate.Namespace)) { sb.Append(@" @@ -919,6 +923,9 @@ public InterceptsLocationAttribute(int version, string data) } } } + + #pragma warning disable CS0612 // Ignore usages of obsolete members or enums + #pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -970,6 +977,8 @@ static file class EnumInterceptors $$""" } } + #pragma warning restore CS0612 // Ignore usages of obsolete members or enums + #pragma warning restore CS0618 // Ignore usages of obsolete members or enums """); return sb.ToString(); diff --git a/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs b/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs index 2c98ac4..078cf8c 100644 --- a/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs +++ b/tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs @@ -133,6 +133,7 @@ public enum StringTesting { [System.ComponentModel.Description("Quotes \"")] Quotes, [System.ComponentModel.Description(@"Literal Quotes """)] LiteralQuotes, + [Obsolete] [System.ComponentModel.Description("Backslash \\")] Backslash, [System.ComponentModel.Description(@"LiteralBackslash \")] BackslashLiteral, [System.ComponentModel.Description("Line\nBreak")] LineBreak, diff --git a/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/Enums.cs b/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/Enums.cs index 88fb764..0117192 100644 --- a/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/Enums.cs +++ b/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/Enums.cs @@ -82,6 +82,9 @@ public enum StringTesting [System.ComponentModel.Description(@"LiteralBackslash \")] BackslashLiteral, + + [System.ComponentModel.Description("Line\nBreak")] + LineBreak, } [EnumExtensions(IsInterceptable = false)] diff --git a/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/InterceptorTests.cs b/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/InterceptorTests.cs index a1ef531..09598f4 100644 --- a/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/InterceptorTests.cs +++ b/tests/NetEscapades.EnumGenerators.Interceptors.IntegrationTests/InterceptorTests.cs @@ -60,8 +60,10 @@ void AssertValue(EnumWithDisplayNameInNamespace value) #endif public void CallingToStringIsIntercepted_StringTesting() { +#pragma warning disable CS0612 var result1 = StringTesting.Backslash.ToString(); var result2 = StringTesting.Backslash.ToStringFast(); +#pragma warning restore CS0612 Assert.Equal(result1, result2); } diff --git a/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs b/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs index eecbfbf..1640734 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs @@ -426,4 +426,96 @@ public Task CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName() Assert.Empty(diagnostics); return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); } + + [Fact] + public Task DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [EnumExtensions] + public enum MyEnum + { + First, + [Obsolete] + Second, + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } + + [Fact] + public Task DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [EnumExtensions] + public enum MyEnum + { + First, + [Obsolete("This is obsolete")] + Second, + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } + + [Fact] + public Task DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [Obsolete] + [EnumExtensions] + public enum MyEnum + { + First, + Second, + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } + + [Fact] + public Task DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [Obsolete("This is obsolete", false)] + [EnumExtensions] + public enum MyEnum + { + First, + Second, + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs b/tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs index fc185b0..8a978e3 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs +++ b/tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs @@ -447,6 +447,94 @@ public void MyTest() return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); } + [Fact] + public Task DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [EnumExtensions] + public enum MyEnum + { + First, + [Obsolete] + Second, + } + + [EnumExtensions] + public enum OtherEnum + { + First, + Second, + } + + internal class InnerClass + { + public void MyTest() + { + #pragma warning disable CS0612 + #pragma warning disable CS0618 + var var1 = OtherEnum.Second.ToString(); + var var2 = MyEnum.Second.ToString(); + #pragma warning restore CS0612 + #pragma warning restore CS0618 + } + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } + + + [Fact] + public Task DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97() + { + const string input = + """ + using System; + using NetEscapades.EnumGenerators; + + [EnumExtensions] + public enum MyEnum + { + First, + [Obsolete("This is obsolete")] + Second, + } + + [EnumExtensions] + [Obsolete("This is obsolete")] + public enum OtherEnum + { + First, + Second, + } + + internal class InnerClass + { + public void MyTest() + { + #pragma warning disable CS0612 + #pragma warning disable CS0618 + var var1 = OtherEnum.Second.ToString(); + var var2 = MyEnum.Second.ToString(); + #pragma warning restore CS0612 + #pragma warning restore CS0618 + } + } + + """; + var (diagnostics, output) = TestHelpers.GetGeneratedOutput(new(input)); + + Assert.Empty(diagnostics); + return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots"); + } + [Fact] public Task DoesNotInterceptToStringWhenOldCsharp() { diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt index 6a72ee4..35ab692 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsForFlagsEnum_Params.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -518,4 +520,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt index dd3371b..97cebb9 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInChildNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt index fb4f651..0762da9 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -12,6 +12,8 @@ using System; #endif +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -485,4 +487,6 @@ using System; nameof(global::MyEnum.Second), }; } - } \ No newline at end of file + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt index ef5faa9..3eaf46d 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt index 2a08a65..6f7cfd5 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt index b748932..755168b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -615,4 +617,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt index c88a1e5..3d1edfc 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace A.B { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace A.B }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt index 54bcbb6..49e1993 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace A.B { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace A.B }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt index 769ec54..9f9e259 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -601,4 +603,6 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt index b17d379..ec6a59f 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -506,4 +508,6 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt index 653a47e..3c2aabe 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace A.B { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -506,4 +508,6 @@ namespace A.B }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt index d72f37a..567ba57 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace A.B { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -506,4 +508,6 @@ namespace A.B }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt index d35bdb9..9c0dc80 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -560,4 +562,6 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt index 0c2137f..ad0016f 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace System.IO { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -572,4 +574,6 @@ namespace System.IO }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt index 1d52e21..ad30bdf 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -15,6 +15,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -741,6 +743,8 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -759,6 +763,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -1251,5 +1257,7 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt index cd1cb6a..94947af 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace Foo { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -470,4 +472,6 @@ namespace Foo }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt new file mode 100644 index 0000000..0762da9 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value) + => value switch + { + global::MyEnum.First => nameof(global::MyEnum.First), + global::MyEnum.Second => nameof(global::MyEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::MyEnum value) + => value switch + { + global::MyEnum.First => true, + global::MyEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::MyEnum.First): + value = global::MyEnum.First; + return true; + case nameof(global::MyEnum.Second): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyEnum[] GetValues() + { + return new[] + { + global::MyEnum.First, + global::MyEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyEnum.First), + nameof(global::MyEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt new file mode 100644 index 0000000..0762da9 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value) + => value switch + { + global::MyEnum.First => nameof(global::MyEnum.First), + global::MyEnum.Second => nameof(global::MyEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::MyEnum value) + => value switch + { + global::MyEnum.First => true, + global::MyEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::MyEnum.First): + value = global::MyEnum.First; + return true; + case nameof(global::MyEnum.Second): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyEnum[] GetValues() + { + return new[] + { + global::MyEnum.First, + global::MyEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyEnum.First), + nameof(global::MyEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt new file mode 100644 index 0000000..0762da9 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value) + => value switch + { + global::MyEnum.First => nameof(global::MyEnum.First), + global::MyEnum.Second => nameof(global::MyEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::MyEnum value) + => value switch + { + global::MyEnum.First => true, + global::MyEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::MyEnum.First): + value = global::MyEnum.First; + return true; + case nameof(global::MyEnum.Second): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyEnum[] GetValues() + { + return new[] + { + global::MyEnum.First, + global::MyEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyEnum.First), + nameof(global::MyEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt new file mode 100644 index 0000000..0762da9 --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class MyEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::MyEnum value) + => value switch + { + global::MyEnum.First => nameof(global::MyEnum.First), + global::MyEnum.Second => nameof(global::MyEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::MyEnum value) + => value switch + { + global::MyEnum.First => true, + global::MyEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::MyEnum.First) => true, + nameof(global::MyEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::MyEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.First; + return true; + case string s when s.Equals(nameof(global::MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::MyEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::MyEnum.First): + value = global::MyEnum.First; + return true; + case nameof(global::MyEnum.Second): + value = global::MyEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::MyEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::MyEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::MyEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::MyEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::MyEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::MyEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::MyEnum[] GetValues() + { + return new[] + { + global::MyEnum.First, + global::MyEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::MyEnum.First), + nameof(global::MyEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt index 6ff23a6..bb01307 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator // @@ -14,6 +14,8 @@ using System; namespace Test { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -675,4 +677,6 @@ namespace Test }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt index e4db1d9..9dd6d08 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptExternalEnumToString.verified.txt @@ -136,6 +136,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -682,6 +684,8 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -710,6 +714,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -724,5 +731,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt index b813b69..ec900eb 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptHasFlag.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -640,6 +642,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -668,6 +672,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -681,5 +688,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt index bebb0f7..67c1374 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptMultipleEnumsToString.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -628,6 +630,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -646,6 +650,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -1138,6 +1144,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -1166,6 +1174,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -1176,6 +1187,8 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums //------------------------------------------------------------------------------ @@ -1204,6 +1217,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -1215,5 +1231,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt index 602b5e3..8ac99b6 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToString.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -610,6 +612,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -638,6 +642,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -651,5 +658,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt index 602b5e3..8ac99b6 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptToStringWhenCsharp11.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -610,6 +612,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -638,6 +642,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -651,5 +658,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt index 1dd2474..0d5bf40 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanInterceptUsingInterceptableAttributeToString.verified.txt @@ -136,6 +136,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -682,6 +684,8 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -710,6 +714,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -724,5 +731,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumToStringInOldSDK.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumToStringInOldSDK.verified.txt index 5bd0efd..4112770 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumToStringInOldSDK.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptEnumToStringInOldSDK.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -610,5 +612,7 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptInterceptableAttributeEnumInOldSDK.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptInterceptableAttributeEnumInOldSDK.verified.txt index e9b5c1e..7d6bda7 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptInterceptableAttributeEnumInOldSDK.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.CanNotInterceptInterceptableAttributeEnumInOldSDK.verified.txt @@ -1,4 +1,4 @@ -[ +[ //------------------------------------------------------------------------------ // // This code was generated by the NetEscapades.EnumGenerators source generator @@ -136,6 +136,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -682,5 +684,7 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt new file mode 100644 index 0000000..5417a4b --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class OtherEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::OtherEnum value) + => value switch + { + global::OtherEnum.First => nameof(global::OtherEnum.First), + global::OtherEnum.Second => nameof(global::OtherEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::OtherEnum value) + => value switch + { + global::OtherEnum.First => true, + global::OtherEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::OtherEnum.First) => true, + nameof(global::OtherEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::OtherEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::OtherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::OtherEnum.First; + return true; + case string s when s.Equals(nameof(global::OtherEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::OtherEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::OtherEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::OtherEnum.First): + value = global::OtherEnum.First; + return true; + case nameof(global::OtherEnum.Second): + value = global::OtherEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::OtherEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::OtherEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::OtherEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::OtherEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::OtherEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::OtherEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::OtherEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::OtherEnum[] GetValues() + { + return new[] + { + global::OtherEnum.First, + global::OtherEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::OtherEnum.First), + nameof(global::OtherEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt new file mode 100644 index 0000000..5417a4b --- /dev/null +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97.verified.txt @@ -0,0 +1,492 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the NetEscapades.EnumGenerators source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#nullable enable +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 +using System; +#endif + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums + /// + /// Extension methods for + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")] + public static partial class OtherEnumExtensions + { + /// + /// The number of members in the enum. + /// This is a non-distinct count of defined names. + /// + public const int Length = 2; + + /// + /// Returns the string representation of the value. + /// If the attribute is decorated with a [Display] attribute, then + /// uses the provided value. Otherwise uses the name of the member, equivalent to + /// calling ToString() on . + /// + /// The value to retrieve the string value for + /// The string representation of the value + public static string ToStringFast(this global::OtherEnum value) + => value switch + { + global::OtherEnum.First => nameof(global::OtherEnum.First), + global::OtherEnum.Second => nameof(global::OtherEnum.Second), + _ => value.ToString(), + }; + + /// + /// Returns a boolean telling whether the given enum value exists in the enumeration. + /// + /// The value to check if it's defined + /// true if the value exists in the enumeration, false otherwise + public static bool IsDefined(global::OtherEnum value) + => value switch + { + global::OtherEnum.First => true, + global::OtherEnum.Second => true, + _ => false, + }; + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration. + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or if a member decorated with a [Display] attribute + /// with the required name exists. + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(string name, bool allowMatchingMetadataAttribute) + { + return name switch + { + nameof(global::OtherEnum.First) => true, + nameof(global::OtherEnum.Second) => true, + _ => false, + }; + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration + /// + /// The name to check if it's defined + /// true if a member with the name exists in the enumeration, false otherwise + public static bool IsDefined(in ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false); + + /// + /// Returns a boolean telling whether an enum with the given name exists in the enumeration, + /// or optionally if a member decorated with a [Display] attribute + /// with the required name exists. + /// Slower then the overload, but doesn't allocate memory./> + /// + /// The name to check if it's defined + /// If true, considers the value of metadata attributes,otherwise ignores them + /// true if a member with the name exists in the enumeration, or a member is decorated + /// with a [Display] attribute with the name, false otherwise + public static bool IsDefined(in ReadOnlySpan name, bool allowMatchingMetadataAttribute) + { + return name switch + { + ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.Ordinal) => true, + ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.Ordinal) => true, + _ => false, + }; + } +#endif + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name); + +#if NETCOREAPP3_0_OR_GREATER + [System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static global::OtherEnum ThrowValueNotFound(string? name) + => throw new System.ArgumentException($"Requested value '{name}' was not found."); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The string representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute) + : TryParseWithCase(name, out value, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case string s when s.Equals(nameof(global::OtherEnum.First), global::System.StringComparison.OrdinalIgnoreCase): + value = global::OtherEnum.First; + return true; + case string s when s.Equals(nameof(global::OtherEnum.Second), global::System.StringComparison.OrdinalIgnoreCase): + value = global::OtherEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::OtherEnum)val; + return true; + default: + value = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + string? name, + out global::OtherEnum value, + bool allowMatchingMetadataAttribute) + { + + switch (name) + { + case nameof(global::OtherEnum.First): + value = global::OtherEnum.First; + return true; + case nameof(global::OtherEnum.Second): + value = global::OtherEnum.Second; + return true; + case string s when int.TryParse(name, out var val): + value = (global::OtherEnum)val; + return true; + default: + value = default; + return false; + } + } + +#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0 + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name) + => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase) + => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the string representation of the name or numeric value of + /// an to the equivalent instance. + /// + /// The case-sensitive string representation of the enumeration name or underlying value to convert + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// An object of type whose + /// value is represented by + public static global::OtherEnum Parse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString()); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum value) + => TryParse(name, out value, false, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum value, + bool ignoreCase) + => TryParse(name, out value, ignoreCase, false); + + /// + /// Converts the span representation of the name or numeric value of + /// an to the equivalent instance. + /// The return value indicates whether the conversion succeeded. + /// + /// The span representation of the enumeration name or underlying value to convert + /// When this method returns, contains an object of type + /// whose + /// value is represented by if the parse operation succeeds. + /// If the parse operation fails, contains the default value of the underlying type + /// of . This parameter is passed uninitialized. + /// true to read value in case insensitive mode; false to read value in case sensitive mode. + /// If true, considers the value included in metadata attributes such as + /// [Display] attribute when parsing, otherwise only considers the member names. + /// true if the value parameter was converted successfully; otherwise, false. + public static bool TryParse( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool ignoreCase, + bool allowMatchingMetadataAttribute) + => ignoreCase + ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute) + : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute); + + private static bool TryParseIgnoreCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::OtherEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.OrdinalIgnoreCase): + result = global::OtherEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::OtherEnum)numericResult; + return true; + default: + result = default; + return false; + } + } + + private static bool TryParseWithCase( +#if NETCOREAPP3_0_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + in ReadOnlySpan name, + out global::OtherEnum result, + bool allowMatchingMetadataAttribute) + { + switch (name) + { + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.First).AsSpan(), global::System.StringComparison.Ordinal): + result = global::OtherEnum.First; + return true; + case ReadOnlySpan current when current.Equals(nameof(global::OtherEnum.Second).AsSpan(), global::System.StringComparison.Ordinal): + result = global::OtherEnum.Second; + return true; + case ReadOnlySpan current when int.TryParse(name, out var numericResult): + result = (global::OtherEnum)numericResult; + return true; + default: + result = default; + return false; + } + } +#endif + + /// + /// Retrieves an array of the values of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the values defined in + public static global::OtherEnum[] GetValues() + { + return new[] + { + global::OtherEnum.First, + global::OtherEnum.Second, + }; + } + + /// + /// Retrieves an array of the names of the members defined in + /// . + /// Note that this returns a new array with every invocation, so + /// should be cached if appropriate. + /// + /// An array of the names of the members defined in + public static string[] GetNames() + { + return new[] + { + nameof(global::OtherEnum.First), + nameof(global::OtherEnum.Second), + }; + } + } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt index 17fa544..45d0811 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptEnumMarkedAsNotInterceptable.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -628,6 +630,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -646,6 +650,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -1150,6 +1156,8 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -1178,6 +1186,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -1193,5 +1204,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt index 279eb12..d842976 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptExternalEnumMarkedAsNotInterceptable.verified.txt @@ -136,6 +136,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -682,6 +684,8 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -700,6 +704,8 @@ using System; namespace System { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -1192,6 +1198,8 @@ namespace System }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } //------------------------------------------------------------------------------ @@ -1220,6 +1228,9 @@ namespace System.Runtime.CompilerServices } } } + +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums namespace NetEscapades.EnumGenerators { static file class EnumInterceptors @@ -1234,5 +1245,7 @@ namespace NetEscapades.EnumGenerators } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt index 5bd0efd..736ce6c 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenDisabled.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -610,5 +612,7 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt index 5bd0efd..736ce6c 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/InterceptorTests.DoesNotInterceptToStringWhenOldCsharp.verified.txt @@ -136,6 +136,8 @@ using System; namespace MyTestNameSpace { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -610,5 +612,7 @@ namespace MyTestNameSpace }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } ] \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly.verified.txt index 3514c48..44e745d 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesEnumCorrectly.verified.txt @@ -14,6 +14,8 @@ using System; namespace Something.Blah { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -488,4 +490,6 @@ namespace Something.Blah }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly.verified.txt index c368bc4..8d7c92b 100644 --- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly.verified.txt +++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFlagsEnumCorrectly.verified.txt @@ -14,6 +14,8 @@ using System; namespace Something.Blah { +#pragma warning disable CS0612 // Ignore usages of obsolete members or enums +#pragma warning disable CS0618 // Ignore usages of obsolete members or enums /// /// Extension methods for /// @@ -500,4 +502,6 @@ namespace Something.Blah }; } } +#pragma warning restore CS0612 // Ignore usages of obsolete members or enums +#pragma warning restore CS0618 // Ignore usages of obsolete members or enums } \ No newline at end of file