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 documentation for CA1514 #37229

Merged
merged 3 commits into from
Oct 5, 2023
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
88 changes: 88 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca1514.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: "CA1514: Avoid redundant length argument"
description: "Learn about code analyzer rule CA1514 - Avoid redundant length argument"
ms.date: 09/23/2023
ms.topic: reference
f1_keywords:
- CA1514
- AvoidLengthCalculationWhenSlicingToEndAnalyzer
helpviewer_keywords:
- CA1514
author: mpidash
dev_langs:
- CSharp
- VB
---

# CA1514: Avoid redundant length argument

| Property | Value |
|-------------------------------------|------------------------------------------------|
| **Rule ID** | CA1514 |
| **Title** | Avoid redundant length argument |
| **Category** | [Maintainability](maintainability-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 7** | No |

## Cause

A redundant length argument is passed to <xref:System.String.Substring%2A?displayProperty=nameWithType>, <xref:System.Span%601.Slice%2A?displayProperty=nameWithType>, <xref:System.ReadOnlySpan%601.Slice%2A?displayProperty=nameWithType>, or <xref:System.Memory%601.Slice%2A?displayProperty=nameWithType> when slicing to the end of a string or buffer.

## Rule description

An explicitly calculated length argument can be error-prone and is unnecessary when you're slicing to the end of a string or buffer.

Code that omits the length argument is more readable and maintainable.

## How to fix violations

Remove the length argument.

## Example

The following code snippet shows a violation of CA1514:

```csharp
string message = "Hello World!";
string world = message.Substring(6, message.Length - 6); // "World!"
```

```vb
Dim message As String = "Hello World!"
Dim world As String = message.Substring(6, message.Length - 6) ' "World!"
```

The following code snippet fixes the violation:

```csharp
string message = "Hello World!";
string world = message.Substring(6); // "World!"
```

```vb
Dim message As String = "Hello World!"
Dim world As String = message.Substring(6) ' "World!"
```

## 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.

## 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 CA1514
// The code that's violating the rule is on this line.
#pragma warning restore CA1514
```

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.CA1514.severity = none
```

For more information, see [How to suppress code analysis warnings](../suppress-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 @@ -90,6 +90,7 @@ The following table lists code quality analysis rules.
> | [CA1507: Use nameof in place of string](ca1507.md) | A string literal is used as an argument where a `nameof` expression could be used. |
> | [CA1508: Avoid dead conditional code](ca1508.md) | A method has conditional code that always evaluates to `true` or `false` at run time. This leads to dead code in the `false` branch of the condition. |
> | [CA1509: Invalid entry in code metrics configuration file](ca1509.md) | Code metrics rules, such as [CA1501](ca1501.md), [CA1502](ca1502.md), [CA1505](ca1505.md) and [CA1506](ca1506.md), supplied a configuration file named `CodeMetricsConfig.txt` that has an invalid entry. |
> | [CA1514: Avoid redundant length argument](ca1514.md) | A redundant length argument is used when slicing to the end of a string or buffer. A calculated length can be error-prone and is also unnecessary. |
> | [CA1700: Do not name enum values 'Reserved'](ca1700.md) | This rule assumes that an enumeration member that has a name that contains "reserved" is not currently used but is a placeholder to be renamed or removed in a future version. Renaming or removing a member is a breaking change. |
> | [CA1707: Identifiers should not contain underscores](ca1707.md) | By convention, identifier names do not contain the underscore (_) character. This rule checks namespaces, types, members, and parameters. |
> | [CA1708: Identifiers should differ by more than case](ca1708.md) | Identifiers for namespaces, types, members, and parameters cannot differ only by case because languages that target the common language runtime are not required to be case-sensitive. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Maintainability rules support library and application maintenance.
| [CA1507: Use nameof in place of string](ca1507.md) | A string literal is used as an argument where a `nameof` expression could be used. |
| [CA1508: Avoid dead conditional code](ca1508.md) | A method has conditional code that always evaluates to `true` or `false` at run time. This leads to dead code in the `false` branch of the condition. |
| [CA1509: Invalid entry in code metrics configuration file](ca1509.md) | Code metrics rules, such as [CA1501](ca1501.md), [CA1502](ca1502.md), [CA1505](ca1505.md) and [CA1506](ca1506.md), supplied a configuration file named `CodeMetricsConfig.txt` that has an invalid entry. |
| [CA1514: Avoid redundant length argument](ca1514.md) | A redundant length argument is used when slicing to the end of a string or buffer. A calculated length can be error-prone and is also unnecessary. |

## See also

Expand Down
2 changes: 2 additions & 0 deletions docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ items:
href: ../../fundamentals/code-analysis/quality-rules/ca1508.md
- name: CA1509
href: ../../fundamentals/code-analysis/quality-rules/ca1509.md
- name: CA1514
href: ../../fundamentals/code-analysis/quality-rules/ca1514.md
- name: Naming rules
items:
- name: Overview
Expand Down
Loading