Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I getting an "Expression always true" warning?

Tags:

c#

It's code where warning arises:

public async Task<Metadata> GetFolderAsync(string strDirectoryPathName, dynamic varKnownFolder = null)
{
    using (await _FolderPathToInfoMapSync.EnterAsync().ConfigureAwait(false))
    {
        Metadata objFolder;
        string strPathName = strDirectoryPathName;
        if (varKnownFolder == null)
        {
            objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
        }
        else
        {
            //Here warning arises
            if (varKnownFolder != null) _FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
            else objFolder = null;
        }
    }
    return objFolder;
}

I mean reason for this is a varKnownFolder in method signature, but I don't understand what is wrong here.

like image 526
ukod Avatar asked Nov 10 '15 10:11

ukod


3 Answers

If your code enters the first else block it means that (varKnownFolder == null) was evaluated to false.

So the second check is useless as varKnownFolder could never be null in this block.

public async Task<Metadata> GetFolderAsync(string strDirectoryPathName, dynamic varKnownFolder = null)
{
     using (await _FolderPathToInfoMapSync.EnterAsync().ConfigureAwait(false))
     {
           Metadata objFolder;
           string strPathName = strDirectoryPathName;

           if (varKnownFolder == null)
           {
               // This would happen if varKnownFolder is null 

               objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
           }
           else
           {
               // The code enters HERE BECAUSE varKnownFolder is not null

               if (varKnownFolder != null) // <-- So this check is useless
                   _FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
               else 
                  objFolder = null;
            }

            return objFolder;
      }

}

Also, it means that you could replace it with this :

public async Task<Metadata> GetFolderAsync(string strDirectoryPathName, dynamic varKnownFolder = null)
{
     using (await _FolderPathToInfoMapSync.EnterAsync().ConfigureAwait(false))
     {
           Metadata objFolder = null;
           string strPathName = strDirectoryPathName;

           if (varKnownFolder == null)
           {
               objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
           }
           else
           {
               // The code enters HERE BECAUSE varKnownFolder is not null
               _FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
            }

            return objFolder;
      }

}
like image 163
Perfect28 Avatar answered Nov 10 '22 05:11

Perfect28


Because you are checking the inverse of the same condition within a block that has already been asserted by the outer if statement. Logically your code is the equivalent of...

if(x==false)
{

}
else // x must be true here
{
    if(x==true) { }
}
like image 32
Matt Avatar answered Nov 10 '22 06:11

Matt


        if (varKnownFolder == null)
        {
        }
        else
        {
            //You already know it is not null (because of the "if" check)
            if (varKnownFolder != null)
        }

Your "if" statement checks if you have a null object.
Inside your "else", varKnownFolder can't be null.

I mean reason for this is a varKnownFolder in method signature

Nope, the varKnownFolder = null inside method signature just means that, if you call this method without varKnownFolder, it will be null.

like image 3
Deblaton Jean-Philippe Avatar answered Nov 10 '22 04:11

Deblaton Jean-Philippe