问题
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?
回答1:
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...
回答2:
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;
回答3:
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;
}
来源:https://stackoverflow.com/questions/20606022/warning-in-resharper-return-value-of-pure-method-is-not-used