"Parameter X is only used for precondition check(s)" - what is this?

ReSharper seems to think there is no use case for having a validation method like so:

     private static void ValidatePdfOptions(PdfOptions options)      {             if(options.Sections == null || options.Sections.Length == 0)                throw new Exception("At least one PdfSectionOptions instance must be supplied");           if(options.Sections.Any(o => string.IsNullOrWhiteSpace(o.DocumentHtml)))                throw new InvalidOperationException("DocumentHtml cannot be null or whitespace");      }



ReSharper says: Parameter 'options' is only used for precondition check(s)

I don't want to put this method on the class because the validation I'm applying is only applicable under certain conditions. Why does ReSharper care whether or not I have a method like this, and why does it want to turn off all "unused parameter" warnings in order to disable this warning? The parameter is not unused as if I were to remove it, the code obviously wouldn't compile, yet if I turn this warning off and have an actual unused parameter, ReSharper won't warn me about it.

Could someone please explain what I'm missing here?
3 comments
Comment actions Permalink

Hello Nathan
     Here ReSharper only tells you that the parameter is only used in some assertion checks. This can indicate a problem if, for instance, your method is supposed to do something other with that parameter besides precondition checks. If you're using this method only as an assertion, you can mark it with 'UsedImplicitly' attribute from ReSharper annotations. You can find more information at http://www.jetbrains.com/resharper/webhelp/Code_Analysis__External_Annotations.html.

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

1
Comment actions Permalink

the problem is it raises the error for unittest assertion helper methods

ie it should not count "NUnit.Framework.Assert.IsTrue" as a precondition check

2
Comment actions Permalink

I also think this heuristics should be refined.

It often happens that we extract some common checks into their own method, which then just contains checks. Those methods (doing several checks, and have either "Check" or "Validate" or "Assert" in their name) should not trigger this warning.

0

Please sign in to leave a comment.