Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for CA2260 #31632

Merged
merged 5 commits into from
Oct 7, 2022
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
64 changes: 64 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2260.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: "CA2260: Implement generic math interfaces correctly"
description: "Learn about code analysis rule CA2260 - Generic math interfaces require the derived type itself to be used for the self recurring type parameter."
ms.date: 10/06/2022
ms.topic: reference
f1_keywords:
- CA2260
- ImplementGenericMathInterfacesCorrectly
helpviewer_keywords:
- ImplementGenericMathInterfacesCorrectly
- CA2260
author: buyaa-n
---
# CA2260: Implement generic math interfaces correctly

| | Value |
| ----------------------------------- | ------------------------------------ |
| **Rule ID** | CA2260 |
| **Category** | [Usage](usage-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |

## Cause

This rule fires when you implement a generic math interface that requires a self-recurring type parameter and you don't pass the type itself as the type parameter.

## Rule description

Some generic math interfaces introduce static abstract members. The only way to access those static members is through a generic constraint that implements the "curiously recurring template pattern" (CRTP). Therefore, the derived type itself must be used for the self-recurring type parameter. If a type implements such an interface without passing the required type parameter and CA2260 is ignored, the code will compile successfully but the static abstract will not be accessible. Thus, the type will not be usable. The compiler emits a warning with ID CS0315 on such usage.

## How to fix violations

Pass correct type parameter for self recurring type parameter (TSelf) when implementing those interfaces.

### Example

**Violation**:

```csharp
using System;

// Warns: The 'IParsable<TSelf>' requires the 'TSelf' type parameter to be filled with the derived type 'MyDate'
public readonly struct MyDate : IParsable<DateOnly>
{ ... }
```

**Fix**:

Pass the `MyDate` type as the type parameter for the `IParsable<TSelf>` interface.

```csharp
using System;

// No warning
public readonly struct MyDate : IParsable<MyDate>
{ ... }
```

## When to suppress errors

Do not suppress a warning from this rule.

## See also

- [Usage warnings](usage-warnings.md)
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ The following table lists code quality analysis rules.
> | [CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface](ca2256.md) | Types attributed with `DynamicInterfaceCastableImplementationAttribute` act as an interface implementation for a type that implements the `IDynamicInterfaceCastable` type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implements `IDynamicInterfaceCastable` will not provide them otherwise. |
> | [CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static'](ca2257.md) | Since a type that implements `IDynamicInterfaceCastable` may not implement a dynamic interface in metadata, calls to an instance interface member that is not an explicit implementation defined on this type are likely to fail at run time. Mark new interface members `static` to avoid run-time errors. |
> | [CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported](ca2258.md) | Providing a functional `DynamicInterfaceCastableImplementationAttribute`-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic. |
> | [CA2260: Implement generic math interfaces correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self-recurring type parameter. |
> | [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. |
> | [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. |
> | [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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ Usage rules support proper usage of .NET.
|[CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface](ca2256.md) | Types attributed with `DynamicInterfaceCastableImplementationAttribute` act as an interface implementation for a type that implements the `IDynamicInterfaceCastable` type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implements `IDynamicInterfaceCastable` will not provide them otherwise. |
|[CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static'](ca2257.md) | Since a type that implements `IDynamicInterfaceCastable` may not implement a dynamic interface in metadata, calls to an instance interface member that is not an explicit implementation defined on this type are likely to fail at run time. Mark new interface members `static` to avoid run-time errors. |
|[CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported](ca2258.md) | Providing a functional `DynamicInterfaceCastableImplementationAttribute`-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic. |
| [CA2260: Implement generic math interfaces correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self-recurring type parameter. |
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,8 @@ items:
href: code-analysis/quality-rules/ca2257.md
- name: CA2258
href: code-analysis/quality-rules/ca2258.md
- name: CA2260
href: code-analysis/quality-rules/ca2260.md
- name: Code style rules
items:
- name: Overview
Expand Down