False warning 'possible null reference exception' ?

I have this code:
if (sourceElement != null && sourceElement is IHTMLElement3)
{
IHTMLElement3 element3 = sourceElement as IHTMLElement3;
element3.setActive();
}

And r# on word element3 in expression 'element3.setActive()' shows me
warning about possible null reference exception. Is this ok?, because
in if condition, there is test if sourceElement is IHTMLElement3 and if
so only then cast to IHTMLElement3 element3 variable.

field sourceElement has type IHTMLElement, and IHTMLElement3 type is
NOT iherited from IHTMLElement. These are interfaces from mshtml COM
library.


Resharper 3.0.2
--
Peter Sulek
terrorix@centrum.sk
XanaNews ver. 1.18.1.6



Attachment(s):
r#_false_nullref.jpg
3 comments
Comment actions Permalink

Hello Peter,

It is probably a bug or a known limitation, responsible developer is on vacation
right now.

Anyway, you'd better use:

IHTMLElement3 element3 = sourceElement as IHTMLElement3;
if (element3 != null)
element3.setActive();

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PS> I have this code:
PS> if (sourceElement != null && sourceElement is IHTMLElement3)
PS> {
PS> IHTMLElement3 element3 = sourceElement as IHTMLElement3;
PS> element3.setActive();
PS> }
PS> And r# on word element3 in expression 'element3.setActive()' shows
PS> me warning about possible null reference exception. Is this ok?,
PS> because in if condition, there is test if sourceElement is
PS> IHTMLElement3 and if so only then cast to IHTMLElement3 element3
PS> variable.
PS>
PS> field sourceElement has type IHTMLElement, and IHTMLElement3 type is
PS> NOT iherited from IHTMLElement. These are interfaces from mshtml COM
PS> library.
PS>
PS> Resharper 3.0.2
PS>


0
Comment actions Permalink

Ilya Ryzhenkov wrote:

Hello Peter,

It is probably a bug or a known limitation, responsible developer is
on vacation right now. Anyway, you'd better use:

IHTMLElement3 element3 = sourceElement as IHTMLElement3;
if (element3 != null)
element3.setActive();

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PS> I have this code:
PS> if (sourceElement != null && sourceElement is IHTMLElement3)
PS> {
PS> IHTMLElement3 element3 = sourceElement as IHTMLElement3;
PS> element3.setActive();
PS> }
PS> And r# on word element3 in expression 'element3.setActive()' shows
PS> me warning about possible null reference exception. Is this ok?,
PS> because in if condition, there is test if sourceElement is
PS> IHTMLElement3 and if so only then cast to IHTMLElement3 element3
PS> variable.
PS> PS> field sourceElement has type IHTMLElement, and IHTMLElement3
type is PS> NOT iherited from IHTMLElement. These are interfaces from
mshtml COM PS> library.
PS> PS> Resharper 3.0.2
PS>


Yes, this is another solution i know. I just want to know if its an bug
in r# or not. Because we have to much expressions like this one in our
solution and have no time to rewrite it :)

--
Peter Sulek
terrorix@centrum.sk
XanaNews ver. 1.18.1.6

0
Comment actions Permalink

Hello Peter,

At least you can spot them with this ReSharper bug ;)

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PS> Ilya Ryzhenkov wrote:
PS>
>> Hello Peter,
>>
>> It is probably a bug or a known limitation, responsible developer is
>> on vacation right now. Anyway, you'd better use:
>>
>> IHTMLElement3 element3 = sourceElement as IHTMLElement3;
>> if (element3 != null)
>> element3.setActive();
>> Sincerely,
>> Ilya Ryzhenkov
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> PS> I have this code:
>> PS> if (sourceElement != null && sourceElement is IHTMLElement3)
>> PS> {
>> PS> IHTMLElement3 element3 = sourceElement as IHTMLElement3;
>> PS> element3.setActive();
>> PS> }
>> PS> And r# on word element3 in expression 'element3.setActive()'
>> shows
>> PS> me warning about possible null reference exception. Is this ok?,
>> PS> because in if condition, there is test if sourceElement is
>> PS> IHTMLElement3 and if so only then cast to IHTMLElement3 element3
>> PS> variable.
>> PS> PS> field sourceElement has type IHTMLElement, and IHTMLElement3
>> type is PS> NOT iherited from IHTMLElement. These are interfaces from
>> mshtml COM PS> library.
>> PS> PS> Resharper 3.0.2
>> PS>
PS> Yes, this is another solution i know. I just want to know if its an
PS> bug in r# or not. Because we have to much expressions like this one
PS> in our solution and have no time to rewrite it :)
PS>


0

Please sign in to leave a comment.