pseries/hotplug-memory.c: Replace nested ifs by switch-case
PATCH 1/1 pseries/hotplug-memory.c: Replace nested ifs by switch-case
I noticed these nested ifs can be easily replaced by switch-cases, which can improve readability.
--- .../platforms/pseries/hotplug-memory.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
arch/powerpc/platforms/pseries/hotplug-memory.c +18 -8
@@ -880,34 +880,44 @@ int dlpar_memory(struct pseries_hp_errorlog *hp_elog)switch (hp_elog->action) {case PSERIES_HP_ELOG_ACTION_ADD : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_add_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_add_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_add_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;case PSERIES_HP_ELOG_ACTION_REMOVE : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_remove_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_remove_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_remove_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;
Signature · 1 line
2.20.1
Quoted 7 lines · Leonardo Bras
I noticed these nested ifs can be easily replaced by switch-cases, which can improve readability.
Signed-off-by Leonardo Bras <[email protected]>--- .../platforms/pseries/hotplug-memory.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
More LOC but seems to be the right thing to do
Quoted 59 lines
arch/powerpc/platforms/pseries/hotplug-memory.c +18
-8@@ -880,34 +880,44 @@ int dlpar_memory(struct pseries_hp_errorlog *hp_elog)switch (hp_elog->action) {case PSERIES_HP_ELOG_ACTION_ADD : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_add_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_add_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_add_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;case PSERIES_HP_ELOG_ACTION_REMOVE : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_remove_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_remove_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_remove_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;
Signature · 3 lines
Thanks, David / dhildenb
Michael Ellerman
reviewerQuoted 7 lines · Leonardo Bras
I noticed these nested ifs can be easily replaced by switch-cases, which can improve readability.
Signed-off-by Leonardo Bras <[email protected]>--- .../platforms/pseries/hotplug-memory.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
Thanks, this looks sensible.
Please use "powerpc/" as the prefix on your patches, eg. in this case:
"powerpc/pseries/hotplug-memory.c: Replace nested ifs by switch-case"
I'll fix it up this time when I apply.
cheers
Quoted 59 lines
arch/powerpc/platforms/pseries/hotplug-memory.c +18
-8@@ -880,34 +880,44 @@ int dlpar_memory(struct pseries_hp_errorlog *hp_elog)switch (hp_elog->action) {case PSERIES_HP_ELOG_ACTION_ADD : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_add_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_add_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_add_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;case PSERIES_HP_ELOG_ACTION_REMOVE : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_remove_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_remove_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_remove_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;Signature · 1 line
2.20.1
Quoted 9 lines · Michael Ellerman
Quoted 7 lines · Leonardo Bras
I noticed these nested ifs can be easily replaced by switch-cases, which can improve readability.
Signed-off-by Leonardo Bras <[email protected]>--- .../platforms/pseries/hotplug-memory.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
Quoted 6 lines
Thanks, this looks sensible.
Please use "powerpc/" as the prefix on your patches, eg. in this case:
"
powerpc/pseries/hotplug-memory.c: Replace nested ifs by switch-case"
Ok, I will make sure to do that next time.
Thanks!
Quoted 63 lines
I'll fix it up this time when I apply.
cheers
Quoted 59 lines
arch/powerpc/platforms/pseries/hotplug-memory.c +18
-8@@ -880,34 +880,44 @@ int dlpar_memory(struct pseries_hp_errorlog *hp_elog)switch (hp_elog->action) {case PSERIES_HP_ELOG_ACTION_ADD : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_add_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_add_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_add_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;case PSERIES_HP_ELOG_ACTION_REMOVE : -if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_COUNT ) { +switch (hp_elog->id_type) { +case PSERIES_HP_ELOG_ID_DRC_COUNT : count = hp_elog->_drc_u.drc_count; rc =dlpar_memory_remove_by_count (count); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_INDEX ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_INDEX : drc_index = hp_elog->_drc_u.drc_index; rc =dlpar_memory_remove_by_index (drc_index); - }else if (hp_elog->id_type ==PSERIES_HP_ELOG_ID_DRC_IC ) { +break ; +case PSERIES_HP_ELOG_ID_DRC_IC : count = hp_elog->_drc_u.ic.count; drc_index = hp_elog->_drc_u.ic.index; rc =dlpar_memory_remove_by_ic (count, drc_index); - }else { +break ; +default : rc = -EINVAL ; +break ; }break ;Signature · 1 line
2.20.1
Quoted 5 lines · Leonardo Bras
I noticed these nested ifs can be easily replaced by switch-cases, which can improve readability.
Signed-off-by Leonardo Bras <[email protected]>Reviewed-by David Hildenbrand <[email protected]>
Series applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/9616dda1aaddb2080122aaeab96ad7fc064e36b4
cheers