From e7079c82bf3b004a9e10aaf1721d8175071027fe Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Sat, 23 Sep 2023 17:07:25 +0200 Subject: [PATCH 1/3] Add documentation for CA1514 --- .../code-analysis/quality-rules/ca1514.md | 88 +++++++++++++++++++ .../code-analysis/quality-rules/index.md | 1 + .../quality-rules/maintainability-warnings.md | 1 + docs/navigate/tools-diagnostics/toc.yml | 2 + 4 files changed, 92 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/ca1514.md diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1514.md b/docs/fundamentals/code-analysis/quality-rules/ca1514.md new file mode 100644 index 0000000000000..8d0920f7ce102 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1514.md @@ -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 , , , or 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. + +## 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). diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 7bf6ca5b9f026..c4130993481ee 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -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. This can be error-prone and can be avoided altogether. | > | [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. | diff --git a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md index f48719a88a719..7aa9499e842b9 100644 --- a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md @@ -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. This can be error-prone and can be avoided altogether. | ## See also diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 3d95446446e3e..b841a69fb7ea7 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -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 From d5001752f8ea79f4ab640c30911c3d9e17b920e3 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Sat, 23 Sep 2023 17:26:13 +0200 Subject: [PATCH 2/3] Fix quotes in YAML --- docs/fundamentals/code-analysis/quality-rules/ca1514.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1514.md b/docs/fundamentals/code-analysis/quality-rules/ca1514.md index 8d0920f7ce102..eda0d0d21d3c4 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1514.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1514.md @@ -1,5 +1,5 @@ --- -title: "CA1514: Avoid redundant length argument +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 From 2a5c92e7b671f0be9a4e498e60b8f408d5b43d73 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Mon, 25 Sep 2023 21:01:04 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1514.md | 4 ++-- docs/fundamentals/code-analysis/quality-rules/index.md | 2 +- .../code-analysis/quality-rules/maintainability-warnings.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1514.md b/docs/fundamentals/code-analysis/quality-rules/ca1514.md index eda0d0d21d3c4..e558183c6780f 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1514.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1514.md @@ -30,9 +30,9 @@ A redundant length argument is passed to | [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. This can be error-prone and can be avoided altogether. | +> | [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. | diff --git a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md index 7aa9499e842b9..ac649966e6bfc 100644 --- a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md @@ -27,7 +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. This can be error-prone and can be avoided altogether. | +| [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