Solution-wide analysis in Rider

In a previous post, we looked at the basics of Rider‘s code analysis. We’ve seen that Rider continuously runs over 2400 inspections and validations on our code, informing us about potential issues and improvements while we are writing it. Code Analysis works with all languages supported by Rider and builds on inspections coming from ReSharper and from IntelliJ-based IDEs.

In this post, we’ll look at solution-wide analysis and how it can help us monitor code quality issues in our entire codebase at once – essentially providing us with an automated code review. We will also check out other ways to detect and fix problems in the scope of your project or solution.

In this series: (table of contents will be updated as we progress)

What is solution-wide analysis and where do I find it?

Solution-wide analysis in Rider does two things for us. First, it lets us constantly keep track of all errors/warnings in our solution, such as compiler errors or errors detected by code inspections. Second, it helps us find issues that can only be detected by analyzing the whole solution at once. Examples would be determining if a public member is used anywhere in the codebase, unassigned fields, …

We can enable/disable solution-wide analysis from Rider settings (Editor | Inspection Settings), from the Errors in Solution tool window (View | Tool Windows | Errors in Solution) or from Rider’s status bar:

Enable/disable solution-wide analysis from status bar Continue reading

Posted in How-To's | Tagged , , , , , | 4 Comments

Introduction to code analysis in Rider

One of the strengths of Rider is code analysis. Rider comes with over 2400 code inspections that analyze our code at design time, allowing us to instantly see any errors, problems or potential improvements – whether in the current file or in our entire solution.

In this blog series, we’ll look at what code analysis is, how it can do automated code review and how it helps us improve the quality of our code. Once we covered that, we’ll look into some of the inspection categories that are available and how they can help spot and resolve potential issues in our codebase.

In this series: (table of contents will be updated as we progress)

Let’s start at the beginning: what is code analysis?

What is code analysis?

How do we know if our code is correct? Unfortunately, there is no single answer to that question. We have to rely on various methods to validate code correctness:

  • Compile our code to check for compilation errors.
  • Run our application to check for issues at runtime.
  • Write and run unit tests to validate code logic.

There’s a downside to these three ways of validating code: they all require an explicit action. For example, to detect runtime errors we have to run our application, click through it and see if things are okay. We have to attach a profiler to figure out whether we implemented the Disposable pattern correctly. What if we could detect potential issues while writing code?

Code Analysis continuously looks at what we are doing and tries to determine what we want to achieve. Rider looks at individual statements and at our overall solution architecture. If it detects a potential issue, it will show a warning. It may show suggestions too, helping us make our code better. Let’s look at an example.

What does it look like in the editor?

We can access code analysis in a number of places, but let’s start with a place where it’s very obvious: in the editor. We’ll see our code get underlined with a “squiggle”, its color based on the severity of the code inspection result (hint, suggestion, warning, error).

Hovering over an inspection shows us what the issue is. In this case, we’re casting a Person to a Customer, which could result in a NullReferenceException at runtime.

Possible NullReferenceException - inspections in action Continue reading

Posted in How-To's | Tagged , , , , , | 2 Comments

Readonly structs, ref readonly and conditional ref expressions – C# 7.2 in Rider and ReSharper

Today, we continue our blog series about C# 7.2 language support in ReSharper and Rider.

In this series:

Last time we indicated that there is a remaining pitfall with in parameters. Let’s see why!

Readonly structs

There is one downside to using in parameters: for regular structs, they impose the compiler to generate copies whenever we call an instance member on it. But why do we need a copy? Didn’t we learn that in parameters will only allow us to read from that variable, and not to modify it? This is only partially true, because it only prohibits reassignments of the parameter. Instance methods of the struct are still allowed to mutate the reference by setting the this parameter – in other words, changing themselves – or reassigning fields or properties. So in order to guarantee our expectations on the call-site – that in parameters will not allow modifications – copies are being created:

The concept of readonly structs introduced with C# 7.2 can solve this issue. Adding the readonly modifier will ensure that all instance members as well as the value itself (this reference) will be completely immutable. We can’t change anything, neither from outside nor from inside the struct:

Continue reading

Posted in How-To's | Tagged , , , | 2 Comments

Leading digit separators, ref structs and in parameters – C# 7.2 in Rider and ReSharper

Time for another blog series! This time we will focus on the new C# 7.2 language features for which we’ve added support in ReSharper 2018.1 and Rider 2018.1. We will see how they help to improve our code quality and how they work under the hood.

In this series:

Let’s get started with something easy …

Leading digit separators in numeric literals

Beginning with C# 7.0, we can use underscores to separate large numeric literals into chunks of digits – thus called digit separators. Using digit separators can help to improve readability in our code base:

Prior to C# 7.2, digit separators were only permitted after the first significant digit of a hexadecimal or binary literal. This limitation has been removed so that we can now use the first group solely to specify the base (or radix) of our numeric literal. Other limitations from C# 7.0 remain the same, including that underscores cannot occur next to the decimal, next to the exponent character or next to the type specifier.

Speaking of numeric literals, we hope you didn’t miss our several new context actions for converting between different bases and adding/removing digit separators:

Converting numeric literals and adding digit separators

Continue reading

Posted in How-To's | Tagged , , , | 1 Comment

Better null checks, string formatting and path completion with JetBrains Annotations

In the previous blog post of our series around using JetBrains Annotations to improve Rider (and ReSharper’s) code analysis, code completion and navigation, we looked at some background: what are these annotations? And how do we add them to our source code? Now that we have them added to our code, we can look at some examples!

In this series:

We started our series with some ASP.NET MVC annotations (of which there are many more). Rider and ReSharper support a large number of annotations – let’s look at a few interesting examples.

Continue reading

Posted in How-To's | Tagged , , , , | 2 Comments

What are JetBrains Annotations – and how to get them?

In our previous blog post, we looked at an example of using JetBrains Annotations. We saw they are an easy way to improve Rider (and ReSharper’s) code analysis, code completion and navigation by adding some simple hints in our code, in the form of attributes. In this post, we’ll look a bit more at the background: what are these annotations? And how do we add them to our source code?

In this series:

we look at how to add JetBrains Annotations to our code, let’s take one step back and look at what they are.

What are JetBrains Annotations?

Rider and ReSharper’s code analysis and inspections are very smart on their own and help find code smells, dead code, potential compile time or run time errors, and more.

We have already seen that we can make this mechanism smarter by telling Rider and ReSharper what we mean. In our previous post, we saw that the ReSharper engine knew we were working with strings, but only we as the author of that code knew these strings were controllers and actions. Code analysis may warn us that a value can be null, but maybe the underlying code never returns null at all. Only one way to tell the engine: annotations. Continue reading

Posted in How-To's | Tagged , , , , | Leave a comment

Improving Rider and ReSharper code analysis using JetBrains Annotations

One of the common questions we often get when meeting our users is this: “Can you show me something I did not know about Rider or ReSharper?” A great example would probably be the use of JetBrains Annotations – an easy way to improve Rider (and ReSharper’s) code analysis and functionality. In the next three blog posts, let’s see what they are and how we can use them to help other developers working with our code!

In this series:

What better way to start this series than to provide just an example, without the nitty-gritty details? Here goes!

A quick example…

Let’s say we are extending ASP.NET MVC’s HtmlHelper with a utility method of our own. The extension method takes three parameters: the HtmlHelper we are extending, a controller name and an action. Here’s the signature:

In our Razor view, it is all too easy to make a mistake when calling this helper method.

Did you spot the issue? We have accidentally mixed up the controller and action parameter. This will not be an issue at compile time, but things would break at run time. Given these are just strings that ASP.NET MVC passes around, making this kind of mistakes is bound to happen. Unless there would be a way to help our tools help us… Continue reading

Posted in How-To's | Tagged , , , , | 7 Comments

Introduce variable, iterate and merge back deconstruction

In our previous post, we’ve looked more closely at and how ReSharper 2018.1 and Rider 2018.1 help maintain consistency when using deconstructing declarations and discards. This time, let’s check out how ReSharper (and Rider) can assist us with introducing variables, iterating and merging back deconstruction when writing new or updating existing C# code.

Introducing variables with deconstruction

Introduce variable is arguably the most used refactoring that has ever existed in ReSharper, and which was available since the very first versions. It allows us to create a new variable, initialize it with a selected expression and optionally replace other occurrences of the same expression with the newly introduced variable.

ReSharper 2018.1 brings an improvement to it: whenever we invoke introduce variable on a tuple or any other expression whose type supports deconstruction, we now get the choice to either introduce a single variable, or to deconstruct the given expression into multiple variables. And if it’s possible to deconstruct value in several ways, ReSharper will show all the alternatives:

Introduce variable demo

Hint: we can introduce a variable in different ways. Via Refactor This… in the Alt+Enter menu, by hitting Ctrl+R,V, by invoking the .var postfix template or by executing the Introduce variable context action.

Continue reading

Posted in How-To's | Tagged , , , , , | Leave a comment

Embrace deconstruction with ReSharper 2018.1

Deconstruction is a great feature that was introduced with C# 7. A while ago, we showed how ReSharper (and Rider) can help generate deconstructors for us. ReSharper 2018.1 adds even more enhanced support to fully embrace the elegance and power of deconstructions with new code styles, quick-fixes, context actions, better refactoring support and more!

In the next two posts, we’ll go over these new features. This first post is dedicated to new code styles. The second part will cover everything else. Let’s dive in!

New code styles

Continue reading

Posted in How-To's | Tagged , , , , | Leave a comment

Navigation improvements in ReSharper 2018.1

Our latest release of ReSharper 2018.1 includes improvements in navigation. We will be looking at several navigation features for Go To that have been improved.

Go To File Member

ReSharper’s ability to move through the members with a file of code has been there since the beginning. We can invoke the ReSharper | Navigate | Go To File Member… command (Alt+\) to view the list of members in the file. We can invoke Alt+\ again and toggle the list to include all members of the current class’s base classes. When we select a member from the list, the caret will be located at that member’s declaration.

Go To File Member

Continue reading

Posted in How-To's | Tagged , , , | 9 Comments