ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Create custom code inspections and quick-fixes

Last modified: 25 September 2024

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:

.. and would rather replace it with:

Now the problem is recognized and highlighted in the editor. The specified description appears when you hover 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:

  • Choose the inspection pattern on the Code Inspection | Custom Patterns page of ReSharper options 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 when you press AltEnter on the highlighted code in the editor.

This feature is supported in the following languages and technologies:

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the Languages and frameworks section.