Skip to content

Commit a1edac8

Browse files
CopilotEgorBo
andcommitted
Simplify validation check to single-level if statement
Changed the NotSupportedException validation logic from nested if statements to a single-level if with a compound condition using logical operators. This addresses PR review feedback to: - Remove unnecessary nesting - Fix indentation issues - Make the logic more readable and maintainable The condition now checks all invalid type scenarios in one expression: - Non-primitive and non-enum types - float and double primitives - Enums backed by float or double All 705 tests pass successfully. Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
1 parent 834b897 commit a1edac8

File tree

1 file changed

+6
-8
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Threading

1 file changed

+6
-8
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Interlocked.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,12 @@ public static unsafe T And<T>(ref T location1, T value) where T : struct
652652
{
653653
// Only integer primitive types and enum types backed by integer types are supported.
654654
// Floating-point types and floating-point backed enums are not supported.
655-
if (!typeof(T).IsPrimitive || typeof(T) == typeof(float) || typeof(T) == typeof(double))
656-
{
657-
if (!typeof(T).IsEnum || typeof(T).GetEnumUnderlyingType() == typeof(float) || typeof(T).GetEnumUnderlyingType() == typeof(double))
655+
if ((!typeof(T).IsPrimitive && !typeof(T).IsEnum) ||
656+
typeof(T) == typeof(float) || typeof(T) == typeof(double) ||
657+
(typeof(T).IsEnum && (typeof(T).GetEnumUnderlyingType() == typeof(float) || typeof(T).GetEnumUnderlyingType() == typeof(double))))
658658
{
659659
throw new NotSupportedException(SR.NotSupported_IntegerEnumOrPrimitiveTypeRequired);
660660
}
661-
}
662661

663662
// For 1-byte and 2-byte types, we need to use CompareExchange-based implementations
664663
// because there are no direct atomic And operations for these sizes.
@@ -796,13 +795,12 @@ public static unsafe T Or<T>(ref T location1, T value) where T : struct
796795
{
797796
// Only integer primitive types and enum types backed by integer types are supported.
798797
// Floating-point types and floating-point backed enums are not supported.
799-
if (!typeof(T).IsPrimitive || typeof(T) == typeof(float) || typeof(T) == typeof(double))
800-
{
801-
if (!typeof(T).IsEnum || typeof(T).GetEnumUnderlyingType() == typeof(float) || typeof(T).GetEnumUnderlyingType() == typeof(double))
798+
if ((!typeof(T).IsPrimitive && !typeof(T).IsEnum) ||
799+
typeof(T) == typeof(float) || typeof(T) == typeof(double) ||
800+
(typeof(T).IsEnum && (typeof(T).GetEnumUnderlyingType() == typeof(float) || typeof(T).GetEnumUnderlyingType() == typeof(double))))
802801
{
803802
throw new NotSupportedException(SR.NotSupported_IntegerEnumOrPrimitiveTypeRequired);
804803
}
805-
}
806804

807805
// For 1-byte and 2-byte types, we need to use CompareExchange-based implementations
808806
// because there are no direct atomic Or operations for these sizes.

0 commit comments

Comments
 (0)