C# S6966 with FluentValidation

  • What language is this for?
    C# (csharp)
  • Which rule?
    S6966 - Awaitable method should be used

We are using SonarCloud

  • Why do you believe it’s a false-positive/false-negative?
    With the FluentValidation library, we derive our validators from AbstractValidator, this means our validator classes will have a Validate method and a ValidateAsync method with the same signature.
    The problem here is that validators are usually only CPU bound work, so using the async overload actually adds overhead.
    Is it possible to conditionally disable this rule? I still want the rule to apply for other async overloads because it is definitely helping us there but I want to be able to let it exclude Validate methods because these are false positives for us.

  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

Validator:

public class ContractIdValidator : AbstractValidator<ContractId>
  {
    public ContractIdValidator()
    {
      this.RuleFor<string>((Expression<Func<ContractId, string>>) (model => model.Id)).Must<ContractId, string>((Func<string, bool>) (p => int.TryParse(p, out int _))).WithMessage<ContractId, string>("Id must be an int.");
    }
  }

Instance where rule triggers:

// method signature
var validationResult = _contractIdValidator.Validate(myId);
// rest of method

This problem is picked up in lots of places in the repo, so I would like to avoid disabling the rule in the code with comments.

Hey there.

I’m not an expert here, but is it possible you’re running into this already reported false-positive? Fix S6966 FP: Methods with different signatures should not trigger this rule. · Issue #9265 · SonarSource/sonar-dotnet · GitHub

It’s slightly different. In the case of FluentValidation the Validate and ValidateAsync methods do have the same signature and I can even imagine this being a true positive for some projects. But in ours we don’t do any I/O operations in our validators so it doesn’t make sense to use the async overload.

I’ll flag this for our C# experts :slight_smile:

1 Like

Hello @evalann,

Thank you for reporting this false positive. I have created Fix S6966 FP: FuentValidation ValidateAsync should not be proposed · Issue #9339 · SonarSource/sonar-dotnet · GitHub and added the issue to our current sprint.

2 Likes

Thanks @Martin_Strecker! For what it’s worth, I’m pleasantly surprised with how fast the feedback was on this post.

2 Likes