We think you will agree that the code analysis provided by ReSharper is really cool and helps create better code. What is even cooler is that ReSharper provides you with features that can fix some issues automatically.
Most errors and issues found by ReSharper can be fixed just by pressing Alt + Enter. This will display a list of the available solutions and lets you select the best one for you.
The quick fixes we just described allow you to fix the issues in one particular place. However, sometimes there are issues that you would like to fix in every file in your project or solution. A great example is removing unused using
statements or the this
keyword.
With ReSharper v8, you do not need to fix such issues manually. Instead, you can use a new feature called Fix in scope. You start as usual by pressing Alt + Enter but instead of just selecting a solution, you can select more options by clicking the small arrow on the right from the available options.
A sample usage of the Fix in scope feature is shown in the following screenshot:
This will allow you to fix the selected issue with just one click!
Even though ReSharper contains a lot of built-in analysis, it also allows you to create your own analyses. You can create your own patterns that will be used to search some structures in your code. This feature is called Structural Search and Replace (SSR).
To open the Search with Pattern window, navigate to RESHARPER | Find | Search with Pattern…. A sample window is shown in the following screenshot:
You can see two things here:
In the preceding example, we were looking for if
statements to compare them with a false
expression.
You can now simply click on the Find button and ReSharper will display every piece of code that matches this pattern. Of course, you can also save your patterns.
SSR allows you not only to find code based on defined patterns, but also replace it with different code. Click on the Replace button available on the top in the preceding screenshot. This will display a new section on the left called Replace pattern. There, you can write code that will be placed instead of code that matches the defined pattern.
For the pattern shown, you can write the following code:
if (false = $value$) { $statement$ }
This will simply change the order of expressions inside the if
statement.
The saved patterns can also be presented as Quick fixes. Simply navigate to RESHARPER | Options | Code Inspection | Custom Patterns and set the right severity for your pattern, as shown in the following screenshot:
This will allow you to define patterns in the code editor, which is shown in the following screenshot:
ReSharper also allows you to fix more than one issue in one run. Navigate to RESHARPER | Tools | Cleanup Code… in the Visual Studio toolbar or just press Ctrl + E, Ctrl + C. This will display the Code Cleanup window, which is shown in the following screenshot:
By clicking on the Run button, ReSharper will fix all issues configured in the selected profile. By default, there are two patterns:
You can add your own pattern by clicking on the Edit Profiles button.
3.145.107.100