Bringing Code Cleanup on Save To Visual Studio 2022 17.1 Preview 2

Denizhan Yigitbas

Starting in Visual Studio 2022 17.1 Preview 2, developers can now perform Code Cleanup automatically when a file is being saved! Code Cleanup automatically on Save is a new feature integrated into Visual Studio 2022 that can clean up your code file to make sure it is formatted correctly and that your coding style preferences are applied. Some customizable preferences include: format document, sort usings, remove unnecessary usings, and more. This feature can help minimize stylistic violations within PRs and more to allow developers to spend less time fixing code not meeting specific standards and more time doing what they do best. To give it code cleanup on save a try, download Visual Studio 2022 Preview:

Moving image demonstrating code cleanup on save

How to enable Code Cleanup on Save

First, navigate to Analyze > Code Cleanup > Configure Code Cleanup to personalize desirable customizations to your code cleanup profile(s).

Screenshot for configuring code cleanup profiles

Next, navigate to Tools > Options > Text Editor > Code Cleanup. Add a check in the “Run Code Cleanup profile on Save”. Be sure to select the appropriate profile you want to execute automatically whenever you save!

Screenshot of dialogue to enable code cleanup on save

 

Try it out and share your feedback!

We would love to get your feedback on Code Cleanup on Save so please give it a try and let us know what you think! You can share any feedback via Developer Community to help us make Visual Studio better for you!

 

22 comments

Discussion is closed. Login to edit/delete existing comments.

  • Josiah Bills 0

    I am quite looking forward to using this feature, but I am having trouble getting this to work with clang-format and C++. Any idea how to get it working?

    • Dante GagneMicrosoft employee 0

      Unfortunately, C++ doesn’t support the Code Cleanup feature yet, and until it does, Code Cleanup on Save isn’t going to work.

      • Josiah Bills 0

        All right, thanks! Is there a devcommunity feedback item tracking this I can upvote?

        • Dante GagneMicrosoft employee 0

          Not that I’ve been able to find. I would encourage you to create one yourself since one doesn’t already exist. If you create it, the PM team will be able to ask you questions if they have any as well as you’ll get updates as progress is being made.

          https://developercommunity.visualstudio.com/

      • Roger B 0

        Indeed, waiting for the c++ support here… In fact, just including “Format document” support will get you most of the way there. Though I wouldn’t mind some clang-tidy fixes too (it seems like the tooling api from msvc itself won’t happen so have to stick with clang toolsets for now).

  • Reilly Wood 0

    This is a nice feature but it’s unclear+confusing how Code Cleanup interacts with EditorConfig.

    • Mika DumontMicrosoft employee 0

      Hi Reilly, Thanks for providing this feedback! We are currently working on making this experience better which you can track here.

  • Eli Black 0

    Is it possible to export the code cleanup configuration and save it to a file? We’d like to be able to share a common set of rules between all of our developers.

    Currently we accomplish this with the CodeMaid extension, but it might be nice for this to be backed into Visual Studio 🙂

    • Mika DumontMicrosoft employee 0

      Yes, this is already part of the plan which you can track here.

      • obama koffi 0

        cool

  • Alex MovsessianMicrosoft employee 0

    Bug – Repro Steps

    Open the Configure Code Cleaup window (Doesn’t matter profile 1 or 2)

    Add from Available fixers to Included fixers using the up arrow button.

    Press OK and close the window.

    When you open it again, you’ll find nothing was added :/

    This is basically preventing us from using the feature.

    Remark

    Unless we can save the profile as part of the project, in a manner similar to how .editorconfig file works, we can’t use it across the team efficiently.

  • Martin Ba 0

    Automatic code formatting and cleanup is great — but only if the whole team uses exactly the same settings for the edited code base.

    How does Visual Studio Code Cleanup support this? Is it possible to check-in some settings into source control (á la .editorconfig) that will be picked up so that these settings are “correct” for all team members on a given code base state?

    Nothing is more annoying that playing ping pong with code formatting rules between different team members 🙂

  • Fan Zhang 0

    Can’t wait for this to release! Can this be saved together with csproj or sln file so that it gets automatically applied no matter who opens the project or solution? Would be super super great to have a way making the same profile work for all team members.

  • Evans, Paul (Secure Solutions & Services) 0

    Is there a risk of this breaking stuff?

    I note the demo removed “unusedVariable” – what if that wasn’t “unusedVariable”, but “soonToBeUsedVariable”?

    Does this also do some of the automagic fixes that Ctrl+. comes up with? I’ve had some strange things from there where the fix has removed half of the line to meet some “Hey! This is a good idea!” rule, but has just messed the line up.
    Related, does it do the automatic “You changed X to Y in half the document – do you want to do it here?” suggestions? Sometimes useful, sometimes not – if I copy the code foe Thing1 and update the copy to use Thing2, I don’t want to change the first bit that works with Thing1.

    Doing an automatic Ctrl+K, Ctrl+D to deal with indents and stuff like that is neat (until saving a legacy file and having a PR made up of 2 lines of real code change and 1000 lines of “I moved your braces and fixed tabs” – automatic removal of code is making my spidey sense tingle a little

  • Giovanni Bassi 0

    That is great, I used the “Format document on Save” extension to do this.
    Will there be a way to save without formatting? This is something the developer added to that extension, and that is also available in vscode.

  • Conor Mendenhall 0

    I’m not seeing Code Cleanup under my Text Editor settings in Visual Studio 2022 version 16.11.10
    I also have Resharper installed. Could that be conflicting with this somehow?
    I am able to run code cleanup, but would love to set it to automatically occur on save.

    • Conor Mendenhall 0

      I just checked, and apparently Resharper has its own cleanup on save options.

    • Aleksandar Dimitrievski 0

      Update your VisualStudio version to 17.1, after that Code Cleanup will be visible.

  • Burçin Şahin 1

    Hi, I do not know if it is asked before. Is there a way to add a 3rd or 4th profile? I have been already using these profiles, now I need to add new profiles to use in specific cases. Thx in advance.

Feedback usabilla icon