Skip to content

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

Description

@tznind

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.

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:

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions