ReSharper Web Help

Code inspections amd many other ReSharper features largely rely on heuristic analysis of the source code to detect issues, suggest possible improvements, and help you in other ways.

However, heuristic analysis cannot detect everything. For example, if a method is designed to never return null and its clients are designed accordingly, no heuristic analysis can help find a possible issue if someone has changed the method to return null.

In this and a lot of other cases, the Annotated Framework is of a great help. By using attributes declared in this framework you can make ReSharper analyze code the way you need it:

public object Foo()
    return null; // Warning: Possible 'null' assignment

This being the simplest example, there are other helpful attributes with more complex use cases in the Annotated Framework. You can find the full list of these attributes in the Reference.

In most cases, code annotation attributes enable new code inspections, for example:

  • StringFormatMethodAttribute helps detect misuses of string formatting methods.
  • CanBeNullAttribute and NotNullAttribute are associated with Possible 'null' assignment to entity marked with 'Value cannot be null' attribute. For more information, see Value Analysis.
  • CannotApplyEqualityOperatorAttribute is bound to Compare with '==' types marked by 'CannotApplyEqualityOperatorAttribute'.
  • ContractAnnotationAttribute introduced since ReSharper 7.0 can be used to define custom contracts for your methods and turn on the corresponding inspections. For example, you can use [ContractAnnotation("s:null => true")] to annotate a method that returns true when parameter s is null.

Also, code annotations enable more quick-fixes, code completion suggestions, and code generation features on annotated items.

ReSharper allows you to annotate code symbols in two ways:

  • You can annotate symbols in your source code as shown in the example above. In this case, you need to reference JetBrains.Annotations namespace in your solution. For more information, see Annotations in Source Code.
  • Even when you do not have access to sources, you can annotate symbols in compiled library code. For more information, see External Annotations.

Part of the work of annotating standard libraries (e.g. .NET Framework Class Library, NUnit Framework, and more) with external annotations is already done and you can benefit from attributes driven inspections, quick-fixes, and other features when using these libraries.

See Also



External Links