|
| 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). |
0 commit comments