Checks for always-true expressions on collections and arrays

Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: csharp-best-practices/unnecessary-length-count-check

Language: C#

Severity: Warning

Category: Best Practices

Description

Because the Length of an array or Count of a collection will never be negative, some expressions will always evaluate to true, and some will always evaluate to false.

if (collection.Count >= 0) { /* ... */ }
// Equivalent to
if (true) { /* ... */ }

if (arr.Length < 0) { /* ... */ }
// Equivalent to
if (false) { /* ... */ }

This rule warns when always-true or always-false expressions are detected.

Non-Compliant Code Examples

using System.Collections.Generic;
using static System.Linq.Enumerable;

class NonCompliant {
    public static void Main()
    {
        List<char> collection = ['a', 'b', 'c'];
        if (collection.Count >= 0) { /* ... */ }
        if (collection.Count >= 0b0) { /* ... */ }
        if (collection.Count >= 0x0) { /* ... */ }
        if (collection.Count >= -1) { /* ... */ }
        if (collection.Count > -1) { /* ... */ }
        if (collection.Count < 0) { /* ... */ }
        if (collection.Count < -1) { /* ... */ }
		
        char[] array = ['a', 'b', 'c'];
        if (array.Count() >= 0) { /* ... */ }
        if (array.Count() >= 0b0) { /* ... */ }
        if (array.Count() >= 0x0) { /* ... */ }
        if (array.Count() >= -1) { /* ... */ }
        if (array.Count() > -1) { /* ... */ }
        if (array.Count() < 0) { /* ... */ }
        if (array.Count() < -1) { /* ... */ }

        if (array.LongCount() >= 0b0) { /* ... */ }
        if (array.LongCount() >= 0x0) { /* ... */ }
        if (array.LongCount() >= -1) { /* ... */ }
        if (array.LongCount() > -1) { /* ... */ }
        if (array.LongCount() < 0) { /* ... */ }
        if (array.LongCount() < -1) { /* ... */ }

        if (array.Length >= 0) { /* ... */ }
        if (array.Length >= 0b0) { /* ... */ }
        if (array.Length >= 0x0) { /* ... */ }
        if (array.Length >= -1) { /* ... */ }
        if (array.Length > -1) { /* ... */ }
        if (array.Length < 0) { /* ... */ }
        if (array.Length < -1) { /* ... */ }
    }
}

Compliant Code Examples

using System.Collections.Generic;
using static System.Linq.Enumerable;

class Compliant {
    public static void Main()
    {
        List<char> collection = ['a', 'b', 'c'];
        if (collection.Count > 0) { /* ... */ }
        if (collection.Count > 0b0) { /* ... */ }
        if (collection.Count > 0x0) { /* ... */ }
        if (collection.Count > 1) { /* ... */ }
        if (collection.Count == 0) { /* ... */ }
		
        char[] array = ['a', 'b', 'c'];
        if (array.Count() > 0) { /* ... */ }
        if (array.Count() > 0b0) { /* ... */ }
        if (array.Count() > 0x0) { /* ... */ }
        if (array.Count() > 1) { /* ... */ }
        if (array.Count() == 0) { /* ... */ }

        if (array.LongCount() > 0) { /* ... */ }
        if (array.LongCount() > 0b0) { /* ... */ }
        if (array.LongCount() > 0x0) { /* ... */ }
        if (array.LongCount() > 1) { /* ... */ }
        if (array.LongCount() == 0) { /* ... */ }

        if (array.Length > 0) { /* ... */ }
        if (array.Length > 0b0) { /* ... */ }
        if (array.Length > 0x0) { /* ... */ }
        if (array.Length > 1) { /* ... */ }
        if (array.Length == 0) { /* ... */ }
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Integraciones sin problemas. Prueba la Seguridad de Código de Datadog