Joe White’s Blog

Life, .NET, and Cats


ReSharper, day 20: Fix errors and warnings with Alt+Enter

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&gt; instead of IList. But now all the places that called it are broken.

ReSharper's Quick Fixes for an unmatched return type

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 abstract yet.

ReSharper's Quick Fixes for an abstract method in a non-abstract class

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:

ReSharper's Quick Fixes for a variable that is written, but never 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

ReSharper's Quick Fix for a field that is read, but never assigned

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.

Fixing warning: Unused parameter

ReSharper's Quick Fix for an unused parameter

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.

There are no responses to “ReSharper, day 20: Fix errors and warnings with Alt+Enter” yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Joe White's Blog copyright © 2004-2011. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS). Privacy policy