Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions docs/fundamentals/code-analysis/quality-rules/ca1001.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA1001: Types that own disposable fields should be disposable (code analysis)"
description: "Learn about code analysis rule CA1001: Types that own disposable fields should be disposable"
ms.date: 11/04/2016
ms.date: 12/18/2020
ms.topic: reference
f1_keywords:
- CA1001
Expand All @@ -25,23 +25,23 @@ dev_langs:

## Cause

A class declares and implements an instance field that is a <xref:System.IDisposable?displayProperty=fullName> type and the class does not implement <xref:System.IDisposable>.
A class declares and implements an instance field that is an <xref:System.IDisposable?displayProperty=fullName> type, and the class does not implement <xref:System.IDisposable>.

By default, this rule analyzes the entire codebase, but this is [configurable](#configure-code-to-analyze).

## Rule description

A class implements the <xref:System.IDisposable> interface to dispose of unmanaged resources that it owns. An instance field that is an <xref:System.IDisposable> type indicates that the field owns an unmanaged resource. A class that declares an <xref:System.IDisposable> field indirectly owns an unmanaged resource and should implement the <xref:System.IDisposable> interface. If the class does not directly own any unmanaged resources, it should not implement a finalizer.
A class that declares an <xref:System.IDisposable> field indirectly owns an unmanaged resource. The class should implement the <xref:System.IDisposable> interface to dispose of the unmanaged resource that it owns once the resource is no longer in use. If the class does not *directly* own any unmanaged resources, it should not implement a finalizer.

This rule respects types implementing <xref:System.IAsyncDisposable?displayProperty=fullName> as disposable types.

## How to fix violations

To fix a violation of this rule, implement <xref:System.IDisposable> and from the <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> method call the <xref:System.IDisposable.Dispose%2A> method of the field.
To fix a violation of this rule, implement the <xref:System.IDisposable> interface. In the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method, call the <xref:System.IDisposable.Dispose%2A> method of the field's type.

## When to suppress warnings

In general, do not suppress a warning from this rule. It is OK to suppress the warning when the dispose ownership of the field(s) is not held by the containing type.
In general, do not suppress a warning from this rule. It's okay to suppress the warning when the dispose ownership of the field is not held by the containing type.

## Configure code to analyze

Expand Down Expand Up @@ -69,3 +69,7 @@ The following example shows a class that violates the rule and a class that sati
- [CA2213: Disposable fields should be disposed](ca2213.md)
- [CA2216: Disposable types should declare finalizer](ca2216.md)
- [CA2215: Dispose methods should call base class dispose](ca2215.md)

## See also

- [Implement a Dispose method](../../../standard/garbage-collection/implementing-dispose.md)
18 changes: 7 additions & 11 deletions docs/fundamentals/code-analysis/quality-rules/ca1062.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA1062: Validate arguments of public methods (code analysis)"
description: "Learn about code analysis rule CA1062: Validate arguments of public methods"
ms.date: 11/04/2016
ms.date: 12/18/2020
ms.topic: reference
f1_keywords:
- CA1062
Expand All @@ -28,17 +28,13 @@ dev_langs:

An externally visible method dereferences one of its reference arguments without verifying whether that argument is `null` (`Nothing` in Visual Basic).

The following aspects of this rule are [configurable](#configure-code-to-analyze):

- Whether or not to analyze extension method 'this' parameter.
- Specify null check validation methods in referenced libraries or projects, which validate that arguments passed to it are non-null.
- Parts of codebase to exclude from analysis.
You can [configure](#configure-code-to-analyze) this rule to exclude certain types and parameters from analysis. You can also [indicate null-check validation methods](#null-check-validation-methods).

## Rule description

All reference arguments that are passed to externally visible methods should be checked against `null`. If appropriate, throw a <xref:System.ArgumentNullException> when the argument is `null`.

If a method can be called from an unknown assembly because it is declared public or protected, you should validate all parameters of the method. If the method is designed to be called only by known assemblies, you should make the method internal and apply the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute to the assembly that contains the method.
If a method can be called from an unknown assembly because it is declared public or protected, you should validate all parameters of the method. If the method is designed to be called only by known assemblies, mark the method `internal` and apply the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute to the assembly that contains the method.

## How to fix violations

Expand Down Expand Up @@ -73,15 +69,15 @@ dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true

### Null check validation methods

This rule can lead to false positives if your code calls special null check validation methods in referenced libraries or projects. You can avoid these false positives by specifying the name or signature of null check validation methods. The analysis will then assume that arguments passed to this method are non-null after the call. For example, to mark all methods named `Validate` as null check validation methods, you can add the following key-value pair to an *.editorconfig* file in your project:
This rule can lead to false positives if your code calls special null-check validation methods in referenced libraries or projects. You can avoid these false positives by specifying the name or signature of null-check validation methods. The analysis assumes that arguments passed to these methods are non-null after the call. For example, to mark all methods named `Validate` as null-check validation methods, add the following key-value pair to an *.editorconfig* file in your project:

```ini
dotnet_code_quality.CA1062.null_check_validation_methods = Validate
```

Allowed method name formats in the option value (separated by `|`):

- Method name only (includes all methods with the name, regardless of the containing type or namespace)
- Method name only (includes all methods with the name, regardless of the containing type or namespace).
- Fully qualified names in the symbol's [documentation ID format](../../../csharp/programming-guide/xmldoc/processing-the-xml-file.md#id-strings), with an optional `M:` prefix.

Examples:
Expand Down Expand Up @@ -165,7 +161,7 @@ End Namespace

## Example 2

Copy constructors that populate field or properties that are reference objects can also violate the CA1062 rule. The violation occurs because the copied object that is passed to the copy constructor might be `null` (`Nothing` in Visual Basic). To resolve the violation, use a static (Shared in Visual Basic) method to check that the copied object is not null.
Copy constructors that populate field or properties that are reference objects can also violate rule CA1062. The violation occurs because the copied object that's passed to the copy constructor might be `null` (`Nothing` in Visual Basic). To resolve the violation, use a `static` (`Shared` in Visual Basic) method to check that the copied object is not null.

In the following `Person` class example, the `other` object that is passed to the `Person` copy constructor might be `null`.

Expand All @@ -192,7 +188,7 @@ public class Person

## Example 3

In the following revised `Person` example, the `other` object that is passed to the copy constructor is first checked for null in the `PassThroughNonNull` method.
In the following revised `Person` example, the `other` object that's passed to the copy constructor is first checked for null in the `PassThroughNonNull` method.

```csharp
public class Person
Expand Down