.Net tools blog – jetbrains tools for .net developers and visual studio users.net tools blog jetbrains tools for .net developers and visual studio users gas monkey monster truck


In our series about Rider‘s code analysis, we’ve seen what it is and how it works, and looked at various examples of inspections that can spot warnings and errors in our codebase. Rider helps us learn common practices and language features, too, using over 2400 inspections coming from ReSharper and from IntelliJ-based IDEs.

Code style is a broad topic, which includes everything from project structure to tabs vs. spaces (bring in the pitchforks!), to documenting public APIs, to internationalization and localization of our libraries and applications. All of these are important in making our codebase easier to consume – both for ourselves and for new developers joining the team. In this post, we’ll look at how Rider’s code analysis can help us maintain a consistent code style throughout our project.

One of the first things I personally do when opening a codebase is exploring where everything is and how things fit together. A clean and consistent project structure following some conventions helps us become familiar with the project and improves maintainability later on.

A good convention is that namespace declarations should match with the file location. In other words, when a class is located in a folder Extensions, we may want to have the namespace reflect this. Rider’s code analysis will show a warning when this is the case – and by now it should be clear there’s a quick fix to resolve this, too ( Alt+Enter). The quick fix will update the namespace for the current file and update any using statements in other files when needed. Keep in mind this inspection can run solution- or project-wide, too.

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.

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, …

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.

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).