Parameter 'x' is only used for precondition check(s)

I've already found some threads about this message, and the answer usually is to decorate the method with [AssertionMethod] or decorate the parameter with [UsedImplicitly].

However, I don't think I understand the concept itself.

I'm programming with Dynamic Data, which uses the Entity Data Model. I try to create a partial method for custom validation, as described in section "Customizing Validation for All Data Fields by Using a Partial-Class Method in the LINQ-to-SQL Data Model" on this page.

The code is pretty straight forward.

partial void OnUnitPriceChanging(decimal? value)
{
    if (UnitsInStock > 0 && UnitPrice > value && !Discontinued)
        throw new ValidationException("Cannot lower the price of an item that is discontinued.");
}


ReSharper gives a warning on parameter value only being used for precondition check(s).

I would only like to decorate the method with [AssertionMethod] if it actually is an assertion method. In this case, the method's job is to validate the unit price of a product. And if it's invalid, the method should throw a ValidationException. I don't think that's an assertion. I don't expect the expression to always be false. I'm checking it because I expect the user could enter invalid data.

I noticed I can make the warning go away by changing my code into this.

partial void OnUnitPriceChanging(decimal? value) {     if (!(UnitsInStock > 0 && UnitPrice > value && !Discontinued)) return;     throw new ValidationException("Cannot lower the price of an item that is discontinued."); }



I'd say I'm still only using the parameter for precondition checks. But in this code, ReSharper sees no problem. Does ReSharper only consider it a precondition if it throws an exception?

I guess my question is to explain the reasoning ReSharper is taking to determine if a parameter is only used for precondition checks.
5 comments
Comment actions Permalink

Too bad I can't edit my post. In meant "Customizing Validation for an Individual Data Field by Using a Partial-Class Method" in stead of "
Customizing Validation for All Data Fields by Using a Partial-Class Method in the LINQ-to-SQL Data Model".

0
Comment actions Permalink

Hello Dick,

Thank you for the detailed description of this behavior.
You're absolutely correct in your description; this case you described is a false-positive analysis. Obviously, in 90% cases Resharper will be correct with this warning, however it cannon detect this 'special Entity Framework method'. As a result you'll get a false-positive in this kind of methods.

I would recommend you to supress this particular inspection with comment if you don't wish to change the source code.

We apologize for the inconveience.
Thanks.

0
Comment actions Permalink

I've been using the code annotations by referencing JetBrains.Annotations.dll in the ReSharper installation directory, as suggested on this page. At the time I did that, I was using R# 7.0.

Today I opened up my solution and couldn't build it. That's because I'm currently using R# 7.1, which is installed in a different directory. So I'd say referencing the dll in the installation directory is bad advice.

0
Comment actions Permalink

Hi Dick,

You may copy the *.dll to your solution folder or 'Copy Default Implementation' of Annotations to clipboard (in the ReSharper | Options | Code Inspection | Code Annotations dialog) to avoid this happening in the future.
Hope this helps.

Thank you!

0
Comment actions Permalink

Hi Alex,

Yes, that helps. I just meant to say it would be wise to mention this on the page about annotations. I think it is bad advise to reference the dll that is in the installation folder.

0

Please sign in to leave a comment.