If you see an issue or a bad practice in your code, but ReSharper does not detect it, you can create a custom code inspection. Additionally, you can specify code that should replace the problematic code, in other words, a custom quick-fix.
If the custom inspection is created correctly, it will be able to find all similar issues in your codebase and highlight the problematic code right in the editor.
To create custom inspections, you can harness the Structural Search and Replace mechanism.
To illustrate the procedure, let's imagine that we do not like the following code:
.. and would rather replace it by:
- In the editor, select a piece of code that should be considered as a problem.
- Right-click the selection and choose Find Similar Code.
ReSharper displays the
Search with Pattern/Edit Highlighting Pattern
dialog box with the selected code as the search pattern.
Some identifiers, arguments, types, etc. are automatically replaced with placeholders:
- To make the inspection pattern more universal, edit the recognized placeholders and replace other code items with placeholders:
To specify a quick-fix for the problem, click
in the right upper corner of the dialog box.
In the Replace pattern area, provide code that should replace the problematic code. Use the same placeholders for the variable code items.
Here is how the refined pattern and a quick-fix would look for our example:
Save and then
when finished. The custom inspection is saved in the pattern catalog.
Note that the problematic piece of code is not yet highlighted in the editor. We need to do a couple of things more to make it work.
- In the main menu, select ReSharper | Options and then open the ReSharper Options | Code Inspection | Custom Patterns page.
In the list of patterns, find the pattern that corresponds to the custom inspection you've just created.
By default its
'Do not show', which means that ReSharper will ignore the issues corresponding to your custom
To enable the inspection, choose any other severity level, e.g. 'Suggestion'.
Lastly, we can specify descriptions that will be shown for the custom inspection and the
To do so, double click the pattern and then, in the Edit Highlighting Pattern dialog box that opens, specify the descriptions in the corresponding fields:
- Save changes to the pattern and close it to get back to the Custom Patterns page of the ReSharper options.
- Click Save to apply the modifications and let ReSharper choose the settings layer, or choose a particular settings layer in the Save To drop-down list. To learn more about saving ReSharper settings, see JetBrains .NET Tools Blog: Save or Save To in ReSharper Options.
Now the problem is recognized and highlighted in the editor. The specified description appears when you hover the mouse over it:
As we have refined the inspection pattern, it will find similar problems even with different number of arguments in the constructor call or different number of statements:
The quick-fix for the custom inspection looks and works exactly the same as the built-in quick-fixes.
If you need to find the custom problem in a larger scope, you have several options:
- Choose the inspection pattern in the ReSharper Options | Code Inspection | Custom Patterns page and click Search Now on the page toolbar.
- Run code inspection in the desired scope and then analyze your custom problems with the rest of detected code issues.
- Use the Find Similar Issues action that appears in the inspection options in the editor.
- Code Analysis
- Code Inspection
- Inspection Severity Levels and Highlighting
- Marker Bar and Status Indicator
- Solution-Wide Analysis
- Quick-Fixes for Code Issues
- Solution-Wide Code Inspections
- Structural Search and Replace
- Inspecting Code in Specific Scope