c# - How to indicate to R# that a function checks a variable for null -


in our codebase, have battery of custom error checking functions (like listed here) check arguments less verbosely. example, check argument null use:

throw.ifnull(theargument, "theargument"); 

the 1 disadvantage of approach r# gives warning "possible nullreferenceexception" on future uses of value because it's not smart enough detect null check (or @ least fail if theargument null). there way indicate method checks against argument being null? example, when try run static extension select() on such value, r# warns me of 'possible null assignment entity marked notnull attribute', can't find documentation of such attribute nor see in reference source enumerable.select().

what you're asking can solved applying resharper annotations! attributes provide additional hints resharper's analysis, allowing add resharper "goodness" onto own methods , classes. have recorded webinar jetbrains called resharper secrets talk , demonstrate annotations, you're welcome watch it!

as question, there 3 annotation attributes can apply solve issues (and add more cool features).

supposing definition ifnull like:

public static class throw {     public static void ifnull<t>(t parameter, string parametername) t : class     {         if (parameter == null)              throw argumentnullexception(string.format("parameter {0} null", parametername));     } } 

you can decorate 3 resharper attributes, contractannotation, notnull , invokerparametername this:

[contractannotation("parameter: null => halt")] public static void ifnull<t>([notnull] t parameter,                              [invokerparametername] string parametername)      t : class {     ... } 

here attributes do:

the first, [contractannotation], tells resharper if parameter heuristically null, method halts program execution, i.e. throws exception (at runtime). prevents "possible nullreferenceexception" warning. language used defining contract annotations explained here.

the second [notnull], tells resharper parameter must not heuristically null. gives "possible null assignment entity marked [notnull] attribute" warning.

the third, [invokerparametername] telling resharper parametername argument name of 1 of parameters calling (invoking) method, provide code completion lists calling method parameters. give warning in resharper if name not parameter, example, local variable name.

here's short video of these attributes in action (applied set of apis, idea same): http://screencast.com/t/nhgvaur7go3b


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -