Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper Code Inspection Additions

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

like image 541
Gene Avatar asked Apr 26 '13 15:04

Gene


People also ask

How use ReSharper code review?

ReSharper helps you resolve most of the discovered code issues automatically. All you need is to press Alt+Enter when the caret is on a code issue highlighted in the editor and check the suggested quick-fixes.

Does ReSharper provide syntax highlighting?

Syntax highlightingReSharper extends the default Visual Studio's symbol highlighting. Additionally, it highlights fields, local variables, types, and other identifier with configurable colors. For example, ReSharper syntax highlighting allows you to easily distinguish between local variables and fields in your code.

How do I change my ReSharper settings?

To configure the main set of ReSharper preferencesIn the Visual Studio menu, choose ReSharper | Options. In the Options dialog that appears, select a node in the left pane and configure settings in the right pane. Use the search box in the left top corner to find a specific preference.


1 Answers

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!

like image 72
Gene Avatar answered Oct 13 '22 09:10

Gene