質問

Is it possible to extend resharper code inspection/annotations to handle cases you know staticly are correct?

For example, I have utility function I know satisfy certain conditions, such as:

    static public bool IsValid(double? d)
    {
        return d != null && IsValid(d.Value);
    }
    static public bool IsValid(double d)
    {
        return !Double.IsNaN(d) && !Double.IsInfinity(d);
    }

So this ensures a nullable has a value, and I'd like the "Possible System.InvalidOperationException" inspection not to fire for something like:

    if (Utils.IsValid(nullableValue))
    {
        DoSomethingWith(nullableValue.Value);
    }

Sure I could suppress the inspection/etc, but is it possible to extend the static typing to indicate that this would actually ensure the value is non-nullable?

(I suppose a related but overly general question is should I be using another static typing check instead of resharper that might handle it, but I won't ask for fear of being overly broad!)

役に立ちましたか?

解決

Per Daniel's suggestion, resharper supports a good deal of annotations to assist with inspection.

Specifically, via the documentation what we're looking for here is something like:

    [ContractAnnotation("d:null => false")]
    static public bool IsValid(double? d)
    {
        return d != null && IsValid(d.Value);
    }

Which does the trick perfectly, and the static check works beautifully.

Love that resharper!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top