CodeQL documentation

Duplicate switch case

ID: go/duplicate-switch-case
Kind: problem
Security severity: 
Severity: error
Precision: very-high
Tags:
   - maintainability
   - correctness
   - external/cwe/cwe-561
Query suites:
   - go-security-and-quality.qls

Click to see the query in the CodeQL repository

If two cases in a ‘switch’ statement are identical, the second case will never be executed. This most likely indicates a copy-paste error where the first case was copied and then not properly adjusted.

Recommendation

Examine the two cases to find out what they were meant to check. If both the conditions and the bodies are identical, then the second case is duplicate code that can be deleted. Otherwise, the second case needs to be adjusted.

Example

In the example below, the function controller checks its parameter msg to determine what operation it is meant to perform. However, the condition of the second case is identical to that of the first, so this case will never be executed.

package main

func controller(msg string) {
	switch {
	case msg == "start":
		start()
	case msg == "start":
		stop()
	default:
		panic("Message not understood.")
	}
}

Most likely, the second case should compare msg to "stop":

package main

func controllerGood(msg string) {
	switch {
	case msg == "start":
		start()
	case msg == "stop":
		stop()
	default:
		panic("Message not understood.")
	}
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy