CodeQL documentation

Duplicate ‘if’ condition

ID: go/duplicate-condition
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 conditions in an ‘if’-’else if’ chain are identical, the second condition will never hold. This most likely indicates a copy-paste error where the first condition was copied and then not properly adjusted.

Recommendation

Examine the two conditions to find out what they were meant to check. If both the conditions and the branches that depend on them are identical, then the second branch is duplicate code that can be deleted. Otherwise, the second condition 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 comparison in the ‘else if’ is identical to the comparison in the ‘if’, so this branch will never be taken.

package main

func controller(msg string) {
	if msg == "start" {
		start()
	} else if msg == "start" {
		stop()
	} else {
		panic("Message not understood.")
	}
}

Most likely, the ‘else if’ branch should compare msg to "stop":

package main

func controllerGood(msg string) {
	if msg == "start" {
		start()
	} else if msg == "stop" {
		stop()
	} else {
		panic("Message not understood.")
	}
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy