S2157 Add clone method just leads to other problem

Please provide

  • Operating system: Windows 10
  • SonarLint plugin version: 7.11.0.70000
  • Programming language you’re coding in: Java
  • Is connected mode used: No

And a thorough description of the problem / question:
Got S2157 reported on a Cloneable class w/o a clone method. So decided to add the method even if here the default behavior was fine (just a number of boolean fields in the class). I then got the S2185 Simply inherit, which should not then happen on a method that we asked to add specifically and also got S2995 to remove clone method and use some form of factory that seems totally contradictory to S2157.

Just wondering… when you override .clone() do you do so by calling super.clone() or by copying each boolean field - maybe even with some “field?true:false” or similar?

(I’m asking because S2185’s message is “Silly math should not be performed”.)

S2995 seems not related to java at all - maybe a typo?

Not, simple clone, but mistyped it: should read S2975 “clone” should not be overridden

Could you show the exact line to which SonarQube gives the S2185 and S2975 messages?

I still suspect your exact implementation of .clone() is what SonarQube barks at, not just the fact of
supplying one.

You could give the inherited .clone() a return-type of your current class,
and an explicit cast to this class. Afterall, that’s what it will always end up
returning. And once it returns a different type than the inherited one,
then the override should make sense also to sonarqube. (at least, that’s
what I’d expect - I haven’t tried it.)

And, given that your own class does implement cloning, you could even
catch superclass’ potential exception and declare this class’ .clone() as
not throwing exceptions.