Tidy and Tidy-Fix: The Easy Way to Keep Your Code Optimized and Clean
Clang-Tidy is an open source code analysis tool for C++ that helps developers identify and fix coding errors and security vulnerabilities. With Clang-Tidy, you can detect and correct potential coding issues more quickly and accurately than ever before.
In this blog, we'll explore how to use Clang Power Tools to maximize your Clang-Tidy experience. We'll walk through how to use the Tidy Tool Window to analyze your files, as well as the performance differences between running the Tidy command from the context menu or from the Tidy Tool Window
- Run Tidy command
- Use Tidy to Analyze Your Files
- Apply improvements with Tidy-Fix
- Bypassing Tidy Tool Window
- Performance Differences between refresh Tidy/Fix and Tidy/Tidy-Fix
Run Tidy command
Tidy offers a wide range of powerful checks that can help you identify and fix coding errors and security vulnerabilities. You can use the predefined flags or create a custom .clang-tidy file for your project. You can explore how to automatically detect your .clang-tidy file.
If you need to access Tidy for your solutions or projects, the context menu in Clang Power Tools provides a quick and convenient option. On the other hand, if you want to run Tidy on the active document, you can use the Clang Power Tools Toolbar Options available in Visual Studio.
💡 To make sure that any warnings, errors, or messages appear in the Error List, ensure that you have the appropriate settings enabled. See the image below for a visual reference.
Use Tidy to Analyze Your Files
Let's walk through an example of Tidy execution in this section. To begin, we'll need to make some configurations in the Predefined Checks. Here are the steps to follow
- To find Tidy checks, you'll need to open the Settings and navigate to the Tidy tab. From there, select Use checks from Predefined Checks and click the 'Select' button next to it. This will open a list of available checks that you can choose from.
- You can toggle the Default button if you want to use the default checks. For this example, we'll use the modernize-null-ptr check. You can click on the description to view additional information about the selected check.
- We'll use the same examples described in the modernize-null-ptr check. After running Tidy, you'll see two warnings displayed in the Error List.
Apply improvements with Tidy-Fix
After running Tidy on a project or a file, a Tidy Tool Window will automatically open, you may notice in your codebase that there are certain areas that require improvement. This is where Tidy-Fix comes in handy. Tidy-Fix provides an easy way to apply the suggested improvements to your codebase directly within the Clang Power Tools Tidy Tool Window.
To apply fixes, simply click on the 'Fix' button located in the Tidy Tool window. This will open a new window displaying the suggested fixes that Tidy has identified. From here, you can choose to apply the fixes individually or all at once.
To review the changes made by Tidy-Fix, you can click on the 'Diff' button within the Tidy Tool Window. This will open a new window displaying a side-by-side comparison of the before and after versions of your code, allowing you to see exactly what changes have been made.
If you're not satisfied with the changes, you can easily revert back to the previous version of your code by clicking the 'Discard' button in the Tidy Tool Window. This will undo the changes made by Tidy-Fix and restore your code to its previous state.
With this feature, you can experiment with different fixes and easily switch back and forth between versions, giving you more control over the changes made to your codebase.
Bypassing Tidy Tool Window
To activate the 1-step Tidy-Fix option, simply navigate to the Clang Power Tools settings and enable it. Once enabled, you can run Tidy-Fix on your entire solution directly from the context menu.
If you're using version control, this may not be an issue, as you can always revert back to a previous version if needed. However, if you're not using version control, it's recommended to use Tidy-Fix with caution and make a backup of your code before running it.
💡 If you're planning to run Tidy-Fix on your entire codebase, it's important to note that the tool will make changes to your code!
Performance Differences between Refresh Tidy/Fix and Tidy/Tidy-Fix
The Refresh Tidy command in the Tidy Tool Window is designed to be used after you've applied fixes or made changes to your codebase. By running Refresh Tidy, you can re-analyze the code to see if there are any remaining issues that need to be addressed. This allows you to review and make any necessary changes to ensure the code remains clean and maintainable.
Is there any distinction between running Tidy/Tidy-Fix on the entire solution from the context menu or from the Tidy tool window (with refresh Tidy/Fix)?
Yes, there are performance differences. If you have a heavy solution, running the command from the context menu is the best option. This approach is particularly useful for heavy solutions, as the script can execute the analysis in parallel for multiple files, further reducing the analysis time.
Running Refresh Tidy/Fix from the Tool Window will cause a longer delay, as the clang-build script is invoked for each file individually.
If you have any suggestion or feedback report, we highly encourage you to let us know on our GitHub. Until then, happy coding!