ReSharper

Code Analysis for C#


On-the-fly Error Detection

ReSharper analyzes your C# 3.0 code before you compile, letting you instantly see whether a file (or the whole solution) contains any errors, warnings, or inefficiences, by looking at the Marker Bar that ReSharper adds to the right-hand side of the editor window.

The Status indicator at the top of the Marker Bar changes its color if the file contains errors or warnings. When you hover over it, the indicator shows the total number of errors and warnings in the current file.

to the top

Multilevel Code Inspection

Errors

ReSharper instantly detects and highlights errors in your code, without having to compile it first. Errors are highlighted with either red font or curly underline. If you roll the mouse pointer over an error, its description is displayed in a tooltip.

ReSharper is capable of detecting errors not only in C# code, but in ASP.NET code-behind classes and markup references.
For XAML, ReSharper checks XML syntax and expertly validates XAML semantics, including references between XAML entities.
In build scripts, ReSharper analyses and highlights unknown properties, targets, tasks, and attributes.

Warnings

In addition to compiler errors and warnings, ReSharper displays its own warnings that don't prevent your code from compiling but may nevertheless represent serious coding inefficiencies. For example, ReSharper informs you about redundant casts, incorrect format strings, declared but never used local variables, etc. Constructs that have associated warnings are emphasized with either grayed text or a blue curly underline.

Suggestions

A deeper level of code analysis is available with code suggestions. Suggestions provide insights into code structure, drawing your attention to things that aren't necessarily bad or wrong, but probably useful to know. Suggestions are highlighted by green curly underline.

For instance, you may be interested to know that a certain parameter can be declared with base type, meaning that your method only uses members of the parameter's base type. ReSharper can also suggest to create an auto-property from a property with backing field, convert a static method invocation to extension method call, or use 'var' keyword when possible.

Hints

ReSharper 4.0 introduces a new, considerably less intrusive level of code analysis results — hint. Unlike errors, warnings and suggestions, hints are not taken into account when you navigate to next/previous highlights, not shown in the Marker Bar and therefore do not influence the color of the Status Indicator. A hint simply brings your attention to a particular code detail and recommends a way of improvement. Code items that have associated hints are highlighted with a solid green underline.

Disabling and configuring code inspection items

At any time, you can change severity of different analysis items - for example, make ReSharper display a particular inspection/notification as error, warning, code suggestion, or hint, according to your individual coding style or corporate requirements.
Even more, sometimes you know better that a particular piece of code is just fine and no warnings are necessary, so you can simply disable a certain warning.

To disable or configure code inspection items, use the Inspection Severity tab in the ReSharper Options dialog box. You can also configure a particular warnning, suggestion, or hint, by selecting Inspection options in the list of available quick-fixes.

to the top

Quick-Fixes

ReSharper provides quick-fixes for most errors and other highlighted items, helping you solve problems instantly. Quick-fixes are represented by the red light bulb that appears automatically to the left of a highlighted code line, when you position the caret on the highlight. You can see the list of available quick-fixes for a given error, either by clicking the light bulb or by pressing Alt+Enter. Then simply select an appropriate quick-fix from the list, and the problem will be resolved in the most convenient way.

to the top

Each error, warning, or suggestion is represented by an individual stripe on the Marker Bar. Clicking the stripe navigates you directly to the line of code that contains the error or causes the warning/suggestion.

You can navigate between errors, warnings and suggestions by pressing F12 (forward) and Shift+F12 (backward). You can also navigate between just errors (skipping any warnings and suggestions) by pressing Alt+F12 (to next error) and Shift+Alt+F12 (to previous error). A message describing the current error, warning, or suggestion displayed in the status bar:

to the top

Solution-Wide Analysis

Not only is ReSharper capable of analyzing a specific code file for errors, but it can extend its analysis skills to cover your whole solution.
Suppose that you changed the visibility of a member from public to internal, assuming that it was only used inside the current assembly. At this stage, ReSharper finds no errors in the current file. Then, you switch on Solution-Wide Analysis and ReSharper discovers that something went wrong. You jump to the next error in solution and you find out that someone used this member from outside of the current assembly.

All you have to do is explicitly switch Solution-Wide Analysis on, and then, after it analyzes the code of your solution, view the list of errors in a dedicated window:

Even without opening that window, you can still easily navigate through errors in your solution with Go to Next Error in Solution (Alt+F12) and Go to Previous Error in Solution (Shift+Alt+F12) commands.

to the top

Value Analysis

ReSharper provides a new type of on-the-fly error analysis called Value Analysis for analyzing null references and boolean values. It automatically detects when you access a variable that can possibly be null and immediately warns you about it. It can also suggest that a condition is always true or false at a specific point in code.

Additional sources for warnings can be customized and added by using custom attributes in the JetBrains.Annotations namespace that is also used to annotate .NET Framework types.

to the top

Annotated Framework

We have analyzed a great share of .NET Framework Class Library, as well as NUnit Framework, and annotated it through external XML files, using a set of custom attributes from the JetBrains.Annotations namespace, specifically:

  • StringFormatMethodAttribute (for methods that take format strings as parameters)
  • InvokerParameterNameAttribute (for methods with string literal arguments that should match one of caller parameters)
  • AssertionMethodAttribute (for assertion methods)
  • AssertionConditionAttribute (for condition parameters of assertion methods)
  • TerminatesProgramAttribute (for methods that terminate control flow)
  • CanBeNullAttribute (for values that can be null)
  • NotNullAttribute (for values that can not be null)

Annotations for members of the standard class library increase the efficiency of code analysis that ReSharper carries out. For example, StringFormatMethodAttribute is used to annotate String.Format-like methods the way that ReSharper checks the number of provided arguments against the structure of format string and warns the user if a necessary argument is missing.

You can also use the code annotation attributes to annotate your own code for better code analysis. To do that, put a reference to JetBrains.Annotations.dll in ReSharper's installation directory, or, alternatively, choose ReSharper | Options | Code Inspection | Code Annotations and copy the default implementation of the code annotation attributes into your solution:

to the top