Skip to content

Commit 8439485

Browse files
authored
Add pragma to suppress unreachable pattern warnings (#143)
* Add pragma to suppress unreachable pattern warnings This commit introduces the `#pragma warning disable CS8510` directive to suppress warnings about unreachable patterns in switch expressions. The directive has been added to `SourceGenerationHelper.cs` and various test files to prevent unnecessary warnings during code generation. The corresponding `#pragma warning restore CS8510` directive has also been included to restore the warning state after the relevant code sections. * Remove `#pragma warning disable CS8510` directive across multiple files and enhance enum generation functionality. Added methods for parsing and checking defined enum values, along with new extension methods. Introduced tests to verify correct generation of enums with repeated values and flags. * Enhance `SourceGenerationHelper` to track constant values of enum members using a new `HashSet<object>`. Update logic for appending enum member values to improve clarity, particularly regarding display names. Introduce two new enums, `EnumWithRepeatedValues` and `EnumWithRepeatedValuesWithDisplayNames`, to handle repeated values and display names. Update tests to reflect changes in enum definitions, including the removal of the `Third` member check due to its repeated value.
1 parent c2bb4e0 commit 8439485

File tree

4 files changed

+628
-21
lines changed

4 files changed

+628
-21
lines changed

src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public class EnumExtensionsAttribute<T> : System.Attribute
103103

104104
public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate)
105105
{
106+
var constantValues = new HashSet<object>();
107+
106108
var sb = new StringBuilder();
107109
sb.AppendLine(Header);
108110
if (!string.IsNullOrEmpty(enumToGenerate.Namespace))
@@ -187,15 +189,19 @@ public static string ToStringFast(this
187189
""");
188190

189191
var hasDisplayNames = false;
192+
constantValues.Clear();
190193
foreach (var member in enumToGenerate.Names)
191194
{
192-
hasDisplayNames |= member.Value.DisplayName is not null;
193-
sb.Append(
194-
"""
195+
if (constantValues.Add(member.Value.ConstantValue))
196+
{
197+
hasDisplayNames |= member.Value.DisplayName is not null;
198+
sb.Append(
199+
"""
195200
196-
197-
""").Append(fullyQualifiedName).Append('.').Append(member.Key)
198-
.Append(" => nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),");
201+
202+
""").Append(fullyQualifiedName).Append('.').Append(member.Key)
203+
.Append(" => nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),");
204+
}
199205
}
200206

201207
if (enumToGenerate.HasFlags)
@@ -236,22 +242,26 @@ private static string ToStringFastWithMetadata(this
236242
value switch
237243
{
238244
""");
245+
constantValues.Clear();
239246
foreach (var member in enumToGenerate.Names)
240247
{
241-
sb.Append(
242-
"""
248+
if (constantValues.Add(member.Value.ConstantValue))
249+
{
250+
sb.Append(
251+
"""
243252
244253
245254
""").Append(fullyQualifiedName).Append('.').Append(member.Key)
246-
.Append(" => ");
255+
.Append(" => ");
247256

248-
if (member.Value.DisplayName is { } dn)
249-
{
250-
sb.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(',');
251-
}
252-
else
253-
{
254-
sb.Append("nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),");
257+
if (member.Value.DisplayName is { } dn)
258+
{
259+
sb.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(',');
260+
}
261+
else
262+
{
263+
sb.Append("nameof(").Append(fullyQualifiedName).Append('.').Append(member.Key).Append("),");
264+
}
255265
}
256266
}
257267

@@ -349,14 +359,18 @@ public static bool IsDefined(
349359
=> value switch
350360
{
351361
""");
362+
constantValues.Clear();
352363
foreach (var member in enumToGenerate.Names)
353364
{
354-
sb.Append(
355-
"""
365+
if (constantValues.Add(member.Value.ConstantValue))
366+
{
367+
sb.Append(
368+
"""
356369
357-
358-
""").Append(fullyQualifiedName).Append('.').Append(member.Key)
359-
.Append(" => true,");
370+
371+
""").Append(fullyQualifiedName).Append('.').Append(member.Key)
372+
.Append(" => true,");
373+
}
360374
}
361375

362376
sb.Append(

tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ public enum EnumWithExtensionInOtherNamespace
148148

149149
Third = 2,
150150
}
151+
152+
[EnumExtensions]
153+
public enum EnumWithRepeatedValues
154+
{
155+
First = 0,
156+
Second = 1,
157+
Third = 0, // Repeated value
158+
Fourth = Second, // Repeated value
159+
Fifth = Second + Third, // Repeated value
160+
}
161+
162+
[EnumExtensions]
163+
public enum EnumWithRepeatedValuesWithDisplayNames
164+
{
165+
[Display(Name = "Main")] First = 0,
166+
Second = 1,
167+
[Display(Name = "Repeated")] Third = 0, // Repeated value with display name
168+
}
151169
}

0 commit comments

Comments
 (0)