GetFolderAsync(string st...">

Why am I getting the warning "Expression always true"?

Code in which the warning occurs:

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 the reason this is the varKnownFolder sign in the method, but I don’t understand what is wrong here.

+4
source share
4 answers

If your code is in the first else block, it means it (varKnownFolder == null)evaluates to false.

So, the second check is useless, since varKnownFolderit can never be zero 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;
      }

}

In addition, this means that you can replace it with the following:

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

}
+9
source

Because you are checking the call to the same condition in a block that has already been confirmed by an external if. Logically, your code is equivalent ...

if(x==false)
{

}
else // x must be true here
{
    if(x==true) { }
}
+5
source
        if (varKnownFolder == null)
        {
        }
        else
        {
            //You already know it is not null (because of the "if" check)
            if (varKnownFolder != null)
        }

"if" .
"else" varKnownFolder .

I mean the reason this is the varKnownFolder sign in the method signature

No, the signature varKnownFolder = nullinside the method simply means that if you call this method without varKnownFolder, it will be zero.

+3
source

The compiler is smart and noticed your error, and then warned you about it.

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

What you wrote is equivalent

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

Source: https://habr.com/ru/post/1615219/


All Articles