ReSharper Curiosity:“参数仅用于前提条件检查

本文关键字:用于 前提 条件 检查 参数 Curiosity ReSharper | 更新日期: 2023-09-27 18:30:52

为什么ReSharper评判我这段代码?

    private Control GetCorrespondingInputControl(SupportedType supportedType, object settingValue)
    {
        this.ValidateCorrespondingValueType(supportedType, settingValue);
        switch(supportedType)
        {
            case SupportedType.String:
                return new TextBox { Text = (string)settingValue };
            case SupportedType.DateTime:
                return new MonthPicker { Value = (DateTime)settingValue, ShowUpDown = true };
            default:
                throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding user control defined.", supportedType));
        }
    }
    private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)
    {
        Type type;
        switch(supportedType)
        {
            case SupportedType.String:
                type = typeof(string);
                break;
            case SupportedType.DateTime:
                type = typeof(DateTime);
                break;
            default:
                throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding Type defined.", supportedType));
        }
        string exceptionMessage = string.Format("The specified setting value is not assignable to the supported type, [{0}].", supportedType);
        if(settingValue.GetType() != type)
        {
            throw new InvalidOperationException(exceptionMessage);
        }
    }

第二种方法 ValidateCorrespondingValueType 的 "settingValue" 参数由 ReSharper 灰显并显示以下消息:"参数'设置值'仅用于前提条件检查。"

ReSharper Curiosity:“参数仅用于前提条件检查

这不是评判,而是试图帮助:)

如果 ReSharper 发现某个参数仅用作引发异常的检查,则会将其灰显,表明您实际上并未将其用于"实际"工作。这很可能是一个错误 - 为什么要传入您不打算使用的参数?它通常表示您已在前提条件下使用它,但随后忘记(或不再需要)在代码中的其他地方使用它。

由于该方法是断言方法(即,它所做的只是断言它是有效的),因此您可以使用 ReSharper 的注释属性(特别是 [AssertionMethod] 属性)将ValidateCorrespondingValueType标记为断言方法来抑制消息:

[AssertionMethod]
private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)
{
  // …
}

有趣的是,如果您在 C#6 中使用新的 nameof 功能,ReSharper 会退缩:

static void CheckForNullParameters(IExecutor executor, ILogger logger)
{
    if (executor == null)
    {
        throw new ArgumentNullException(nameof(executor));
    }
    if (logger == null)
    {
        throw new ArgumentNullException(nameof(logger));
    }
}

以下修复了该问题(在ReSharper 2016.1.1,VS2015中),但我不确定它是否解决了"正确"的问题。无论如何,它显示了ReSharper关于这个主题的机制的模糊性:

这将产生警告:

    private void CheckForNull(object obj)
    {
        if (ReferenceEquals(obj, null))
        {
            throw new Exception();
        }
    }

但这不会:

    private void CheckForNull(object obj)
    {
        if (!ReferenceEquals(obj, null))
        {
            return;
        }
        throw new Exception();
    }

有趣的是,等效代码(反转由ReSharper :D完成)给出了不同的结果。似乎模式匹配根本不会拾取第二个版本。

我的首选解决方案是让锐化者认为使用了该参数。 与使用UsedImplicitly等属性相比,这具有优势,因为如果您停止使用该参数,锐化器将再次开始警告您。 如果使用属性,锐化器也不会捕获将来的真实警告。

使锐化者认为使用了参数的一种简单方法是用一种方法替换throw。 所以而不是...

if(myPreconditionParam == wrong)
    throw new Exception(...);

。你写:

if(myPreconditionParam == wrong)
    new Exception(...).ThrowPreconditionViolation();

对于未来的程序员来说,这是一个很好的自我记录,resharper停止了抱怨。

ThrowPreconditionViolation 的实现是微不足道的:

public static class WorkAroundResharperBugs 
{
    //NOT [Pure] so resharper shuts up; the aim of this method is to make resharper 
    //shut up about "Parameter 'Foobaar' is used only for precondition checks" 
    //optionally: [DebuggerHidden]
    public static void ThrowPreconditionViolation(this Exception e)
    {
        throw e;
    }
}

异常上的扩展方法是命名空间污染,但它是相当包含的。

其他人已经回答了这个问题,但没有人提到以下关闭警告的方法。

在方法签名上方添加此项以仅关闭该方法:

    // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local

在类声明上方添加以下内容以关闭整个文件:

     // ReSharper disable ParameterOnlyUsedForPreconditionCheck.Local
我相信

下面是一个合法的情况,我使用 lambda Any方法检查列表中的所有项目。然后我使用相同的方式下一行,但锐化器失败。

if (actions.Any(t => t.Id == null)) // here says Paramter t is used only for precondition check(s)
    throw new Exception();
actionIds = actions.Select(t => t.Id ?? 0).ToList();

我不得不忽略评论。

在参数上方使用此注释

ReSharper 禁用一次 ParameterOnlyUsedForPreconditionCheck.Local

以禁用附加条款中的建议