github / github/codeql

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

未关闭
#2,993 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
C# false-positive
主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 15 小时
30 天内合并 PR
141

描述

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);
}
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。