我正在尝试对包含实体模型(edmx文件)的程序集运行代码分析.在项目属性中,我检查了"从生成的代码中抑制结果"选项,但是我仍然收到很多与自动生成的EF代码有关的CA错误.
有没有人经历过这个?是否有解决方法?
关于VS2010可能过于严格地应用代码分析规则CA2000的事实,有很多问题,但我似乎遇到了应该应用它的情况,但事实并非如此.
请考虑以下代码:
Image srcImage = Image.FromFile(source);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
newImage.Save(destination, ImageFormat.Jpeg);
Run Code Online (Sandbox Code Playgroud)
现在,如果我在Visual Studio 2010中运行Code Analysis,它会抱怨newImage没有处理(很容易修复,把它放在另一个使用块),但是它没有抱怨srcImage(它也有Dispose()方法)我永远不会打电话).有谁知道为什么Code Analysis不会在这里抱怨?
当存在需要的IFormatProvider过载但在代码中未使用此过载时,CA1305会引发.
例如,以下代码引发此警告:
string.Format("{0} - {1}", id, name);
Run Code Online (Sandbox Code Playgroud)
摆脱此警告的明显方法是指定要使用的文化:
string.Format(CultureInfo.InvariantCulture, "{0} - {1}", id, name);
Run Code Online (Sandbox Code Playgroud)
但是,我觉得这太冗长了.
有哪些替代品?
我正在尝试将自定义FxCop(=代码分析)规则从Visual Studio 2010迁移到Visual Studio 2012.
到目前为止,它们工作正常,除了一件事:它们的名称在结果窗口中是空白的(View- > Other windows- > Code Analysis),只有CheckId和Resolution显示,而名称显示为Microsoft的规则:

令人感到奇怪的是,这个名字是在规则集编辑器中可见:

我的规则出了什么问题?
以下是两个规则在MSBuild输出中的显示方式(那里有一些可疑的东西,但我不明白为什么我有不同的消息):
6>c:\Users\Me\MySolution\MyProject\Program.cs(15): warning : CA1804 : Microsoft.Performance : 'Foo<T>.SomeMethod()' declares a variable, 'z', of type 'int', which is never used or is only assigned to. Use this variable or remove it.
6>MSBUILD : warning : CF1001 : CustomRules.ThreadSafety : The public Public Field "Tests.Program.Foo" must be preceded of readonly
Run Code Online (Sandbox Code Playgroud)
以下是我的规则的XML文件中的规则声明:
<?xml version="1.0" encoding="utf-8" ?>
<Rules …Run Code Online (Sandbox Code Playgroud) Visual Studio 2013是否具有显示未使用功能的功能?
删除这些功能的最佳解决方案是什么?
我正在编写一个Roslyn 代码分析器,我想确定一个async方法是否不采用CancellationToken,然后建议添加它的代码修复:
//Before Code Fix:
public async Task Example(){}
//After Code Fix
public async Task Example(CancellationToken token){}
Run Code Online (Sandbox Code Playgroud)
我已经DiagnosticAnalyzer通过检查 将连接起来以正确报告诊断methodDeclaration.ParameterList.Parameters,但我找不到用于将 a 添加Paramater到的Roslyn APIParameterList里面CodeFixProvider。
这是我到目前为止所得到的:
private async Task<Document> HaveMethodTakeACancellationTokenParameter(
Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
var method = syntaxNode as MethodDeclarationSyntax;
// what goes here?
// what I want to do is:
// method.ParameterList.Parameters.Add(
new ParameterSyntax(typeof(CancellationToken));
//somehow return the Document from method
} …Run Code Online (Sandbox Code Playgroud) 我有几个类,它们的字段仅通过反射分配或使用。因此,这些字段会导致以下警告:
CS0169: The field [...] is never used
CS0649: Field [...] is never assigned to, and will always have its default value 0
Run Code Online (Sandbox Code Playgroud)
解决方案是在所有这些类上使用 SuppressMessageAttribute,但这似乎不干净,因为相关类已经具有自定义属性。
我更喜欢这样的东西:
[KeepMyFields]
class SomeClass
{
int usedField;
}
class KeepMyFieldsAttribute : System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
{
// [...]
}
Run Code Online (Sandbox Code Playgroud)
但是,SuppressMessageAttribute 是密封的。除了向每个相关类添加代码片段之外,我还有什么其他选择?自定义 MSBuild 任务是否合适,或者是否有更简单的方法?
目前,在使用为我们的项目定义的自定义代码分析规则集配置 ASP.Net Core 2.0 应用程序后,我收到 CA0055 和 CA0052 代码分析错误(使用 Visual Studio 2017 和使用 MSBuild 命令)。通过提供的不同解决方案尝试了不同的方法来解决这些错误,但没有运气。请帮助我们解决这些代码分析错误...
以下是错误详情
MSBUILD:错误:CA0055:无法识别“D:\Source\Temp\WebClient\Business\bin\Debug\netco reapp2.0\Business.dll”的平台。[D:\Source\Temp\WebClient\Business\Business.csproj] MSBUILD:错误:CA0052:未选择任何目标。[D:\Source\Temp\WebClient\Business\Business.csproj] 代码分析完成 -- 2 个错误,0 个警告
谢谢,维努马达夫。
使用 Visual studio 2019发布 ASP.NET Core 3 API netcoreapp3.1项目时,也会发布多个代码分析 dll。此外,这些 dll 还以 13 种语言发布,外加英文版本。
如何防止 Visual Studio 在生产服务器上发布代码分析 dll(包括英文版本)?
这些 dll 是:
Microsoft.CodeAnalysis.CSharp.resources.dll
Microsoft.CodeAnalysis.CSharp.Workspaces.resources.dll
Microsoft.CodeAnalysis.resources.dll
Microsoft.CodeAnalysis.Workspaces.resources.dll
Run Code Online (Sandbox Code Playgroud)
我在 Window 10 上使用 Microsoft Visual Studio Enterprise 2019 版本 16.4.5。
发布配置为:
Configuration: Release x64
Target framework: netcoreapp3.1
Deployment mode: Framework dependent
Target runtime: win-x64
File Publish Options:
Produce single file: no
Enable ReadyToRun compilation: yes
Delete all existing files prior to publish: yes
Run Code Online (Sandbox Code Playgroud) dll code-analysis publishing visual-studio-2019 netcoreapp3.1
有谁知道这个错误:代码分析失败,出现异常:org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments:意外的2个声明?
感谢:D
error-handling code-analysis compiler-errors jetbrains-ide android-studio
code-analysis ×10
c# ×4
.net ×2
asp.net-core ×1
dispose ×1
dll ×1
fxcop ×1
localization ×1
msbuild-task ×1
publishing ×1
roslyn ×1