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..e558183c6780f --- /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 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). diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 7bf6ca5b9f026..837b67e4708e0 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. 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 f48719a88a719..ac649966e6bfc 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. A calculated length can be error-prone and is also unnecessary. | ## 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