StrictlySocial
StrictlySocial

Reputation: 697

StyleCop/Code Analysis Error - How to suppress

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)));
}

Upvotes: 1

Views: 2771

Answers (2)

Oleg Shuruev
Oleg Shuruev

Reputation: 1349

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.

Upvotes: 0

StrictlySocial
StrictlySocial

Reputation: 697

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

Upvotes: 1

Related Questions