StyleCop/代码分析错误 - 如何抑制

发布于 2024-10-19 09:50:51 字数 1200 浏览 1 评论 0 原文

当我在签入时运行代码分析时,我收到 SA0102(“文件中发现语法错误”)错误(另一个奇怪的问题,因为我认为 SA 是 StyleCop 问题?!)。通过添加/删除代码的过程,我推断出以下方法导致抛出此错误。据我所知,此方法是有效的,并且基于其他人的评论 - StyleCop 中似乎存在一些围绕此规则的错误。

无论如何,我的问题是如何抑制这个警告?我尝试将文件名更改为 .designer.cs,但这似乎没有效果(我已确保选中忽略设计器文件)。我对如何解决这个问题有点迷茫,因为如果不通过 StyleCop 签入政策,我就无法签入代码!另外,

作为参考,这是导致问题的方法 - 我怀疑在声明中使用可空值可能是问题所在。

编辑:我正在使用 StyleCop v4.4.1.2

编辑:如果我删除 IEnumerable>已选择,整数?根据参数 page = null,规则通过。

[HttpPost]
public ActionResult Search(string searchCriteria, IEnumerable`<int?`> selected, int? page = null)
{
    if (page.HasValue)
    {
        const int PageSize = 6;
        IEnumerable<MyClass> src = this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria));
        string rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * PageSize).Take(PageSize));
        return this.Json(new { rows, more = src.Count() > page * PageSize });
    }

    return this.View(@"Awesome\LookupList", this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria)));
}

I am getting an in SA0102 ('A syntax error has been discovered in the file') error being thrown when I run Code Analysis upon check-in (another strange issue as I thought SA was a StyleCop issue?!). Through a process of adding/removing code, I have deduced that the following method is causing this error to be thrown. As far as I am aware, this method is valid and based on comments from other - there appear to be some bugs in StyleCop surrounding this rule.

Anyway, my question is how can I suppress this warning? I have tried changing the filename to .designer.cs, however this appears to have no effect (I have made sure ignore designer files is checked). I am a little lost as to how I can resolve this issue as I can't check-in the code without passing the StyleCop check-in policy! Also, this

For reference, this is the method causing the issue - I suspect the use of nullables in the declaration may be the problem.

Edit: I am using StyleCop v4.4.1.2

Edit: If I remove the IEnumerable<int?> selected, int? page = null from the parameters, the rule passes.

[HttpPost]
public ActionResult Search(string searchCriteria, IEnumerable`<int?`> selected, int? page = null)
{
    if (page.HasValue)
    {
        const int PageSize = 6;
        IEnumerable<MyClass> src = this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria));
        string rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * PageSize).Take(PageSize));
        return this.Json(new { rows, more = src.Count() > page * PageSize });
    }

    return this.View(@"Awesome\LookupList", this.sectors.Where(o => (selected == null || !selected.Contains(o.Id)) && o.Name.Contains(searchCriteria)));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2024-10-26 09:50:51

好的,如果参数中使用了可空类型,就会导致问题。

OK, if would appear to be the use of nullable types in the parameter that is causing the issue.

默嘫て 2024-10-26 09:50:51

刚刚用 4.4.1.2 检查了您的示例,它工作正常。

您确定您的签到政策使用 4.4.1.2 吗?它在服务器上运行,而不是在您的本地计算机上 - 因此您可能安装了 StyleCop 4.4.1.2,但服务器使用其他版本。

以前的 StyleCop 版本(例如 4.4.0.14)确实存在一些与可选参数相关的错误。

Just checked your example with 4.4.1.2 and it works fine.

Are you sure that your check-in policy uses 4.4.1.2? It runs on server, not on your local machine - so you may have StyleCop 4.4.1.2 installed, but server uses some other version.

Previous StyleCop versions (for instance, 4.4.0.14) were having a couple of bugs connected with optional parameters indeed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文