Using the PVS-Studio extension for VS Code to effectively handle errors in C# code
Tags: vscode, csharp
Published: 2023-11-09 08:30:18

VS Code is a code editor that many developers are familiar with. Adding different extensions to enhance the programming experience is one of its main features. Now one of them helps find errors more efficiently not only in C, C++ but in C# code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr2fe37eqemv7cmow4un.png)
Introduction
Today, the dev community has many common approaches for enhancing code quality. Developers follow the code design principles and adopt various programming practices, such as code review and unit testing, in the development process. Moreover, tools for detecting errors and vulnerabilities in code are rapidly gaining their popularity. The PVS-Studio static analyzer of C, C++, C# and Java code is an example of such a tool. This analyzer is easy to use and can be integrated into various IDEs (as well as into VS Code) via special extensions. These extensions help developers easily run the analysis and efficiently handle its results.

I've reviewed the PVS-Studio extension for Visual Studio Code [earlier](https://pvs-studio.com/en/blog/posts/1030/). Since then, the PVS-Studio team has added several new features:

- Running the analysis of C, C++ (CMake) and C# (MSBuild) solutions in the VS Code interface;
- Using suppress files for warnings suppression;
- Running the analysis of individual code files or .NET projects (.csproj).

In this article, using the example of the .NET project analysis, I'll show you how easy it is to find errors with the PVS-Studio extension for VS Code!

**Preparing for the analysis**
Before running the analysis, you need to check whether the PVS-Studio analyzer for your programming language and the PVS-Studio extension for VS Code are installed. If they aren't, you can consult the [installation instructions](https://pvs-studio.com/en/blog/posts/1030/#IDE7EFA9DD17) from the previous article about this extension.

**Running the analysis**
First, open the folder with the solution. You can run the analysis of the entire solution — just click the Analyze project button in the PVS-Studio window. This window is usually located at one of the tabs in the VS Code bottom pane.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zxby3j860mgo8yaaug2.png)

If the PVS-Studio window is hidden, you can show it by executing the 'Show window' command in the command palette (Ctrl + Shift + P).

What if you need to analyze only certain code files or projects? In this case, you can follow one of the new ways to run the analysis, for example, via the File Explorer context menu.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvc7exddvvkpcp3zs4dr.png)

You can find other ways in the [documentation](https://pvs-studio.com/en/blog/posts/1030/).

The first time you analyze the .NET solution, you'll see the following message:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h86n5ow2l4vw6jjv0m6w.png)

Clicking the Edit button will open the .jsonc file. You can specify optional analysis parameters similar to some arguments of the analyzer console version.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo1wgd2tac2f4pn47vfh.png)

After reviewing this file, run the analysis again. Now the analyzer may prompt you to select the solution (.sln) in the command palette. The solution includes the analyzed files or projects (in case there is more than one solution in the open directory). After that, the analysis will be run, and the PVS-Studio window will look as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drp25wzybf3oxjophnqu.png)

By the end of the analysis, you'll have a complete list of warnings. Since I have been analyzing only a few files from the solution, there is not so many of them.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwp6hexgeanxrkiuflb3.png)

**Reviewing warnings and fixing errors**
The plugin has several filters and features that make it easier to handle the analysis results. You can learn more about them in the [documentation](https://pvs-studio.com/en/blog/posts/1030/#ID1C9E12307F).

Among the warnings received, there are two that are especially notable. Let's take a look at the warnings and the code they point to. Double-click one of the selected warnings. As a result, the analyzer will display the code in the editor.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj4epfpfxv6fsssg3zxc.png)

The PVS-Studio warning:[V3142](https://pvs-studio.com/en/docs/warnings/v3142/) Unreachable code detected. It is possible that an error is present. SharedDoAfterSystem.cs: 52.

This warning points to line 52 and indicates that the following code is unreachable.

You won't have to think long and hard because the following warning will clearly indicate the cause of the problem.

The PVS-Studio warning:[V3022](https://pvs-studio.com/en/docs/warnings/v3022/). _Expression is always true. Probably the '&&' operator should be used here. SharedDoAfterSystem.cs: 49._

Let's take a look at the conditional expression on line 49. According to the warning, the expression is always true. We can easily see that the _args.NewMobState_ field is checked for inequality with two values:_ MobState.Dead_ and _MobState.Critical_ via the _OR_ operator. As a result, the second check is always true if the first check is false, and vice versa.

This error causes the _OnStateChanged_ method to always terminate with a return on line 50, and all the following code is never executed.

**Suppressing reviewed warnings**
When you review warnings and fix errors, it's worth suppressing false-positive or minor warnings. So, they won't distract developers in later analyses.

To do this, you need to select the warnings in the table (you can select them all at once using "Ctrl + A"), open the context menu, and select the _Mark_ as a _False Alarm_ or _Add selected messages to suppression file_ options. You can also use these options by clicking the lightning icon button in the upper-right corner of the PVS-Studio window.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9gsxi1cfrtnvrqk8o7l.png)

The first function is different from the second one: it adds the comment such as _//-V[Warning Code]_ to the first line of code pointed by the warning to suppress it. However, the second function stores information about suppressed warnings in a special file.

Note that it's preferable to use _Mark_ as a _False Alarm_ to suppress unnecessary warnings.

Adding warnings to the suppression file is a handy way to put technical debt aside and focus on the quality of new code.

In this case, the standard scenario for using the feature is as follows.

1. Perform a general analysis of the solution.
2. Suppress the received warnings.
3. Periodically review the suppressed warnings in small increments. To return the suppressed warnings, edit or delete the .suppress.json file located in the .PVS-Studio folder of the solution directory.

You can learn more about it here: ["How to introduce a static code analyzer in a legacy project and not to discourage the team"](https://pvs-studio.com/en/blog/posts/0743/).

## Conclusion
I hope I've shown you that the PVS-Studio extension is an easy-to-use and handy tool to be integrated in the development process. In conclusion, I'd like to note that the extension is being worked on, and in the future versions, Java developers will be able to use it for checking their projects too. However, that's a story for another article.

Let me remind you that you can download the PVS-Studio extension for VS Code from the [official website](https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&utm_medium=pvs&utm_campaign=article&utm_content=1079).

Clean code and successful projects to you! See you in the next articles!