我没有看到代码分析规则 csharp_prefer_simple_sim_usis_statement
aka“使用Simple'使用'使用'语句(IDE0063)”在预期时产生输出。我在项目中的一种方法中添加了一些虚拟代码,例如:
using (var file = Image.FromFile(userName))
{
System.Diagnostics.Debug.Assert(file != null);
}
我的.sln式解决方案vs 2022中包括几个.csproj式项目(即“旧方法”)。我在与.sln的同一文件夹中有一个.editorConfig文件,并且其他各种内置的.NET分析仪和Roslynator Analyzers工作正常。
在.editorConfig中i有 csharp_prefer_simple_simple_sus_statement = true:parnning
,i
还添加了 dotnet_diagnostic.ide0063.severity =警告
良好的方法。我仔细检查了配置中其他位置的重复,并且解决方案/项目文件夹中的任何地方都没有其他.EditorConfig文件。
即使我知道它应该被.EditorConfig文件取代,但我在VS选项中找到了相同的设置并在此处启用了相同的设置:
我还打开了项目文件(C#10/.NET 6最新,BTW),并设置< Analysis Level> festment-recmummended</shiallyslevel&gtvel>
。
我已经清洁了构建,重新启动VS并重建,并且在输出,错误列表或编辑器中仍然没有看到任何内容,表明它建议简化使用使用语句。同样,我拥有许多其他代码分析规则,它们的产品输出同时使用在编辑器中和构建输出中&错误列表。
我要去哪里出错?
编辑:@guru Stron的问题使我的Spidey感觉挠痒痒,我发现,虽然这里的第一个方法确实不是产生IDE0063,但后者确实如此。为什么?
public Stream GenerateReport()
{
using (var reportContext = new ReportRenderContext(this.ReportTemplate))
{
reportContext.Render();
}
return this.FileStream;
}
public static int GetAreaOfImage(string fileName)
{
using (var image = Image.FromFile(fileName))
{
return image.Size.Width * image.Size.Height;
}
}
I am not seeing the code analysis rule csharp_prefer_simple_using_statement
aka "Use simple 'using' statement (IDE0063)" produce output when expected. I added some dummy code to a method in my project, like so:
using (var file = Image.FromFile(userName))
{
System.Diagnostics.Debug.Assert(file != null);
}
My .sln-style Solution in VS 2022 includes several .csproj-style Projects (i.e. the "old way"). I have a .editorconfig file in the same folder as my .sln, and a variety of other built-in .NET analyzers and Roslynator analyzers work fine.
In the .editorconfig I have csharp_prefer_simple_using_statement = true:warning
, and I
also added dotnet_diagnostic.IDE0063.severity = warning
for good measure. I have double-checked that neither are duplicated elsewhere in the config, and there are no other .editorconfig files anywhere in the solution/project folders.
Even though I know it's supposed to be superseded by the .editorconfig file, I found the same setting in VS Options and enabled it there too:

And I also opened the project file (C# 10 / .NET 6 latest, btw), and set <AnalysisLevel>latest-recommended</AnalysisLevel>
.
I have cleaned the build, restarted VS, and rebuilt, and I still see nothing in the Output, Error List, or in the editor indicating that it suggests simplifying the using statement. Again, I have many other code analysis rules that product output both live in the editor and in the build output & errors list.
Where am I going wrong, please?
EDIT: @Guru Stron's question tickled my spidey sense, and I discovered that while the first method here does not produce IDE0063, the latter does. Why?
public Stream GenerateReport()
{
using (var reportContext = new ReportRenderContext(this.ReportTemplate))
{
reportContext.Render();
}
return this.FileStream;
}
public static int GetAreaOfImage(string fileName)
{
using (var image = Image.FromFile(fileName))
{
return image.Size.Width * image.Size.Height;
}
}
发布评论
评论(1)
声明基于范围的作品,资源将在范围末尾处置,因此下一个:
不是类似的to:
后一个类似于:
在某些情况下可能会有所不同,因此由于该编译器不会产生警告(编译器非常聪明并且同时非常愚蠢,它并不“真正”知道什么
this.fileStream
这样做。 this.fileStream 将引入运行时错误或this.fileStream
可以是一个相对较长的操作,并且dispose
的主要目的之一就是将不需要它们,就会有“反向”示例href =“ https://stackoverflow.com/a/19103343/2501279”> async上下文)。using
declaration works based on scope, resource will be disposed at the end of the scope, so the next one:Is not analogous to:
The latter one is analogous to:
Which can have difference in some cases, so due to this compiler will not produce the warning (compiler is very smart and very dumb at the same moment, it does not "really" know what
this.FileStream
does. For example it can access the same resource asReportRenderContext
(like some file in non-shareable fashion) and disposing afterreturn this.FileStream
will introduce runtime error. Orthis.FileStream
can be just a relatively long operation and one of the main purposes ofDispose
is to free resources as soon as they are not needed. There is "reverse" example when the 2nd snippet can fix a "bug" - in async context).