ReSharper Curiosity: "Parameter is only used for precondition check(s)."
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)
{
// …
}
Interestingly, ReSharper backs off if you use the new nameof
functionality in C#6:
static void CheckForNullParameters(IExecutor executor, ILogger logger)
{
if (executor == null)
{
throw new ArgumentNullException(nameof(executor));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
}
The following fixes the issue (in ReSharper 2016.1.1, VS2015), but I am not sure it solves the 'right' problem. In any case, it shows the ambiguity in ReSharper's mechanics regarding this topic:
This yields the warning:
private void CheckForNull(object obj)
{
if (ReferenceEquals(obj, null))
{
throw new Exception();
}
}
But this does not:
private void CheckForNull(object obj)
{
if (!ReferenceEquals(obj, null))
{
return;
}
throw new Exception();
}
It is interesting that equivalent code (the inversion was done by ReSharper :D) gives different results. It seems that the pattern matching simply does not pick up the second version.