ukod
ukod

Reputation: 149

Why I getting an "Expression always true" warning?

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.

Upvotes: 5

Views: 6298

Answers (4)

Perfect28
Perfect28

Reputation: 11317

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

}

Upvotes: 9

user1023602
user1023602

Reputation:

The compiler is clever and spotted your mistake, then it warned you about it.

 if (varKnownFolder == null)
 {
     // null
 }
 else
 {
     // *** NOT null ***
     if (varKnownFolder != null) _FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
     else objFolder = null;
}

So what you wrote is equivalent to

  if (varKnownFolder == null)
  {
      objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
  }
  else
  {
      _FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
  }

Upvotes: 3

Matt
Matt

Reputation: 1678

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) { }
}

Upvotes: 6

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11398

        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.

Upvotes: 3

Related Questions