Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning in Resharper "Return value of pure method is not used"

I have a quick question regarding a warning that I am getting from Resharper in Visual studio on a c# project that I am working. The warning is:

" Return Value of pure method is not used"

The method where this is happening is as below:

 private static bool FilePathHasInvalidChars(string userInputPath)
    {
        try
        {
            //this is where the warning occurs:
            Path.GetFullPath(userInputPath);

        }
        catch (Exception e)
        {
            Log.Error(String.Format(
                "The Program failed to run due to invalid characters or empty " + 
                "string value for the Input Directory. " + 
                "Full Path : <{0}>. Error Message : {1}.",
                userInputPath, e.Message), e);
            return true;

        }
        return false;
    }

I think I know why the warning is happening. I am using Path.GetFullPath(path) only for the purpose of catching all exceptions to do with invalid characters. The path is to be supplied as input by the user therefore I do not actually use the result of the Path.GetFullPath(userInputPath). The only use I have for it is on a check that I have for this method is on a check that I do on the main method to ensure the path supplied is not empty or does not have any invalid characters.

The place where I use the above method is as below:

if (FilePathHasInvalidChars(inputDirectory))
{
     return;
}

Basically it is just an exit point before program commences execution using an invalid parameter.

I was wondering if this warning would cause any issues or if I am misusing the Path.GetFullPath method in a way which will cause me problems in the future?

like image 946
Jetnor Avatar asked Dec 16 '13 08:12

Jetnor


3 Answers

Nope, that should not cause any problems for you, as this is in fact how you do want to use it.

The Resharper hint in this case is just a pointer in case you've forgotten to create a variable in which to keep the data you've fetched. Since you're just validating, and don't actually need that data, you should be fine.

Edit: Note that you can avoid the hint, and make it clear that this is on purpose by using a specific Resharper comment, like this:

// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
Path.GetFullPath(userInputPath);

Edit #2: SynerCoder is probably right though, about System.IO.Directory.Exists() being a better option for your specific purpose...

like image 58
Kjartan Avatar answered Oct 20 '22 09:10

Kjartan


In your sample code you catch Exception which can be any of the following: ArgumentException, SecurityException, ArgumentNullException, NotSupportedException, PathTooLongException, but the one that is being thrown when path contains invalid characters is only ArgumentException MSDN.
Furthermore,

I am using Path.GetFullPath(path) only for the purpose of catching all exceptions to do with invalid characters.

you should rather use the following code, and omit the exception handling:

foreach (char invalidChar in Path.GetInvalidPathChars())
{
    if (userInputPath.Contains(invalidChar))
    {
        return true;
    }
}
return false;
like image 29
Yurii Avatar answered Oct 20 '22 11:10

Yurii


You should not use your own method for checking if the path is illegal. Since you are checking a directory (inputDirectory) you should use the following code:

if (!System.IO.Directory.Exists(inputDirectory))
{
    return;
}
like image 5
SynerCoder Avatar answered Oct 20 '22 11:10

SynerCoder