

Roslyn
source link: https://hownot2code.com/2020/10/30/roslyn/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How Not To Code
C, C++, C#, Java bad practices: learn how to make a good code by bad example

public SyntaxToken GenerateUniqueName(SemanticModel semanticModel,
SyntaxNode location,
SyntaxNode containerOpt,
string baseName,
CancellationToken cancellationToken)
{
return GenerateUniqueName(semanticModel,
location,
containerOpt,
baseName,
filter: null,
usedNames: null,
cancellationToken);
}
V3156 The sixth argument of the ‘GenerateUniqueName’ method is passed as an argument to the ‘Concat’ method and is not expected to be null. Potential null value: null. AbstractSemanticFactsService.cs 24
We’ll be honest: when making this diagnostic, we didn’t really expect triggering warnings for simple null. After all, it is quite strange to pass null to a method that throws an exception because of it. Although, we have seen places where this was justified (for example, with the Expression class), but that’s not the point now.
So, we were very intrigued when we saw this warning. Let’s see what is happening in the GenerateUniqueName method.
public SyntaxToken GenerateUniqueName(SemanticModel semanticModel,
SyntaxNode location,
SyntaxNode containerOpt,
string baseName,
Func filter,
IEnumerable usedNames,
CancellationToken cancellationToken)
{
var container = containerOpt ?? location
.AncestorsAndSelf()
.FirstOrDefault(a => SyntaxFacts.IsExecutableBlock(a)
|| SyntaxFacts.IsMethodBody(a));
var candidates = GetCollidableSymbols(semanticModel,
location,
container,
cancellationToken);
var filteredCandidates = filter != null ? candidates.Where(filter)
: candidates;
return GenerateUniqueName(baseName,
filteredCandidates.Select(s => s.Name)
.Concat(usedNames));
}
As we can see, there is only one exit point in the method, no exceptions are thrown and there is no goto. In other words, nothing prevents us from passing usedNames to the Concat method and getting the ArgumentNullException.
Please click here to see more bugs from this project.
Recommend
-
48
【前言】 Roslyn 是微软公司开源的 .NET 编译器。 编译器支持 C# 和 Visual Basic 代码编译,并提供丰富的代码分析 API。 Roslyn不仅仅可以直接编译输出,难能可贵的就
-
22
Some time ago I came across Jimmy Bogard’s article “Immutability in DTOs?” about the...
-
40
基于 Roslyn 实现一个简单的条件解析引擎 Intro 最近在做一个勋章的服务,我们想定义一些勋章的获取条件,满足条件之后就给用户颁发一个勋章,定义条件的时候会定义需要哪些参数,参数的类型,获取勋章的时候会提供所...
-
15
Dynamic Class Creation in C# - Preserving Type Safety in C# with Roslyn Posted by:
-
12
Replies 4 comments As with C#8 and .NET Core 3.0,
-
17
How use Visual Studio, MSBuild or Roslyn previews in GitHub or DevOps CIIf you want to leverage the many awesome C# 9 features, including
-
10
How to generate code using Roslyn source generators in real world scenariosRoslyn (as of 16.8 Preview 3) now brings first-class support for source code generators that run as part of a project compilation. The provided
-
14
Roslyn 打包自定义的文件到 NuGet 包在使用 sdk 格式的项目文件支持快速进行打包,但使用这个方式打包的时候将默认只带程序集输出文件,而没有带依赖的文件。本文告诉大家如何在打包的时候加上需要放在包里面的文件 在
-
7
Roslyn 判断当前使用 dotnet core 编译器进行编译在写 msbuild 预编译或编译调度逻辑时,如何知道当前执行的编译器使用的是上古版本的 msbuild 还是用了 dotnet core 内核的 Roslyn 编译器?本文解决的问题是我期望在 Windows 系统使用 .NET Framework 版...
-
12
Getting Started with the Roslyn APIs: Writing Code with Code For the last few weeks, I’ve been working on designing and developing a C# code generator. In this post, I want to explain some of the core concepts that I’ve learne...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK