Skip to content

IH-640: Eliminate unnecessary usage of List.length to check for empty lists #5762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ocaml/idl/dtd_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let is_element = function Element (_, _, _) -> true | _ -> false
let string_of_attribute = function
| Attribute (n, options, default) ->
let opt_string =
if List.length options = 0 then
if options = [] then
"CDATA"
else
"(" ^ String.concat " | " options ^ ")"
Expand All @@ -59,7 +59,7 @@ let string_of_attribute = function
sprintf "%s %s %s" n opt_string def_string

let strings_of_attributes parent atts =
if List.length atts > 0 then
if atts <> [] then
let prefix = sprintf "<!ATTLIST %s " parent in
let body = List.map string_of_attribute atts in
(prefix :: body) @ [">"]
Expand Down
2 changes: 1 addition & 1 deletion ocaml/libs/ezxenstore/core/watch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let wait_for ~xs ?(timeout = 300.) (x : 'a t) =
Thread.create
(fun () ->
let r, _, _ = Unix.select [p1] [] [] timeout in
if List.length r > 0 then
if r <> [] then
()
else
try Xs_client_unix.Task.cancel task with _ -> ()
Expand Down
2 changes: 1 addition & 1 deletion ocaml/libs/http-lib/buf_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let fill_buf ~buffered ic timeout =
let buf_size = Bytes.length ic.buf in
let fill_no_exc timeout len =
let l, _, _ = Unix.select [ic.fd] [] [] timeout in
if List.length l <> 0 then (
if l <> [] then (
let n = Unix.read ic.fd ic.buf ic.max len in
ic.max <- n + ic.max ;
if n = 0 && len <> 0 then raise Eof ;
Expand Down
2 changes: 1 addition & 1 deletion ocaml/libs/xml-light2/xml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ let esc_pcdata data =

let str_of_attrs attrs =
let fmt s = Printf.sprintf s in
if List.length attrs > 0 then
if attrs <> [] then
" "
^ String.concat " "
(List.map (fun (k, v) -> fmt "%s=\"%s\"" k (esc_pcdata v)) attrs)
Expand Down
12 changes: 6 additions & 6 deletions ocaml/sdk-gen/powershell/gen_powershell_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,14 @@ and gen_destructor obj classname messages =
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if List.length asyncMessages > 0 then
( if asyncMessages <> [] then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
)
(ocaml_class_to_csharp_class classname)
(print_xenobject_params obj classname true true true)
( if List.length asyncMessages > 0 then
( if asyncMessages <> [] then
sprintf
"\n\
\ protected override bool GenerateAsyncParam\n\
Expand Down Expand Up @@ -725,7 +725,7 @@ and gen_remover obj classname messages =
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if List.length asyncMessages > 0 then
( if asyncMessages <> [] then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
Expand Down Expand Up @@ -790,7 +790,7 @@ and gen_setter obj classname messages =
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if List.length asyncMessages > 0 then
( if asyncMessages <> [] then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
Expand Down Expand Up @@ -855,7 +855,7 @@ and gen_adder obj classname messages =
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if List.length asyncMessages > 0 then
( if asyncMessages <> [] then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
Expand Down Expand Up @@ -1060,7 +1060,7 @@ and is_message_with_dynamic_params classname message =
let nonClassParams =
List.filter (fun x -> not (is_class x classname)) message.msg_params
in
if List.length nonClassParams > 0 || message.msg_async then
if nonClassParams <> [] || message.msg_async then
true
else
false
Expand Down
4 changes: 2 additions & 2 deletions ocaml/tests/common/alcotest_comparators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ let vdi_operations_set : API.vdi_operations_set Alcotest.testable =
)
(fun o1 o2 ->
List.length (intersect o1 o2) = List.length o1
&& List.length (set_difference o1 o2) = 0
&& List.length (set_difference o2 o1) = 0
&& set_difference o1 o2 = []
&& set_difference o2 o1 = []
)
2 changes: 1 addition & 1 deletion ocaml/xapi-cli-server/cli_frontend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3918,7 +3918,7 @@ let rio_help printer minimal cmd =
)
cmd.params
in
if List.length cmds > 0 then
if cmds <> [] then
List.iter docmd (List.map fst cmds)
else
let cmds =
Expand Down
123 changes: 64 additions & 59 deletions ocaml/xapi-cli-server/cli_operations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ let get_hosts_by_name_or_id rpc session_id name =

let get_host_by_name_or_id rpc session_id name =
let hosts = get_hosts_by_name_or_id rpc session_id name in
if List.length hosts = 0 then failwith ("Host " ^ name ^ " not found") ;
if hosts = [] then failwith ("Host " ^ name ^ " not found") ;
List.nth hosts 0

let get_host_from_session rpc session_id =
Expand Down Expand Up @@ -862,7 +862,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
]
in
let ops =
if List.length settable > 0 then
if settable <> [] then
( cli_name "param-set"
, ["uuid"]
, settable
Expand All @@ -877,7 +877,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
ops
in
let ops =
if List.length addable > 0 then
if addable <> [] then
ops
@ [
( cli_name "param-add"
Expand All @@ -902,7 +902,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
ops
in
let ops =
if List.length clearable > 0 then
if clearable <> [] then
ops
@ [
( cli_name "param-clear"
Expand Down Expand Up @@ -2928,13 +2928,7 @@ let event_wait_gen rpc session_id classname record_matches =
(List.map (fun r -> (r.name, fun () -> safe_get_field r)))
current_tbls
in
debug "Got %d records" (List.length all_recs) ;
(* true if anything matches now *)
let find_any_match recs =
let ls = List.map record_matches recs in
List.length (List.filter (fun x -> x) ls) > 0
in
find_any_match all_recs
List.exists record_matches all_recs
in
finally
(fun () ->
Expand Down Expand Up @@ -3305,9 +3299,9 @@ let do_host_op rpc session_id op params ?(multiple = true) ignore_params =
failwith "No matching hosts found"
| 1 ->
[op 1 (List.hd hosts)]
| _ ->
| len ->
if multiple && get_bool_param params "multiple" then
do_multiple (op (List.length hosts)) hosts
do_multiple (op len) hosts
else
failwith
( if not multiple then
Expand Down Expand Up @@ -3917,11 +3911,13 @@ let vm_install_real printer rpc session_id template name description params =
failwith
"SR specified via sr-uuid doesn't have the name specified via \
sr-name-label"
| None ->
if List.length sr_list > 1 then
| None -> (
match sr_list with
| [x] ->
Some x
| _ ->
failwith "Multiple SRs with that name-label found"
else
Some (List.hd sr_list)
)
)
else
sr_ref
Expand Down Expand Up @@ -4058,12 +4054,12 @@ let vm_install printer rpc session_id params =
List.fold_left filter_records_on_fields all_recs
(("name-label", name) :: filter_params)
in
match List.length templates with
| 0 ->
match templates with
| [] ->
failwith "No templates matched"
| 1 ->
(List.hd templates).getref ()
| _ ->
| [x] ->
x.getref ()
| _ :: _ :: _ ->
failwith "More than one matching template found"
in
if
Expand Down Expand Up @@ -4114,7 +4110,7 @@ let console fd _printer rpc session_id params =
| [] ->
marshal fd (Command (PrintStderr "No VM found\n")) ;
raise (ExitWithError 1)
| _ :: _ ->
| _ :: _ :: _ ->
marshal fd
(Command
(PrintStderr
Expand Down Expand Up @@ -4153,9 +4149,10 @@ let vm_uninstall_common fd _printer rpc session_id params vms =
(* add extra text if the VDI is being shared *)
let r = Client.VDI.get_record ~rpc ~session_id ~self:vdi in
Printf.sprintf "VDI: %s (%s) %s" r.API.vDI_uuid r.API.vDI_name_label
( if List.length r.API.vDI_VBDs <= 1 then
( match r.API.vDI_VBDs with
| [] | [_] ->
""
else
| _ :: _ :: _ ->
" ** WARNING: disk is shared by other VMs"
)
in
Expand Down Expand Up @@ -4477,18 +4474,15 @@ let vm_retrieve_wlb_recommendations printer rpc session_id params =
in
try
let vms = select_vms rpc session_id params [] in
match List.length vms with
| 0 ->
match vms with
| [] ->
failwith "No matching VMs found"
| 1 ->
| [x] ->
printer
(Cli_printer.PTable
[
("Host(Uuid)", "Stars, RecID, ZeroScoreReason")
:: table (List.hd vms)
]
[("Host(Uuid)", "Stars, RecID, ZeroScoreReason") :: table x]
)
| _ ->
| _ :: _ :: _ ->
failwith
"Multiple VMs found. Operation can only be performed on one VM at a \
time"
Expand Down Expand Up @@ -4628,7 +4622,7 @@ let vm_migrate printer rpc session_id params =
)
pifs
in
if List.length management_pifs = 0 then
if management_pifs = [] then
failwith
(Printf.sprintf "Could not find management PIF on host %s"
host_record.API.host_uuid
Expand Down Expand Up @@ -5026,7 +5020,7 @@ let vm_disk_remove printer rpc session_id params =
(fun x -> device = Client.VBD.get_userdevice ~rpc ~session_id ~self:x)
vm_record.API.vM_VBDs
in
if List.length vbd_to_remove < 1 then
if vbd_to_remove = [] then
failwith "Disk not found"
else
let vbd = List.nth vbd_to_remove 0 in
Expand All @@ -5052,7 +5046,7 @@ let vm_cd_remove printer rpc session_id params =
)
vm_record.API.vM_VBDs
in
if List.length vbd_to_remove < 1 then
if vbd_to_remove = [] then
raise (failwith "Disk not found")
else
let vbd = List.nth vbd_to_remove 0 in
Expand All @@ -5071,7 +5065,7 @@ let vm_cd_add printer rpc session_id params =
)
vdis
in
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found!") ;
if vdis = [] then failwith ("CD " ^ cd_name ^ " not found!") ;
let vdi = List.nth vdis 0 in
let op vm =
create_vbd_and_plug rpc session_id (vm.getref ()) vdi
Expand All @@ -5094,9 +5088,14 @@ let vm_cd_eject printer rpc session_id params =
(fun vbd -> Client.VBD.get_type ~rpc ~session_id ~self:vbd = `CD)
vbds
in
if List.length cdvbds = 0 then failwith "No CDs found" ;
if List.length cdvbds > 1 then
failwith "Two or more CDs found. Please use vbd-eject" ;
( match cdvbds with
| [] ->
failwith "No CDs found"
| [_] ->
()
| _ :: _ :: _ ->
failwith "Two or more CDs found. Please use vbd-eject"
) ;
let cd = List.hd cdvbds in
Client.VBD.eject ~rpc ~session_id ~vbd:cd
in
Expand All @@ -5113,13 +5112,18 @@ let vm_cd_insert printer rpc session_id params =
)
vdis
in
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found") ;
if List.length vdis > 1 then
failwith
("Multiple CDs named "
^ cd_name
^ " found. Please use vbd-insert and specify uuids"
) ;
( match vdis with
| [] ->
failwith ("CD " ^ cd_name ^ " not found")
| [_] ->
()
| _ :: _ :: _ ->
failwith
("Multiple CDs named "
^ cd_name
^ " found. Please use vbd-insert and specify uuids"
)
) ;
let op vm =
let vm_record = vm.record () in
let vbds = vm_record.API.vM_VBDs in
Expand All @@ -5131,15 +5135,16 @@ let vm_cd_insert printer rpc session_id params =
)
vbds
in
if List.length cdvbds = 0 then
raise
(Api_errors.Server_error
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
) ;
if List.length cdvbds > 1 then
failwith "Two or more empty CD devices found. Please use vbd-insert" ;
let cd = List.hd cdvbds in
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
match cdvbds with
| [] ->
raise
(Api_errors.Server_error
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
)
| [cd] ->
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
| _ :: _ :: _ ->
failwith "Two or more empty CD devices found. Please use vbd-insert"
in
ignore (do_vm_op printer rpc session_id op params ["cd-name"])

Expand Down Expand Up @@ -5555,7 +5560,7 @@ let pool_retrieve_wlb_report fd _printer rpc session_id params =
in
download_file_with_task fd rpc session_id filename Constants.wlb_report_uri
(Printf.sprintf "report=%s%s%s" (Http.urlencode report)
(if List.length other_params = 0 then "" else "&")
(if other_params = [] then "" else "&")
(String.concat "&"
(List.map
(fun (k, v) ->
Expand Down Expand Up @@ -5978,7 +5983,7 @@ let vm_is_bios_customized printer rpc session_id params =
let bios_strings =
Client.VM.get_bios_strings ~rpc ~session_id ~self:(vm.getref ())
in
if List.length bios_strings = 0 then
if bios_strings = [] then
printer
(Cli_printer.PMsg "The BIOS strings of this VM have not yet been set.")
else if bios_strings = Constants.generic_bios_strings then
Expand Down Expand Up @@ -7259,7 +7264,7 @@ let subject_role_common rpc session_id params =
let roles =
Client.Role.get_by_name_label ~rpc ~session_id ~label:role_name
in
if List.length roles > 0 then
if roles <> [] then
List.hd roles (* names are unique, there's either 0 or 1*)
else
Ref.null
Expand Down
Loading
Loading