Null Checking on bool Extensions
Hi,
I tried to learn resharper the way I do the null checking, but unfortunately failed. I have extension methods on type bool like
public static void MustBeTrue<T>(this bool expression, string message, params object[] varArgs) where T : Exception, new()
{
if (!expression)
{
var errorMessage = string.Format(CultureInfo.CurrentCulture, message, varArgs);
throw (T)Activator.CreateInstance(typeof(T), errorMessage);
}
}
public static void MustBeFalse<T>(this bool expression, string message, params object[] varArgs) where T : Exception, new()
{
(!expression).MustBeTrue<T>(message, varArgs);
}
With this extensions I do null checkings like this
(node == null).MustBeFalse($"{node} must not be null");
I tried to add this pattern in Resharper
($EXPR$ == null).MustBeFalse($MESSAGE$);
But unfortunately resharper told me that there is a syntax error.
Is there a way to learn resharper this pattern?
Please sign in to leave a comment.
Hello Andre!
Thank you for contacting us.
Could you please provide screenshot of Search Pattern window?
In my case no error is indicated and the correspondent occurrence is found.
Thank you.
Hello Angelina,
in the Search Pattern Window everything works fine. I also found the the patterns.
But when I add the same expression to the null checking customization I end up with this
If I do something wrong here, I would be grateful, if you could advice me where my mistake is.
Thanks and best regards
I just discovered that I need to do it without the semicolon at the end when it's the Custom(expression) and with semicolon if it's custom(statement). Unfortunately resharper still says "Possible System.NullReferenceException"
Hello!
Thank you for the provided info.
It seems that null checking pattern feature isn't useful in the specified case. It's used for generating null checking code but not for avoiding exceptions.
I'd recommend you to try code annotations, please refer to - https://www.jetbrains.com/help/resharper/Code_Analysis__Annotations_in_Source_Code.html.
Thank you.