Resharper中的警告“未使用纯方法的返回值" [英] Warning in Resharper "Return value of pure method is not used"

查看:141
本文介绍了Resharper中的警告“未使用纯方法的返回值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于正在从我正在工作的c#项目上的Visual Studio中的Resharper收到警告,我有一个简短的问题.警告是:

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

我想我知道为什么会发生警告. 我仅将Path.GetFullPath(path)用于捕获与无效字符有关的所有异常的目的.路径将由用户作为输入提供,因此我实际上并未使用Path.GetFullPath(userInputPath)的结果.我对此方法的唯一用途是检查该方法的安全性,以确保提供的路径不为空或没有任何无效字符.

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.

我想知道此警告是否会引起任何问题,或者我是否误用了Path.GetFullPath方法,这种方式将来会导致我出现问题?

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?

推荐答案

不会,这不会给您造成任何问题,因为实际上这就是您要使用它的方式.

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

在这种情况下,Resharper提示只是一个指针,以防您忘记创建一个变量来保存已获取的数据.由于您只是在进行验证,实际上并不需要该数据,因此应该没问题.

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.

编辑:请注意,您可以避免使用提示,并通过使用特定的Resharper注释来明确这是故意的,

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


编辑#2: SynerCoder可能是正确的,关于为您的特定目的提供更好的选择...


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

这篇关于Resharper中的警告“未使用纯方法的返回值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆