Skip to content

Commit 9541ee9

Browse files
NewellClarkYoussef1313gewarren
authored
Ca2250 (#24324)
* Add file ca2250.md * Update ca2250.md * Add CA2250 * Add CA2250 * Add CA2250 * Apply suggestions from code review Co-authored-by: Youssef Victor <youssefvictor00@gmail.com> * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Co-authored-by: Youssef Victor <youssefvictor00@gmail.com> Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
1 parent a45aa5e commit 9541ee9

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: "CA2250: Use `ThrowIfCancellationRequested`"
3+
description: "Learn about code analysis rule CA2250: Use `ThrowIfCancellationRequested`"
4+
ms.date: 05/21/2021
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA2250
8+
- UseCancellationTokenThrowIfCancellationRequested
9+
helpviewer_keywords:
10+
- UseCancellationTokenThrowIfCancellationRequested
11+
- CA2250
12+
author: NewellClark
13+
dev_langs:
14+
- CSharp
15+
- VB
16+
---
17+
# CA2250: Use `ThrowIfCancellationRequested`
18+
19+
| | Value |
20+
|-|-|
21+
| **Rule ID** |CA2250|
22+
| **Category** |[Usage](usage-warnings.md)|
23+
| **Fix is breaking or non-breaking** |Non-breaking|
24+
25+
## Cause
26+
27+
This rule flags conditional statements that check <xref:System.Threading.CancellationToken.IsCancellationRequested> before throwing <xref:System.OperationCanceledException>.
28+
29+
## Rule description
30+
31+
You can accomplish the same thing by calling <xref:System.Threading.CancellationToken.ThrowIfCancellationRequested?displayProperty=nameWithType>.
32+
33+
## How to fix violations
34+
35+
To fix violations, replace the conditional statement with a call to <xref:System.Threading.CancellationToken.ThrowIfCancellationRequested>.
36+
37+
```csharp
38+
using System;
39+
using System.Threading;
40+
41+
public void MySlowMethod(CancellationToken token)
42+
{
43+
// Violation
44+
if (token.IsCancellationRequested)
45+
throw new OperationCanceledException();
46+
47+
// Fix
48+
token.ThrowIfCancellationRequested();
49+
50+
// Violation
51+
if (token.IsCancellationRequested)
52+
throw new OperationCanceledException();
53+
else
54+
DoSomethingElse();
55+
56+
// Fix
57+
token.ThrowIfCancellationRequested();
58+
DoSomethingElse();
59+
}
60+
```
61+
62+
```vb
63+
Imports System
64+
Imports System.Threading
65+
66+
Public Sub MySlowMethod(token As CancellationToken)
67+
68+
' Violation
69+
If token.IsCancellationRequested Then
70+
Throw New OperationCanceledException()
71+
End If
72+
73+
' Fix
74+
token.ThrowIfCancellationRequested()
75+
76+
' Violation
77+
If token.IsCancellationRequested Then
78+
Throw New OperationCanceledException()
79+
Else
80+
DoSomethingElse()
81+
End If
82+
83+
' Fix
84+
token.ThrowIfCancellationRequested()
85+
DoSomethingElse()
86+
End Sub
87+
```
88+
89+
## When to suppress warnings
90+
91+
It is safe to suppress warnings from this rule.
92+
93+
## See also
94+
95+
- [Usage Warnings](usage-warnings.md)

docs/fundamentals/code-analysis/quality-rules/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ The following table lists code quality analysis rules.
179179
> | [CA2247: Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum.](ca2247.md) | TaskCompletionSource has constructors that take TaskCreationOptions that control the underlying Task, and constructors that take object state that's stored in the task. Accidentally passing a TaskContinuationOptions instead of a TaskCreationOptions will result in the call treating the options as state. |
180180
> | [CA2248: Provide correct enum argument to Enum.HasFlag](ca2248.md) | The enum type passed as an argument to the `HasFlag` method call is different from the calling enum type. |
181181
> | [CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md) | Calls to `string.IndexOf` where the result is used to check for the presence/absence of a substring can be replaced by `string.Contains`. |
182+
> | [CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has. |
182183
> | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
183184
> | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
184185
> | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |

docs/fundamentals/code-analysis/quality-rules/usage-warnings.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ Usage rules support proper usage of .NET.
5252
|[CA2247: Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum](ca2247.md)|TaskCompletionSource has constructors that take TaskCreationOptions that control the underlying Task, and constructors that take object state that's stored in the task. Accidentally passing a TaskContinuationOptions instead of a TaskCreationOptions will result in the call treating the options as state.|
5353
|[CA2248: Provide correct 'enum' argument to 'Enum.HasFlag'](ca2248.md)|The enum type passed as an argument to the `HasFlag` method call is different from the calling enum type.|
5454
|[CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md)|Calls to `string.IndexOf` where the result is used to check for the presence or absence of a substring can be replaced by `string.Contains`.|
55+
|[CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has.|

docs/fundamentals/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,8 @@ items:
12251225
href: code-analysis/quality-rules/ca2248.md
12261226
- name: CA2249
12271227
href: code-analysis/quality-rules/ca2249.md
1228+
- name: CA2250
1229+
href: code-analysis/quality-rules/ca2250.md
12281230
- name: Code style rules
12291231
items:
12301232
- name: Overview

0 commit comments

Comments
 (0)