Welcome to day 20 of my 31 Days of ReSharper.
I’ve been talking about Alt+Enter, and how it can fix a couple of different kinds of errors, but I’ve only scratched the surface. It can fix all sorts of things — not everything, but quite a bit. And it’s not just errors; it can fix quite a few warnings (grays) as well.
So today I’ll cover a few more examples. These are not meant to be exhaustive.
A note about “disable this warning”
When you’re over a ReSharper warning in the code (either gray code, or a blue squiggly), and press Alt+Enter, one of the quick fixes will be to disable the warning, e.g., “Disable warning ‘Unused private member'”. My advice is, avoid this command.
I tried it once, because I thought it meant “put in a comment or a
#pragma or something to flag that this particular occurrence is okay”. It doesn’t. It means “never check for this condition again, ever, anywhere”. And there’s no Undo for it. You have to dig through ReSharper Options until you find the right setting to turn it back on.
Fortunately, you can disable this command entirely (on the same page where you go to clean up after you selected it unintentionally). Go to ReSharper Options > Highlighting, and uncheck “Show ‘Disable warning …’ action”.
Fixing error: Return type doesn’t match
Suppose you’re upgrading code from VS 2003 to VS 2005, and are starting to use generics. So you refactor a utility method to return
IList<Foo> instead of
IList. But now all the places that called it are broken.
Here’s one of the call sites that’s assigning the method result into an
IList. In this example, you would want to select Change type of ‘errors’ to ‘IList<Exception>’. And you can do the same thing at each other call site. Beats re-typing the same return type over and over.
Fixing error: Abstract method in concrete class
If you’re in the middle of introducing a template method, you might end up with abstract methods in a class, but you haven’t marked the class itself as
In this example, you just cursor to the
abstract keyword, press Alt+Enter, and select Make ‘ClassName‘ abstract. Sure, it’s not that much typing to just scroll to the class declaration and type
abstract there; but the nice thing about this one is that the squiggly shows up right there in the code you’re working with, so you don’t need to scroll to the top of the file.
Fixing warning: Variable is assigned but never read
ReSharper can fix variables that are declared but never used, but that’s not very interesting. Variables that are one-way — read but never written, or vice versa — are a little more interesting. So here’s a variable that’s assigned in its declaration, but then never gets read:
When it says “assignment(s)”, it means it. It will remove any assignment that happens at the declaration, and it will remove any other assignments that happen anywhere else. This is particularly striking when you have a field that’s never read; ReSharper will remove every assignment in every method. This can be very handy when you’re ripping out code you don’t need anymore.
Be careful with this one. If any of the assignments have side effects, you will lose those side effects. In the above example, the value would never get added to the list box.
Fixing warning: Field is read but never written
This one is cool. You just select Initialize field in constructor(s), and then you’re done. ReSharper automatically adds a new parameter to your constructor and generates the code to assign it.
Oddly, there isn’t a quick-fix to create a read/write property. I’m sure I’ve seen that as a quick-fix, but apparently not here.
Yep. When you use ReSharper to remove an unused parameter, it automatically fixes all the call sites to keep the code compilable.
Often, this yields more gray code, especially when you had one object that you were plumbing through seven layers of code, and then you refactor so you don’t need it anymore. Remove it at the bottom layer, and now the next layer up has a gray parameter.
You might not see this very often, though, because ReSharper only seems to mark a parameter as gray if you’re in a private method. This is silly, of course; you would care about unused parameters in public methods at least as much as in private methods.
F12 and Alt+Enter
That’s just a taste of what Alt+Enter can do to fix errors and warnings. When you see stripes in the color bar, it can be worth it to just keep hitting F12 (jump to next error) and Alt+Enter, and seeing what ReSharper can do to help clean up your code.