Visual Studio 2022 17.7 Preview 3 Introduces #include Cleanup

2023/07/12
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

Visual Studio 17.7 Preview 3 introduces a new feature aimed at enhancing developers' productivity. The #include cleanup tool helps maintain clean code by generating suggestions to remove unused headers and add direct headers. To start using this feature, developers need to update to the latest version of Visual Studio Preview.

The #include cleanup feature in Visual Studio offers suggestions to remove unused headers from files, enhancing code cleanliness. When an unused include is detected, it is visually dimmed by default. Developers can hover over the dimmed include to see a quick action prompt, notifying them about the unused include in the file. Clicking on the light bulb allows them to remove the unused include or all unused includes, making it easier to clean up code and ensure that only necessary includes are kept.

In addition to removing unused includes, Visual Studio now provides suggestions for adding direct includes when a file has indirect dependencies. Including indirect dependencies can result in longer compilation times, but with the help of direct include suggestions, developers can optimize compilation time by including only the essential direct dependencies.

When a direct include for certain content is missing, a quick action prompt will appear, informing developers that content from that transitive include is being used. They can choose to add the direct include individually or add all transitively used includes. By including all transitively used includes, all the direct headers will be automatically added wherever indirect headers are used in the file. After including the direct dependencies, it is crucial to remove any unused includes, retaining only those that are necessary.

To streamline the code cleanup process, developers can configure code cleanup by adding "Add transitively used and remove unused #include statements (C++)". Once this setup is complete, they can add all transitively used includes and remove all unused includes with just a single click on the broom icon. This ensures that code remains clean and organized.

By enabling the #include cleanup feature in Visual Studio, developers can easily maintain clean code by removing unused headers and adding direct headers. This feature helps optimize compilation time and ensures that only necessary includes are kept, improving code cleanliness and overall productivity.