Finding uses of the "null-forgiving" operator
I'm trying to find uses of the “null-forgiving” operator (aka “damnit operator”, or “Janet Weiss operator”) in my project.
Obviously a simple text-based search won't work, since “!” can also be used for the boolean negation operator, or as part of the not-equal-to operator, or within strings…
I can't see anything built-in to Visual Studio. I've tried the Nullable.Extended add-in, but it seems to get stuck on “analyzing” for ages.
Since R# already warns about redundant use of the operator, does it have a tool I could use to find every use? Or is it something that could be added to a future version?
Please sign in to leave a comment.
thank you for contacting us.
Have you tried to VS built-in find?
1. press Ctrl+F to open Quick Find window
2. press Alt+E to use regular expressions
3. insert the following regular expression '.?'
All "null-forgiving" operators will be highlighted.
Thank you!
That won't work.
Firstly, “?” is the null-conditional operator; I'm looking for the “null-forgiving” operator, which is “!”.
Both operators have multiple uses - “?” in a ternary expression, and “!” as the boolean negation operator and the not-equals operator. They can also appear within string literals.
And the regular expression “.?” would find every occurrence of zero or more runs of any character - combing through that to find uses of the null-forgiving operator would be worse than combing through the original code!
The closest regex I can think of would be:
[a-zA-Z0-9\]]\!That should identify a “!” that immediately follows an identifier or indexer. But it would still pick up instances in strings, and could potentially still catch the not-equals operator if the code isn't formatted “properly” with spaces around the operator.
R# obviously knows how to identify the null-forgiving operator - it highlights any that it thinks are redundant. I was hoping there was some way to use that knowledge to easily find them all.
thank you for the answer.
Sorry for misspelling, I meant '.!' not '.?'.
Anyway, I've created a feature request: https://youtrack.jetbrains.com/issue/RSRP-498969/Add-a-possibility-to-find-uses-of-the-null-forgiving-operator
You are welcome to comment on it and vote.
Thank you!