Skip to content

Commit b015080

Browse files
mpidashgewarren
andauthored
Add documentation for CA1514 (#37229)
* Add documentation for CA1514 * Fix quotes in YAML * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: Genevieve Warren <[email protected]>
1 parent c760c7b commit b015080

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: "CA1514: Avoid redundant length argument"
3+
description: "Learn about code analyzer rule CA1514 - Avoid redundant length argument"
4+
ms.date: 09/23/2023
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA1514
8+
- AvoidLengthCalculationWhenSlicingToEndAnalyzer
9+
helpviewer_keywords:
10+
- CA1514
11+
author: mpidash
12+
dev_langs:
13+
- CSharp
14+
- VB
15+
---
16+
17+
# CA1514: Avoid redundant length argument
18+
19+
| Property | Value |
20+
|-------------------------------------|------------------------------------------------|
21+
| **Rule ID** | CA1514 |
22+
| **Title** | Avoid redundant length argument |
23+
| **Category** | [Maintainability](maintainability-warnings.md) |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default in .NET 7** | No |
26+
27+
## Cause
28+
29+
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.
30+
31+
## Rule description
32+
33+
An explicitly calculated length argument can be error-prone and is unnecessary when you're slicing to the end of a string or buffer.
34+
35+
Code that omits the length argument is more readable and maintainable.
36+
37+
## How to fix violations
38+
39+
Remove the length argument.
40+
41+
## Example
42+
43+
The following code snippet shows a violation of CA1514:
44+
45+
```csharp
46+
string message = "Hello World!";
47+
string world = message.Substring(6, message.Length - 6); // "World!"
48+
```
49+
50+
```vb
51+
Dim message As String = "Hello World!"
52+
Dim world As String = message.Substring(6, message.Length - 6) ' "World!"
53+
```
54+
55+
The following code snippet fixes the violation:
56+
57+
```csharp
58+
string message = "Hello World!";
59+
string world = message.Substring(6); // "World!"
60+
```
61+
62+
```vb
63+
Dim message As String = "Hello World!"
64+
Dim world As String = message.Substring(6) ' "World!"
65+
```
66+
67+
## When to suppress warnings
68+
69+
It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code.
70+
71+
## Suppress a warning
72+
73+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
74+
75+
```csharp
76+
#pragma warning disable CA1514
77+
// The code that's violating the rule is on this line.
78+
#pragma warning restore CA1514
79+
```
80+
81+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
82+
83+
```ini
84+
[*.{cs,vb}]
85+
dotnet_diagnostic.CA1514.severity = none
86+
```
87+
88+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The following table lists code quality analysis rules.
8989
> | [CA1507: Use nameof in place of string](ca1507.md) | A string literal is used as an argument where a `nameof` expression could be used. |
9090
> | [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. |
9191
> | [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. |
92+
> | [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. |
9293
> | [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. |
9394
> | [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. |
9495
> | [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. |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Maintainability rules support library and application maintenance.
2626
| [CA1507: Use nameof in place of string](ca1507.md) | A string literal is used as an argument where a `nameof` expression could be used. |
2727
| [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. |
2828
| [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. |
29+
| [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. |
2930

3031
## See also
3132

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ items:
835835
href: ../../fundamentals/code-analysis/quality-rules/ca1508.md
836836
- name: CA1509
837837
href: ../../fundamentals/code-analysis/quality-rules/ca1509.md
838+
- name: CA1514
839+
href: ../../fundamentals/code-analysis/quality-rules/ca1514.md
838840
- name: Naming rules
839841
items:
840842
- name: Overview

0 commit comments

Comments
 (0)