github / github/codeql

LGTM.com - false positive C# if `is` followed by `as`

Đang mở
#2,993 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
C# false-positive
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

Warning for 'value might be null' because upstream path potentially contains a null. Caused by use of `as` keyword but preceeding `is` keyword should make it impossible.

```csharp
if (value is string)
Validate(fi, dicomFile, dicomItem, value as string); //<- lgtm considers that 'value' could be null because of as
```

https://lgtm.com/projects/g/SMI/SmiServices/snapshot/673082cacc04b4597e96c7461c47096b94d08a45/files/src/microservices/Microservices.IsIdentifiable/Runners/DicomFileRunner.cs?sort=name&dir=ASC&mode=heatmap#x75953d6da49aa143:1

Admittedly the code itself could be better. It should probably use a direct cast instead of `as` or use the declaration feature of C# 7.0 (i.e. `if(value is string s)`)

Unit test:

```csharp
[Test]
public void TestNull()
{
string a = null;

Assert.IsFalse(a is string);
a = "yayy";
Assert.IsTrue(a is string);
}
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.