Go rule "Two branches in a conditional structure..." should consider that this is necessary sometimes in type switches

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  Developer Edition Version 8.1 (build 31237)
  • what are you trying to achieve
    The rule

Two branches in a conditional structure should not have exactly the same implementation

fails even if (I think) it is justified to have more then one equal line of code in some cases.

  • what have you tried so far to achieve this

This are two structs of an external library:

type Foo struct {
   field1 int
   field2 int
   field3 int
   specialA int
   specialB int
}

type Bar struct {
   field1 int
   field2 int
   field3 int
   otherSpecialA int
   otherSpecialA int
}

This is my code:

type Common struct {
   field1 int
   field2 int
   field3 int
}


func getCommonHeader(header interface{}) Common {
	switch input := header.(type) {
	case band.Foo:
		return Common{
                     field1: input.field1,
                     field2: input.field2,
                     field3: input.field3,
                }
	case timeseries.Bar:
		return Common{
                     field1: input.field1,
                     field2: input.field2,
                     field3: input.field3,
                }
	default: 
              panic("...")
}

This code clearly fails because the two branches in the switch statement are the same. I don’t see a more ideomatic way to extract the common fields of the two external structs. Woudln’t be it ok in such cases to have the code be duplicated in the two branches?

Hi Guenther,
Sorry for the late answer. I created SONARSLANG-554 to remove the false positives.
Thanks for your feedback,
Alban