Repository: pingcap/tidb
File: pkg/planner/core/find_best_task.go (Go)

Code before:
		}

		if hintTask.Invalid() && hasNormalPreferTask(baseLP.Self(), initState, pp, childTasks) {
			if normalPreferTask.Invalid() {
				normalPreferTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalPreferTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalPreferTask = curTask
			}
		}

		if hintTask.Invalid() && normalPreferTask.Invalid() {
			if normalIterTask.Invalid() {
				normalIterTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalIterTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalIterTask = curTask
			}
		}
	}

	if err != nil {
		return nil, false, err
	}

	returnedTask := base.InvalidTask
	var hintCanWork bool
	if !hintTask.Invalid() {
		returnedTask = hintTask
		hintCanWork = true
	} else if !normalPreferTask.Invalid() {
		returnedTask = normalPreferTask
	} else if !normalIterTask.Invalid() {
		returnedTask = normalIterTask
	}

	if !hintCanWork && returnedTask != nil && !returnedTask.Invalid() {
		// It means there is no hint or hint is not applicable.
		// So record hint warning if necessary.
		if warn := recordWarnings(baseLP.Self(), prop, addEnforcer); warn != nil {
			returnedTask.AppendWarning(warn)
		}
	}
	return returnedTask, hintCanWork, nil
}

// TODO: remove the taskTypeSatisfied function, it is only used to check the task type in the root, cop, mpp task.
func taskTypeSatisfied(propRequired *property.PhysicalProperty, childTask base.Task) bool {
	// check the root, cop, mpp task type matched the required property.

Review comment (refactor):
This error check is unreachable dead code. The `err` variable is only assigned within the loop at line 157, and any non-nil error immediately returns at line 159. At this point, `err` is guaranteed to be nil. Remove this check.
```suggestion

```

Code after:
			}
		}

		if hintTask.Invalid() && hasNormalPreferTask(baseLP.Self(), initState, pp, childTasks) {
			if normalPreferTask.Invalid() {
				normalPreferTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalPreferTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalPreferTask = curTask
			}
		}

		if hintTask.Invalid() && normalPreferTask.Invalid() {
			if normalIterTask.Invalid() {
				normalIterTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalIterTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalIterTask = curTask
			}
		}
	}

	returnedTask := base.InvalidTask
	var hintCanWork bool
	if !hintTask.Invalid() {
		returnedTask = hintTask
		hintCanWork = true
	} else if !normalPreferTask.Invalid() {
		returnedTask = normalPreferTask
	} else if !normalIterTask.Invalid() {
		returnedTask = normalIterTask
	}

	if !hintCanWork && returnedTask != nil && !returnedTask.Invalid() {
		// It means there is no hint or hint is not applicable.
		// So record hint warning if necessary.
		if warn := recordWarnings(baseLP.Self(), prop, addEnforcer); warn != nil {
			returnedTask.AppendWarning(warn)
		}
	}
	return returnedTask, hintCanWork, nil
}

// TODO: remove the taskTypeSatisfied function, it is only used to check the task type in the root, cop, mpp task.
func taskTypeSatisfied(propRequired *property.PhysicalProperty, childTask base.Task) bool {
	// check the root, cop, mpp task type matched the required property.
	if childTask == nil || propRequired == nil {
		// index join v1 may occur that propRequired is nil, and return task is nil too. Return true
		// to make sure let it walk through the following logic.