S6647 false positive when parent constructor is protected

Hello,

We got “s6647 - Unnecessary constructors should be removed” code smell when we use an abstract parent class with protected constructor.
I think that it is a false positive report because the code cannot be built without these constructors.

A typical example:

abstract class Parent {
    protected constructor(
        private injector: Injector
      ) {
      }
}

class Child extends Parent {
    constructor( injector: Injector ) {
        super( injector );
    }
}

Hi,

Welcome to the community and thanks for this report!

Can you tell us where you’re seeing this?

  • SonarCloud?
  • SonarQube - which version?
  • SonarLint - which IDE/version?
    • in connected mode with SonarQube or SonarCloud?

 
Thx,
Ann

Hi,

Thanks for the welcome.
It appears in sonarcloud.

Thx, Joe.

1 Like

Hi @joe_csuti,

thanks for reporting this. Seems a duplicate of Sonar smell typescript:S6647 Unnecessary constructors should be removed, however I could not reproduce the issue back then and now I’m able to.

We are investigating the rule and we’ll come back to you.

Cheers,
Victor

Hello @joe_csuti,

can you develop further what you mean by “Cannot be built”? If it’s due to the protected constructor of the parent class, you can provide a factory method?

abstract class Parent {
  protected constructor(
      private x: number
  ) {
  }
}

class Child extends Parent {
  static createChild() {
    return new Child(0);
  }
}

const child = Child.createChild();

The constructor of the child class is not needed when the implementation is empty. The only reason your code cannot be built in the given example is that the parent constructor is protected, right? Can you further develop your point?

I’m not saying this is not an FP, but would like to further understand the reasoning. I understand also that providing a factory is less preferable than an empty constructor.

Thanks,
Victor