-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: "CA1510: Use ArgumentNullException throw helper" | ||
description: "Learn about code analysis rule CA1510: Use ArgumentNullException throw helper" | ||
ms.date: 11/16/2023 | ||
f1_keywords: | ||
- CA1510 | ||
helpviewer_keywords: | ||
- CA1510 | ||
--- | ||
# CA1510: Use ArgumentNullException throw helper | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------| | ||
| **Rule ID** | CA1510 | | ||
| **Title** | Use ArgumentNullException throw helper | | ||
| **Category** | [Maintainability](maintainability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-Breaking | | ||
| **Enabled by default in .NET 8** | No | | ||
|
||
## Cause | ||
|
||
Code checks whether an argument is `null` and then conditionally throws an <xref:System.ArgumentNullException>. | ||
|
||
## Rule description | ||
|
||
Argument checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as <xref:System.ArgumentNullException.ThrowIfNull%2A?displayProperty=nameWithType> are simpler and more efficient than `if` blocks that construct a new exception instance. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1510: | ||
|
||
```csharp | ||
void M(string arg) | ||
{ | ||
if (arg is null) | ||
throw new ArgumentNullException(nameof(arg)); | ||
} | ||
``` | ||
|
||
The following code snippet shows the fix: | ||
|
||
```csharp | ||
void M(string arg) | ||
{ | ||
ArgumentNullException.ThrowIfNull(arg); | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
Replace the `if` block that throws the exception with a call to <xref:System.ArgumentNullException.ThrowIfNull%2A?displayProperty=nameWithType>. Or, in Visual Studio, use the lightbulb menu to fix your code automatically. | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. It is also fine to suppress violations that are identified to be false positives. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1510 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1510 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1510.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: "CA1511: Use ArgumentException throw helper" | ||
description: "Learn about code analysis rule CA1511: Use ArgumentException throw helper" | ||
ms.date: 11/16/2023 | ||
f1_keywords: | ||
- CA1511 | ||
helpviewer_keywords: | ||
- CA1511 | ||
--- | ||
# CA1511: Use ArgumentException throw helper | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------| | ||
| **Rule ID** | CA1511 | | ||
| **Title** | Use ArgumentException throw helper | | ||
| **Category** | [Maintainability](maintainability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-Breaking | | ||
| **Enabled by default in .NET 8** | No | | ||
|
||
## Cause | ||
|
||
Code checks whether an argument is `null` or an empty string and then conditionally throws an <xref:System.ArgumentException>. | ||
|
||
## Rule description | ||
|
||
Argument checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as <xref:System.ArgumentException.ThrowIfNullOrEmpty(System.String,System.String)?displayProperty=nameWithType> are simpler and more efficient than `if` blocks that construct a new exception instance. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1511: | ||
|
||
```csharp | ||
void M(string arg) | ||
{ | ||
if (string.IsNullOrEmpty(arg)) | ||
throw new ArgumentException("", "arg"); | ||
} | ||
``` | ||
|
||
The following code snippet shows the fix: | ||
|
||
```csharp | ||
void M(string arg) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(arg); | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
Replace the `if` block that throws the exception with a call to <xref:System.ArgumentException.ThrowIfNullOrEmpty(System.String,System.String)?displayProperty=nameWithType>. Or, in Visual Studio, use the lightbulb menu to fix your code automatically. | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. It is also fine to suppress violations that are identified to be false positives. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1511 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1511 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1511.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
110 changes: 110 additions & 0 deletions
110
docs/fundamentals/code-analysis/quality-rules/ca1512.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: "CA1512: Use ArgumentOutOfRangeException throw helper" | ||
description: "Learn about code analysis rule CA1512: Use ArgumentOutOfRangeException throw helper" | ||
ms.date: 11/16/2023 | ||
f1_keywords: | ||
- CA1512 | ||
helpviewer_keywords: | ||
- CA1512 | ||
--- | ||
# CA1512: Use ArgumentOutOfRangeException throw helper | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------| | ||
| **Rule ID** | CA1512 | | ||
| **Title** | Use ArgumentOutOfRangeException throw helper | | ||
| **Category** | [Maintainability](maintainability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-Breaking | | ||
| **Enabled by default in .NET 8** | No | | ||
|
||
## Cause | ||
|
||
Code checks whether an argument is less than or greater than a given value and then conditionally throws an <xref:System.ArgumentOutOfRangeException>. | ||
|
||
## Rule description | ||
|
||
Argument checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as <xref:System.ArgumentOutOfRangeException.ThrowIfGreaterThan%2A?displayProperty=nameWithType> are simpler and more efficient than `if` blocks that construct a new exception instance. | ||
|
||
## Example | ||
|
||
The following code snippet shows violations of CA1512: | ||
|
||
```csharp | ||
void M(int arg) | ||
{ | ||
if (arg is 0) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg < 0) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg <= 0) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg <= 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg < 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg > 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg >= 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg == 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
if (arg != 42) | ||
throw new ArgumentOutOfRangeException(nameof(arg)); | ||
} | ||
``` | ||
|
||
The following code snippet shows the fixes: | ||
|
||
```csharp | ||
void M(int arg) | ||
{ | ||
ArgumentOutOfRangeException.ThrowIfZero(arg); | ||
ArgumentOutOfRangeException.ThrowIfNegative(arg); | ||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(arg); | ||
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(arg, 42); | ||
ArgumentOutOfRangeException.ThrowIfLessThan(arg, 42); | ||
ArgumentOutOfRangeException.ThrowIfGreaterThan(arg, 42); | ||
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arg, 42); | ||
ArgumentOutOfRangeException.ThrowIfEqual(arg, 42); | ||
ArgumentOutOfRangeException.ThrowIfNotEqual(arg, 42); | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
Replace the `if` block that throws the exception with a call to one of the following throw-helper methods: | ||
|
||
- <xref:System.ArgumentOutOfRangeException.ThrowIfZero%60%601(%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfNegative%60%601(%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfNegativeOrZero%60%601(%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfLessThanOrEqual%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfLessThan%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfGreaterThan%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfEqual%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
- <xref:System.ArgumentOutOfRangeException.ThrowIfNotEqual%60%601(%60%600,%60%600,System.String)?displayProperty=nameWithType> | ||
|
||
Or, in Visual Studio, use the lightbulb menu to fix your code automatically. | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. It is also fine to suppress violations that are identified to be false positives. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1512 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1512 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1512.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "CA1513: Use ObjectDisposedException throw helper" | ||
description: "Learn about code analysis rule CA1513: Use ObjectDisposedException throw helper" | ||
ms.date: 11/16/2023 | ||
f1_keywords: | ||
- CA1513 | ||
helpviewer_keywords: | ||
- CA1513 | ||
--- | ||
# CA1513: Use ObjectDisposedException throw helper | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------| | ||
| **Rule ID** | CA1513 | | ||
| **Title** | Use ObjectDisposedException throw helper | | ||
| **Category** | [Maintainability](maintainability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-Breaking | | ||
| **Enabled by default in .NET 8** | No | | ||
|
||
## Cause | ||
|
||
Code checks if an object is disposed and then conditionally throws an <xref:System.ObjectDisposedException>. | ||
|
||
## Rule description | ||
|
||
Object checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as <xref:System.ObjectDisposedException.ThrowIf%2A> are simpler and more efficient than `if` blocks that construct a new exception instance. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1513: | ||
|
||
```csharp | ||
class C | ||
{ | ||
private bool _disposed = false; | ||
void M() | ||
{ | ||
if (_disposed) | ||
throw new ObjectDisposedException(GetType().Name); | ||
} | ||
} | ||
``` | ||
|
||
The following code snippet shows the fix: | ||
|
||
```csharp | ||
class C | ||
{ | ||
private bool _disposed = false; | ||
void M() | ||
{ | ||
ObjectDisposedException.ThrowIf(_disposed, GetType().Name); | ||
} | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
Replace the `if` block that throws the exception with a call to <xref:System.ObjectDisposedException.ThrowIf%2A>. Or, in Visual Studio, use the lightbulb menu to fix your code automatically. | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. It is also fine to suppress violations that are identified to be false positives. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1513 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1513 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1513.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters