@@ -152,18 +152,16 @@ private static (string textForUser, ExpressionSyntax MaybeFullExpression) Conver
152
152
int parsedHexValue = int . Parse ( hexValue , NumberStyles . HexNumber , CultureInfo . InvariantCulture ) ;
153
153
154
154
// This is a very special case where for 8 digit hex strings, C# interprets them as unsigned ints, but VB interprets them as ints
155
- // This can lead to a compile error if assigned to an int in VB. So in a case like 0x91234567, we generate `int.MinValue + 0x11234567 `
155
+ // This can lead to a compile error if assigned to an int in VB. So in a case like 0x91234567, we generate `unchecked(( int)0x91234567) `
156
156
// This way the value looks pretty close to before and remains a compile time constant
157
157
if ( parsedHexValue < 0 ) {
158
- int positiveValue = parsedHexValue - int . MinValue ;
159
-
160
- var intMinValueExpr = SyntaxFactory . MemberAccessExpression (
161
- SyntaxKind . SimpleMemberAccessExpression ,
162
- SyntaxFactory . PredefinedType (
163
- SyntaxFactory . Token ( SyntaxKind . IntKeyword ) ) ,
164
- ValidSyntaxFactory . IdentifierName ( nameof ( int . MinValue ) ) ) ;
165
- var positiveValueExpr = NumericLiteral ( SyntaxFactory . Literal ( "0x" + positiveValue . ToString ( "X8" , CultureInfo . InvariantCulture ) , positiveValue ) ) ;
166
- return ( null , SyntaxFactory . BinaryExpression ( SyntaxKind . AddExpression , intMinValueExpr , positiveValueExpr ) ) ;
158
+ var hexValueExpr = NumericLiteral ( SyntaxFactory . Literal ( textForUser , parsedHexValue ) ) ;
159
+ var checkedExpr = SyntaxFactory . CheckedExpression (
160
+ CSSyntaxKind . UncheckedExpression ,
161
+ SyntaxFactory . CastExpression (
162
+ SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( CSSyntaxKind . IntKeyword ) ) ,
163
+ hexValueExpr ) ) ;
164
+ return ( null , checkedExpr ) ;
167
165
}
168
166
} else if ( canBeBinaryOrHex && textForUser . StartsWith ( "&B" , StringComparison . OrdinalIgnoreCase ) ) {
169
167
textForUser = "0b" + textForUser . Substring ( 2 ) ;
0 commit comments