声纳问题 Assignments should not be redundant (squid:S4165)

Sonar issue Assignments should not be redundant (squid:S4165)

private void updateEmployee(String employeeCode, UpdateUserDto updateUserDto){
    Employee emp = getEmpDetails(employeeCode);
    emp = updateMobile&Email(emp, updateUserDto.getMobile(), updateUserDto.getMail());
    // Remove this useless assignment; "emp" already holds the assigned value along all execution paths.

    ...
    ...
    emp.setisActive(updateUserDto.getIsActive());
    empRepo.save(emp);
}

private Employee updateMobile&Email(Employee emp, String mobile, String mail){
    if(emp.getMobile() == null && (mobile != null || mobile.isBlank())){
        emp.setMobile(mobile);
    }
    if(emp.getMail() == null && (getMail != null || getMail.isBlank())){
        emp.setMail(mail);
    }
    return emp;
}

由于连接的复杂性,我做了一些功能。上面一个只是举例,代码比较大

删除这个无用的赋值; "emp" 已在所有执行路径上保留分配的值。

在 updateEmployee() 的第 2 行

声纳问题详情

Assignments should not be redundant (squid:S4165)

Noncompliant
a = b;
c = a;
b = c; // Noncompliant: c and b are already the same

Compilant
a = b;
c = a;

emp 参数将通过引用传递给 updateMobile&Email 方法,这意味着第 2 行赋值是不必要的。

我建议将 updateMobile&Email 更新为 void return 类型。