You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fundamentals/code-analysis/quality-rules/ca2020.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,11 +47,12 @@ With the [numeric IntPtr feature](https://github.com/dotnet/csharplang/blob/main
47
47
Investigate the code to determine if the expression flagged could cause a behavioral change, and choose appropriate way to fix the diagnostic
48
48
49
49
Fix options:
50
-
- If the expression would not cause a behavioral change:
51
-
- If the `IntPtr/UIntPtr` type used as native int/uint purpose change the type to `nint/nuint`.
52
-
- If the `IntPtr/UIntPtr` type used as native pointer change the type to corresponding native pointer type
53
-
- If you cannot change the type of the variable last resort will be suppress the warning
54
-
- If the expression could cause a behavioral change wrap it with corresponding `checked`/`unchecked` statement to preserve the .NET 6.0 behavior.
50
+
51
+
- If the expression would not cause a behavioral change:
52
+
- If the `IntPtr/UIntPtr` type used as native int/uint purpose change the type to `nint/nuint`.
53
+
- If the `IntPtr/UIntPtr` type used as native pointer change the type to corresponding native pointer type
54
+
- If you cannot change the type of the variable last resort will be suppress the warning
55
+
- If the expression could cause a behavioral change wrap it with corresponding `checked`/`unchecked` statement to preserve the .NET 6.0 behavior.
55
56
56
57
### Example
57
58
@@ -81,15 +82,15 @@ public unsafe class IntPtrTest
81
82
intPtrVariable= (IntPtr)longVariable; // Starting with .NET 7 the explicit conversion '(IntPtr)Int64' will not throw when overflowing in an unchecked context. Wrap the expression with a 'checked' statement to restore the .NET 6 behavior.
82
83
83
84
inta= (int)intPtrVariable; // Starting with .NET 7 the explicit conversion '(Int32)IntPtr' will not throw when overflowing in an unchecked context. Wrap the expression with a 'checked' statement to restore the .NET 6 behavior.
84
-
}
85
+
}
85
86
}
86
87
```
87
88
88
89
**Fix**:
89
90
90
-
- If the expression would not cause a behavioral change:
91
-
- If the IntPtr/UIntPtr type used as native int/uint purpose change the type to nint/nuint.
92
-
91
+
- If the expression would not cause a behavioral change:
92
+
- If the IntPtr/UIntPtr type used as native int/uint purpose change the type to nint/nuint.
93
+
93
94
```csharp
94
95
usingSystem;
95
96
@@ -114,11 +115,11 @@ public unsafe class IntPtrTest
114
115
intPtrVariable= (nint)longVariable;
115
116
116
117
inta= (int)intPtrVariable;
117
-
}
118
+
}
118
119
}
119
-
```
120
+
```
120
121
121
-
- If the expression could cause a behavioral change wrap it with corresponding `checked`/`unchecked` statement to preserve the old .NET 6.0 behavior.
122
+
- If the expression could cause a behavioral change wrap it with corresponding `checked`/`unchecked` statement to preserve the old .NET 6.0 behavior.
122
123
123
124
```csharp
124
125
usingSystem;
@@ -144,9 +145,9 @@ public unsafe class IntPtrTest
144
145
intPtrVariable=checked((IntPtr)longVariable); // wrap with checked
0 commit comments