Checks for always-true expressions on collections and arrays This product is not supported for your selected
Datadog site . (
).
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우
언제든지 연락주시기 바랍니다.
이 규칙을 사용해 보세요 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 >= 0 b0 ) { /* ... */ }
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 () >= 0 b0 ) { /* ... */ }
if ( array . Count () >= 0x0 ) { /* ... */ }
if ( array . Count () >= - 1 ) { /* ... */ }
if ( array . Count () > - 1 ) { /* ... */ }
if ( array . Count () < 0 ) { /* ... */ }
if ( array . Count () < - 1 ) { /* ... */ }
if ( array . LongCount () >= 0 b0 ) { /* ... */ }
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 >= 0 b0 ) { /* ... */ }
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 > 0 b0 ) { /* ... */ }
if ( collection . Count > 0x0 ) { /* ... */ }
if ( collection . Count > 1 ) { /* ... */ }
if ( collection . Count == 0 ) { /* ... */ }
char [] array = [ 'a' , 'b' , 'c' ];
if ( array . Count () > 0 ) { /* ... */ }
if ( array . Count () > 0 b0 ) { /* ... */ }
if ( array . Count () > 0x0 ) { /* ... */ }
if ( array . Count () > 1 ) { /* ... */ }
if ( array . Count () == 0 ) { /* ... */ }
if ( array . LongCount () > 0 ) { /* ... */ }
if ( array . LongCount () > 0 b0 ) { /* ... */ }
if ( array . LongCount () > 0x0 ) { /* ... */ }
if ( array . LongCount () > 1 ) { /* ... */ }
if ( array . LongCount () == 0 ) { /* ... */ }
if ( array . Length > 0 ) { /* ... */ }
if ( array . Length > 0 b0 ) { /* ... */ }
if ( array . Length > 0x0 ) { /* ... */ }
if ( array . Length > 1 ) { /* ... */ }
if ( array . Length == 0 ) { /* ... */ }
}
}
원활한 통합. Datadog Code Security를 경험해 보세요