我正在研究强制执行代码样式,并开始实现 IDE0058 , IDE0059和 IDE0060在我的 .editorconfig 中,如下所示:
csharp_style_unused_value_expression_statement_preference = discard_variable:error
csharp_style_unused_value_assignment_preference = discard_variable:error
dotnet_code_quality_unused_parameters = all:error
当我在 Windows 上的 Visual Studio 2022 中运行构建时,我确实遇到了错误,尽管构建仍然成功。
但是,当我从命令行运行 dotnet build
时,我没有收到任何错误,并且当我搜索样式代码时,我找不到任何提及它们的内容。
同事在他的macbook上测试了一下,mac版的visual studio没有报错。命令行也没有。
在我们的 gitlab 管道中,我们还运行了 dotnet build
,它也顺利通过,没有任何错误。
在所有环境中,我们都使用 .NET 6.0。
是否可以使用 .editorconfig
强制执行这些代码样式?如果是这样,怎么办?
如果没有,如何强制执行代码样式?
最佳答案
将以下行添加到 csproj 文件
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>6.0-recommended</AnalysisLevel>
</PropertyGroup>
然后在 editorconfig 中除了现有规则之外添加
dotnet_diagnostic.IDE0058.severity = error
dotnet_diagnostic.IDE0059.severity = error
dotnet_diagnostic.IDE0060.severity = error
关于c# - 如何强制执行 C#/.NET 的代码样式,例如 IDE0058、IDE0059 和 IDE0060?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70517332/