-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add documentation for CA1514 #37229
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
Merged
Merged
Add documentation for CA1514 #37229
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not needed when slicing to the end of a string or buffer. | ||
|
|
||
| Omitting the length argument results in more readable and maintainable code. | ||
mpidash marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.