Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers


Fix in Scope: a New Code Cleanup Experience in ReSharper 8

As you all know, ReSharper has lots of inspections, all of varying complexity. Some inspections present a multitude of choices – for example, when ReSharper meets an undeclared symbol, it has no idea what to make of it. Does the user want it as a parameter, a local variable or perhaps a field or property of the containing class?

On the other hand, plenty of fixes are unambiguous. Some examples include:

  • Redundant using statements

  • Redundant this. prefix on class members

  • Properties with backing fields that can be transformed into auto-properties

There are two ways that users can interact with these inspections. The first, and most direct, way is via a quick-fix — that little pop-up that shows up in code when the cursor is at a location where a fix can be applied:

Remove redundant qualifier quick-fix

Another option is to invoke a predefined set of fixes in bulk at the scope of a file, folder, project or solution. This is the Code Cleanup mechanism that can fix a configurable set of issues in every file in the selected scope:

Code cleanup options

However, in ReSharper 8 we’re introducing a new mechanism that we call Fix in Scope. This feature is an extension of the quick-fix mechanic that lets you either apply the fix just in a particular location or, alternatively, you can expand the menu and pick a wider scope for the fix to be applied:

ReSharper 8 Fix in Scope

Here’s a video overview of Fix in Scope — one of several videos about ReSharper 8 available on YouTube:

It is our hope that this feature helps you streamline fixes of a particular type when you need to do them en masse. For example, you’re starting a new project, and a default project template generates lots of plumbing with lots of unnecessary using statements. Just move the cursor over one of the unused directives, expand the Remove unused directives in file, pick Remove unused directives in project and you’re done — extraneous using statements in all files in the project will be instantly removed!

At the moment, Fix In Scope supports a set of features provided by Code Cleanup. Here’s a (hopefully) complete list:

  • Remove code redundancies, including the removal of
    • Explicit integer type specifiers for enumerations
    • true specifiers in for statements
    • Redundant type casts
    • Redundant qualified references
    • Redundant this qualifiers
    • Redundant parentheses from attributes without arguments
    • Explicit calls to base class constructors without arguments
    • Redundant boolean comparisons
    • Redundant braces from collection initializers with a single expression
    • Redundant explicit delegate creation expressions
    • Empty argument lists from object/collection initializers
    • Redundant new keywords
    • Redundant partial modifiers on type declarations where a type has a single part
    • Redundant unsafe contexts and statements
    • Redundant object.ToString() calls (for example, in a concatenation with a string or as an argument of a string.Format() call)
    • Redundant string.ToCharArray() calls
    • Redundant specifications of type arguments
    • Signatures in anonymous methods where its parameters are not used in the method body
    • Non-referenced labels
    • Redundant field initializers
    • Redundant explicit names in anonymous type properties
    • Redundant nullable types where value type is implicitly convertible to nullable

    as well as the following conversions:

    • Nullable types to short form (Nullable<T> to T?)
    • Static method invocations to extension method calls where extension methods are invoked as static methods
    • Conversions to implicitly typed array creation expressions when array type can be inferred from initializer
  • Use auto-property if possible
  • Make field read-only, if possible
  • Use ‘var’ in declaration, inculding options to
    • Use ‘var’; or
    • Specify type explicitly
  • Optimize ‘using’ directives
  • Optimize ‘import’ directives
  • Shorten qualified references
  • Remove redundant qualifiers
  • Terminate statements
  • Optimize register directives

In the future, we plan to expand the set of supported inspections to make the process of code improvement even more streamlined. Meanwhile, we suggest you check out ReSharper 8 and let us know what you think!

image description