Pull to refresh
312.84
PVS-Studio
Static Code Analysis for C, C++, C# and Java

Finally! PVS-Studio Supports .NET 5 Projects

Reading time 4 min
Views 609

On November 10th, 2020, Microsoft released a new version of .NET Core - .NET 5. The updated platform presents many new enhancements. For example, it allows C# developers to use features the new C# 9 offers: records, relational pattern matching, etc. Unfortunately, there was a disadvantage: PVS-Studio could not analyze these projects. However (and here's the good news)... That used to be the case. Our next release, PVS-Studio 7.13, will support projects that target .NET 5.

Aka .NET Core 4

.NET 5 is the next step after .NET Core 3.1. We can see that Microsoft changed the name from .NET Core to .NET. This is to show that they will focus on this platform further on. Microsoft basically confirmed this on their official website – they've already posted three preview versions of .NET 6 there.

But why did the version number go from 3.1 to 5? This does not seem surprising - remember how Windows 10 followed Windows 8.1? :)

In fact, Microsoft skipped number 4 to avoid confusion between .NET Framework and .NET. However, "Entity Framework Core 5.0", based on .NET 5, will keep its "Core" postfix. Otherwise, it would not be possible to differentiate between Core and Framework versions of "Entity Framework 5.0".

.NET 5 supplies developers with many new tools. For example, C# now offers an enhanced pattern matching mechanism. Then there is this unusual and interesting feature that enables you to write code outside of functions or classes. Not every developer needs this capability, but it's bound to attract some fans. A full list of C# 9 enhancements is available on Microsoft's official website.

You can read about new features in .NET 5 here.

PVS-Studio and .NET 5

We received many requests from our clients to support .NET 5 projects. We also understood that the analyzer must support the latest innovations. However, although important, new .NET versions are not PVS-Studio's only direction for development. This is why it took us a while to present our new version that supports .NET 5 projects.

The upgrade concerns our analyzer versions for Windows, Linux and macOS. PVS-Studio for Windows still requires .NET Framework 4.7.2. On Linux and macOS, the analyzer now requires .NET 5. Previously, .NET Core 3.1 was sufficient.

Why did we switch the analyzer for Linux and macOS to .NET 5?

Our initial plan was to switch the analyzer from .NET Core 3.1 to .NET 5 at a later time. However, this had to change when we encountered a problem.

When analyzing projects for .NET Core or .NET 5, PVS-Studio actively interacts with the corresponding version's SDK. The tricky thing is, .NET 5 SDK libraries depend on the .NET 5 version's "System.Runtime" library. Meanwhile, if the analyzer targets .NET Core 3.1, it loads the 3.1 version's "System.Runtime" library. As a result, we kept getting a conflict - the analyzer could not interact with the SDK library, and the analysis was not possible.

Switching from .NET Core to .NET 5 completely resolved this problem.

Instantaneous Enhancements

To support .NET 5 project analysis we needed to update some dependencies. Specifically, PVS-Studio now uses newer Roslyn and MSBuild versions. This enables the analyzer to process code, that uses C# 9 features, correctly. For example, the code below could trigger false warnings:

user = user with { Name = "Bill" }

The analyzer did not have any information about WithExpression - and thus could not process this code correctly. As a result, the analyzer issued a warning that the user variable was assigned to itself. Of course, the analysis report also contained a warning that the analyzer did not support this project. However, the latter did not make things easier. :( Luckily, the new update fixed the problem automatically. Other problems we had to fix manually.

Problems with New Versions

To stay up-to-date, the static analyzer needs to adapt to new language versions. The analyzer's developers cannot foresee all features a language may get in the future. To support a new language version, the analyzer needs more than just correct parsing and semantics structure. The analyzer's developers also need to review the tool's code, evaluate the use of various internal technologies and diagnostic rules. The analyzer will likely require additional changes after a new language version comes out.

One of the new - and problematic - C# features is top-level statements. When working on the C# analyzer, we expected that local variables, conditions, loops, etc. are always inside methods. Now that you can write C# code even without declaring a class... Obviously, this is something very interesting, but it creates some problems for us. Though we doubt that all developers will start to write all code outside of classes. :)

Another new feature that prompted us to change our tool's code is the init accessor. We had to rework the V3140 diagnostic rule.

The rule is triggered when a property's accessors use different internal variables. The code sample below (from the RunUO Check article) triggers this diagnostic:

private bool m_IsRewardItem;

[CommandProperty( AccessLevel.GameMaster )]
public bool IsRewardItem
{
  get{ return m_IsRewardItem; }
  set{ m_IsRewardItem = value; InvalidateProperties(); }
}

private bool m_East;

[CommandProperty( AccessLevel.GameMaster )]
public bool East                                       // <=
{
  get{ return m_East; }
  set{ m_IsRewardItem = value; InvalidateProperties(); } 
}

PVS-Studio warns: V3140 Property accessors use different backing fields. WallBanner.cs 77

When writing the V3140 diagnostic, we assumed that a property can have only two accessors - get and set. The new init accessor went outside this expectation. This did not just cause the diagnostic's incorrect operation - the diagnostic kept crashing and throwing an exception! Luckily, we caught this problem when testing - and fixed it successfully.

Can't Wait for the Release?

You do not have to wait! Of course, now it's not too long before the release, but still... Don't you want to try the new version as soon as possible? No problem - we are eager to help! Go to the feedback page and request the new version. We'll try to respond as soon as possible - which means soon you can analyze your (and not yours only) .NET 5 projects!

We also encourage you to share your experience with PVS-Studio. Whatever your experience with the analyzer, we would like to know about it. After all, it is largely thanks to user reviews that PVS-Studio develops and becomes better.

Tags:
Hubs:
+2
Comments 0
Comments Leave a comment

Articles

Information

Website
pvs-studio.com
Registered
Founded
2008
Employees
31–50 employees