7

I clicked "Find Code Issues", then Resharper showed me list of unused classes and methods. But I can't find how to automatically remove all them. List of unused classes and methods contains thousands of lines, so сlicking on each line and delete method manually is not real. How to do it automatically?

I tried to use "Code Cleanup", but it doesn't remove unused classes and methods

The version of Resharper: JetBrains ReSharper 8.2.1 Full Edition Build 8.2.1000.4556 on 2014-05-19T09:12:38

Anatolii Humennyi
  • 1,807
  • 3
  • 26
  • 37
  • Which version of R# are you using? – Matthijs May 30 '14 at 12:46
  • The latest which I downloaded today. JetBrains ReSharper 8.2.1 Full Edition Build 8.2.1000.4556 on 2014-05-19T09:12:38 – Anatolii Humennyi May 30 '14 at 12:47
  • 1
    I am not sure about the fixing all code issues, but you could take a look at "Code Cleanup": http://www.jetbrains.com/resharper/features/code_formatting.html – Matthijs May 30 '14 at 12:49
  • @Matthijs Unfortunately "Code Cleanup" doesn't remove unused classes and methods. I tried it already. – Anatolii Humennyi May 30 '14 at 12:50
  • 1
    Have you checked the box that says "Remove code redundancies"? If you have, them I'm afraid I don't know any other options. – Matthijs May 30 '14 at 12:53
  • @Matthijs yes sure. I checked it. I don't know why Resharper didn't remove unused classes and methods – Anatolii Humennyi May 30 '14 at 12:58
  • 1
    My best guess would be that it is not a built in option, because it may generate false positives and, when running Code Cleanup, delete a lot of potentially used and valuable code. – Matthijs May 30 '14 at 13:03
  • if you use reflection or copy-construction only, some versions of r# may false-flag entire classes as unused. – Andrew Hill Jul 21 '15 at 05:53

1 Answers1

5

Unfortunately Resharper does not provide this feature as it can be unsafe.

To partially automate the deletion you can try installing AutoHotkey. This program will allow you to automate the entry of hotkeys and therefore 'automate' repetitive Resharper tasks like deleting unused code detected by Code Issues.

The following script automatically goes to the next code issue and attempts to 'Safely Delete'. If this takes too long it cancels it with {Escape} as this is generally means Resharper has found a conflict or usage.

#d::
    Loop {
        Send !{Del}
        Sleep, 500
        Send {Enter}
        Sleep, 1000
        Send {Escape}
        Sleep, 500
        Send {F8}
        Sleep, 500
    }
Return

To use this script click on the first item under the Type Or Member is Never Used category then hit the Win-D hot key. The script will then cycle through all the issues deleting the methods which do not have conflicts. To break the loop select outside of Visual Studio & Reload the script.

If you remove the Loop & {Escape} then you can use this as a single shortcut to delete & move to the next issue.

Daniel Roberts
  • 534
  • 5
  • 8