C# Use of private constructor not detected when using new keyword

Then tell us:

  • What language is this for?
    C#

  • Which rule?
    csharpsquid:S3453 and csharpsquid:S1144

  • Why do you believe it’s a false-positive/false-negative?
    I do indeed use this private constructor in a static list just with the new keyword instead of the full qualified class name.

  • Are you using

    • SonarQube Cloud?
      Yes sonarcloud team services
    • SonarQube for IDE - which IDE/version?
      10.22.0.81244 in connected mode with our sonarcloud
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)’

The file creating the issue is in the following github gist. I anonymized the file but the issue remains the same.

Hello @lars-at-joz thanks for the report!

Unfortunately I cannot reproduce the issue. Can you confirm that I got everything correct?

  1. I had to simplify the code snippet a bit, but that shouldn’t change much. Does this snippet raise S1144 for you?
    internal class LogConverter
    {
        private LogConverter(string regex, string alertType)
        {
            AlertType = alertType;
        }

        public string AlertType { get; }

        public static readonly LogConverter[] Converters =
        [
            new("^AGV route completed", "0xAAFF0001"),
            new("^AGV route failed during execution", "0xAAFF0002"),
        ];
    }
  1. You are using SonarQube for IDE in IntelliJ’s Rider (I inferred that from the version you mentioned).

We did not touch S1144 in a while and the version you are using is recent. So it’s not quite obvious what could be different. Please double check that my reproduction attempt does in fact raise the issue for you and then I’ll try to investigate deeper.

Hi I’m sorry I was on vacation for a full month.
I’ll try and check it somewhere this week and update you.

I also can not reproduce the issue. The only way to trigger S1144 is actually removing the new() statements, which would result in true positive.

1 Like

I tried to reproduce it today with both the sample and the code that originally caused the issue. I am now unable to reproduce this issue myself in either the IDE or sonarcloud.io.

I’m not sure what happend or if I mis saw this but I think we can close this topic since there is no way to move forward and the issue seems resolved.

1 Like

Thanks for confirming!