Another invalid "Possible Null Reference Exception"

I understand why ReSharper isn't catching this... but it probably should
catch things like this:

                    if (Path.HasExtension(file) &&
Path.GetExtension(file).ToUpperInvariant().Equals(".SQL"))
                    {
                    ...

In this case, the "Path.GetExtension(file)" is underlined and a "possible
null-reference exception" warning is given.  But because of the
"HasExtension" preceding it, that simply isn't true.

I think it would be smart if ReSharper gained the intelligence to "know
about" the "Has" functions so that they can operate the same was as adding a
call to "string.IsNullOrEmpty(GetExtension(file))" ... since the effect is
nearly identical.

Thoughts?



2 comments
Comment actions Permalink

Hello Paul,

Thank you for feedback! I think in general finding out that some method checks
some conditions and the other method doesn't return null under those conditions
is a really hard code analysis task (or even unsolvable), so I'm afraid we
won't be able to teach ReSharper to "catch" such things in the foreseeable
future.

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I understand why ReSharper isn't catching this... but it probably
should catch things like this:

if (Path.HasExtension(file) &&
Path.GetExtension(file).ToUpperInvariant().Equals(".SQL"))
{
...
In this case, the "Path.GetExtension(file)" is underlined and a
"possible null-reference exception" warning is given.  But because of
the "HasExtension" preceding it, that simply isn't true.

I think it would be smart if ReSharper gained the intelligence to
"know about" the "Has" functions so that they can operate the same was
as adding a call to "string.IsNullOrEmpty(GetExtension(file))" ...
since the effect is nearly identical.

Thoughts?



0
Comment actions Permalink

I guess I was hoping you could just add "HasExtension" (and the handful of
other "Has" methods) as valid checks for "GetExtension" (and the hanful of
other "Get" methods)... in short, treat it similar to the way
"string.IsNullOrEmpty()" is treated prior to a reference of a string
variable.  It's taking the same concept and extending it.

Otherwise the code starts looking awful just to avoid the erroneous
warning... having to put a redundant null check, or mess up the code with
comments to suppress the warning... seems ReSharper really should be smart
enough to handle this.


"Andrew Serebryansky" <andrew.serebryansky@jetbrains.com> wrote in message
news:c8a898dd96288cd01eead964f78@news.intellij.net...

Hello Paul,

>

Thank you for feedback! I think in general finding out that some method
checks some conditions and the other method doesn't return null under
those conditions is a really hard code analysis task (or even unsolvable),
so I'm afraid we won't be able to teach ReSharper to "catch" such things
in the foreseeable future.

>

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>> I understand why ReSharper isn't catching this... but it probably
>> should catch things like this:
>>
>> if (Path.HasExtension(file) &&
>> Path.GetExtension(file).ToUpperInvariant().Equals(".SQL"))
>> {
>> ...
>> In this case, the "Path.GetExtension(file)" is underlined and a
>> "possible null-reference exception" warning is given.  But because of
>> the "HasExtension" preceding it, that simply isn't true.
>>
>> I think it would be smart if ReSharper gained the intelligence to
>> "know about" the "Has" functions so that they can operate the same was
>> as adding a call to "string.IsNullOrEmpty(GetExtension(file))" ...
>> since the effect is nearly identical.
>>
>> Thoughts?
>>
>

0

Please sign in to leave a comment.