#include cleanup in Visual Studio - C++ Team Blog (original) (raw)
We are gradually rolling out this feature in Visual Studio 2022 version 17.8 to monitor some data. This means that not everyone will have access to the feature immediately after its release.
#include cleanup
Visual Studio now offers #include cleanup, a feature that improves the quality of your code by generating suggestions to remove unused headers and add direct headers. Our suggested workflow is to first go through the direct include suggestions to add direct headers where indirect headers are used, followed by removing the unused includes.
Remove unused #include statements
This feature provides suggestions to remove unused headers from your files, enhancing code cleanliness. When an unused include is detected, it is visually dimmed by default. By hovering over the dimmed include, a quick action (indicated by three dots in the default view) prompt will appear, notifying you about the unused include in the file. You can click on the light bulb to remove the unused include or all unused includes. This makes it easier to clean up your code and ensures that you keep only the necessary includes to keep your code well-organized.
Gif showing remove unused includes.
Add transitively used #include statements
In Visual Studio, there is an existing feature that alerts users when an include is used but not added to the file. This is indicated by a squiggle and suggests adding the required include. Currently, we have added a new feature that provides suggestions for adding direct includes when your file has indirect dependencies. Including indirect dependencies can result in longer compilation times. However, with the help of direct include suggestions, you can optimize compilation time by including only the essential direct dependencies.
In instances where the direct include for certain content is missing, a quick action (indicated by three dots in the default view) will appear. Hovering over it will inform you that content from that transitive include is being used. Then, you have the option to either add all transitively used includes. By choosing to include 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.
Gif showing add direct include suggestion.
To quickly perform code cleanup, you can configure code cleanup by adding “Add transitively used and remove unused #include statements (C++)”. Once this setup is complete, you’ll have the convenience of adding all transitively used includes and removing all unused includes with just a single click on the broom icon. This streamlines the process and ensures that your code remains clean.
Gif displaying Configure Code Cleanup.
By default, #include cleanup is disabled, but you can enable it by navigating to Tools > Options > Text Editor > C/C++ > IntelliSense and selecting “Enable #include cleanup.” Once enabled, you have the flexibility to adjust the settings and configure different levels to meet your specific needs and preferences.
Image displaying code cleanup setting.
Customization Options
You have the flexibility to customize how the #include cleanup generates suggestions, allowing it to seamlessly adapt and integrate with intricate and extensive codebases. By making these changes or adjustments, you can ensure that the process better aligns with the specific needs and complexities of your project, resulting in a more efficient and effective code cleanup. In the EditorConfig (EditorConfig settings), you can add a single header, and if you want to include multiple headers, you should separate them using commas.
To exclude specific includes from #include cleanup suggestions, you can utilize EditorConfig. This allows you to categorize and define which files should not be considered for suggestions, providing greater control over the suggestions generated by #include cleanup. Examples of excluding single and multiple files (exclude vcruntime.h
and vcruntime_string.h
from cleanup suggestions):
cpp_include_cleanup_excluded_files = vcruntime.h
cpp_include_cleanup_excluded_files = vcruntime.h,vcruntime_string.h
Required Files
In cases where headers require other headers, and you don’t want suggestions generated, you can specify the dependencies in the EditorConfig. This ensures that required files won’t be marked as unused. Examples of single and multiple required files (atlwin.h
requires altbase.h
and atlcom.h
requires altbase.h
):
cpp_include_cleanup_required_files = atlwin.h:altbase.h
cpp_include_cleanup_required_files = atlwin.h:altbase.h,atlcom.h:altbase.h
Files Replacement
This feature also supports the remapping of facade headers. If a file is intended to represent a different file, you can redirect the usage of the first file to the usage of the second file in EditorConfig. Examples of single and multiple replaced files (replace stdio.h
by cstdio
, and replace stdint.h
by cstdint
):
cpp_include_cleanup_replacement_files = stdio.h:cstdio
cpp_include_cleanup_replacement_files = stdio.h:cstdio,stdint.h:cstdint
Alternate Files
In certain situations, you may have alternative options for including a file, such as facade files. When the usage of one file can be considered as an alternative to the usage of another file, you can specify this relationship in EditorConfig. By doing so, you can prevent #include cleanup from generating suggestions for alternate matches. Examples of single and multiple alternate files (windows.h
is an alternate for minwindef.h
and windows.h
is an alternate for winerror.h
):
cpp_include_cleanup_alternate_files = windows.h:minwindef.h
cpp_include_cleanup_alternate_files = windows.h:minwindef.h,windows.h:winerror.h
Send us your feedback!
Explore #Include Cleanup by downloading the latest version of Visual Studio Preview. We genuinely value your feedback as it plays a crucial role in shaping our development process. Please share your thoughts in the comments below, on Developer Community, or reach out to us on Twitter (@VisualC) or via email at visualcpp@microsoft.com. Your input is highly appreciated!