Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper Curiosity: "Parameter is only used for precondition check(s)."

Why is ReSharper judging me for this code?

    private Control GetCorrespondingInputControl(SupportedType supportedType, object settingValue)     {         this.ValidateCorrespondingValueType(supportedType, settingValue);          switch(supportedType)         {             case SupportedType.String:                 return new TextBox { Text = (string)settingValue };             case SupportedType.DateTime:                 return new MonthPicker { Value = (DateTime)settingValue, ShowUpDown = true };             default:                 throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding user control defined.", supportedType));         }     }      private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)     {         Type type;          switch(supportedType)         {             case SupportedType.String:                 type = typeof(string);                 break;             case SupportedType.DateTime:                 type = typeof(DateTime);                 break;             default:                 throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding Type defined.", supportedType));         }         string exceptionMessage = string.Format("The specified setting value is not assignable to the supported type, [{0}].", supportedType);         if(settingValue.GetType() != type)         {             throw new InvalidOperationException(exceptionMessage);         }     } 

The second method ValidateCorrespondingValueType's "settingValue" parameter is grayed out with the following message by ReSharper: "Parameter 'settingValue' is only used for precondition check(s)."

like image 635
Corpsekicker Avatar asked Nov 19 '14 06:11

Corpsekicker


1 Answers

It's not judging, it's trying to help :)

If ReSharper sees that a parameter is only used as a check to throw an exception, it greys it out, indicating that you're not actually using it for "real" work. This is most likely a mistake - why pass in a parameter you're not going to use? It usually indicates that you've used it in a pre-condition, but then forgotten (or no longer need) to use it elsewhere in the code.

Since the method is an assertion method (that is, all it does is assert it's valid), you can suppress the message by marking the ValidateCorrespondingValueType as an assertion method, using ReSharper's annotation attributes, specifically the [AssertionMethod] attribute:

[AssertionMethod] private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue) {   // … } 
like image 70
citizenmatt Avatar answered Sep 21 '22 12:09

citizenmatt