ReSharper 8.0 Web Help

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 create a custom code inspection with a quick-fix

To illustrate the procedure, let's imagine that we do not like the following code:

Custom inspections. Incorrect code

.. and would rather replace it by:

Custom inspections. Correct code

  1. In the editor, select a piece of code that should be considered as a problem.
  2. Right-click the selection and choose Find Similar Code.
  3. 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:

    Custom inspections. Raw inspection pattern

  4. To make the inspection pattern more universal, edit the recognized placeholders and replace other code items with placeholders:
  5. To specify a quick-fix for the problem, click Replace 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:

    Custom inspections. Refined pattern and a quick-fix for custom inspection

  6. Click Save and then Close 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.
  7. In the main menu, select ReSharper | Options and then open the ReSharper Options | Code Inspection | Custom Patterns page.
  8. In the list of patterns, find the pattern that corresponds to the custom inspection you've just created. By default its severity level is 'Do not show', which means that ReSharper will ignore the issues corresponding to your custom inspection.
    To enable the inspection, choose any other severity level, e.g. 'Suggestion'.
  9. Lastly, we can specify descriptions that will be shown for the custom inspection and the corresponding quick-fix.
    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:

    Custom inspections. Descriptions for the inspection and the quick-fix

  10. Save changes to the pattern and close it to get back to the Custom Patterns page of the ReSharper options.
  11. 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:

Custom inspections. Descriptions for the inspection and the quick-fix

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:

Custom inspections. Descriptions for the inspection and the quick-fix

The quick-fix for the custom inspection looks and works exactly the same as the built-in quick-fixes.

Custom inspections. Descriptions for the inspection and the quick-fix

If you need to find the custom problem in a larger scope, you have several options: