S3172 False-positive, pattern matching switch expression, deems it a chain

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for?

  • Which rule?
    S3172 Delegates should not be subtracted

  • Why do you believe it’s a false-positive/false-negative?
    The exact same code but written as a traditional switch statement does not trigger this analyzer

  • Are you using

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

    public static void UnSubscribeActions<T>(this IEnumerable<IDbSaveActions> dbSaveActions, Action<T> action, /*enum to switch on*/RequiredActions requiredAction) where T : DbContext
        foreach (var saveAction in dbSaveActions.Where(x => x.RequiredActions.HasFlag(requiredAction)))
            action -= requiredAction switch
                RequiredActions.PreSaveAction => saveAction.Pre,
                RequiredActions.PostSaveAction => saveAction.Post,
                _ => throw new NotImplementedException(NotImplemented(requiredAction)),
1 Like

Hi @JJoensuu, welcome to the community!

I can confirm this as a FP, you can follow the ticket on GitHub. I’ll add a reproducer shortly in our repository.

Thanks for your feedback!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.