ReSharper 10.0 Help

Creating Custom Code Inspections and Quick-Fixes

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 creating custom inspection, let's imagine that we do not like the following code:

string line; var tr = new MyReader(); try { line = tr.Read(); } finally { tr.Dispose(); }
.. and would rather replace it with:
string line; using(var tr = new MyReader()) { tr.Read(); }

To create a custom code inspection with a quick-fix

  1. In the editor, select a piece of code that should be considered as a problem.
  2. Right-click the selection and choose Search with Pattern.
  3. ReSharper displays the Search with Pattern dialog box with the selected code pre-parsed as the search pattern. Some identifiers, arguments, types, etc. are automatically replaced with placeholders:
    ReSharper's custom code inspections. Raw inspection pattern
  4. To make the inspection pattern more universal, edit the recognized placeholders: and replace other code items with more placeholders. For instance, we can replace the single statement in the try block in our example, with a placeholder that matches any number of statements.
  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, specify a pattern that should be used to 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:
    ReSharper's 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 using the smart save logic.
    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 go to the 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:
    ReSharper's custom inspections. Descriptions for the inspection and the quick-fix
  10. Save changes to the pattern, close it, and then click Save again on the Custom Patterns page of the ReSharper options.

Now the problem is recognized and highlighted in the editor. The specified description appears when you hover the mouse over it:

ReSharper's 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:
ReSharper's 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.

ReSharper's custom inspections. Descriptions for the inspection and the quick-fix

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

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts Protobuf
feature_available feature_available feature_available feature_available feature_available feature_available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

See Also

Last modified: 8 January 2016