Skip to content

Commit 845ffdd

Browse files
author
Andrii Sultanov
committed
Eliminate unnecessary usage of List.length to check for empty lists
Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
1 parent d768dd6 commit 845ffdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+159
-134
lines changed

ocaml/idl/dtd_backend.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let is_element = function Element (_, _, _) -> true | _ -> false
4141
let string_of_attribute = function
4242
| Attribute (n, options, default) ->
4343
let opt_string =
44-
if List.length options = 0 then
44+
if options = [] then
4545
"CDATA"
4646
else
4747
"(" ^ String.concat " | " options ^ ")"
@@ -59,7 +59,7 @@ let string_of_attribute = function
5959
sprintf "%s %s %s" n opt_string def_string
6060

6161
let strings_of_attributes parent atts =
62-
if List.length atts > 0 then
62+
if atts <> [] then
6363
let prefix = sprintf "<!ATTLIST %s " parent in
6464
let body = List.map string_of_attribute atts in
6565
(prefix :: body) @ [">"]

ocaml/libs/ezxenstore/core/watch.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ let wait_for ~xs ?(timeout = 300.) (x : 'a t) =
5151
Thread.create
5252
(fun () ->
5353
let r, _, _ = Unix.select [p1] [] [] timeout in
54-
if List.length r > 0 then
54+
if r <> [] then
5555
()
5656
else
5757
try Xs_client_unix.Task.cancel task with _ -> ()

ocaml/libs/http-lib/buf_io.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ let fill_buf ~buffered ic timeout =
8080
let buf_size = Bytes.length ic.buf in
8181
let fill_no_exc timeout len =
8282
let l, _, _ = Unix.select [ic.fd] [] [] timeout in
83-
if List.length l <> 0 then (
83+
if l <> [] then (
8484
let n = Unix.read ic.fd ic.buf ic.max len in
8585
ic.max <- n + ic.max ;
8686
if n = 0 && len <> 0 then raise Eof ;

ocaml/libs/xml-light2/xml.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ let esc_pcdata data =
121121

122122
let str_of_attrs attrs =
123123
let fmt s = Printf.sprintf s in
124-
if List.length attrs > 0 then
124+
if attrs <> [] then
125125
" "
126126
^ String.concat " "
127127
(List.map (fun (k, v) -> fmt "%s=\"%s\"" k (esc_pcdata v)) attrs)

ocaml/sdk-gen/powershell/gen_powershell_binding.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,14 @@ and gen_destructor obj classname messages =
644644
Licence.bsd_two_clause
645645
(ocaml_class_to_csharp_class classname)
646646
(qualified_class_name classname)
647-
( if List.length asyncMessages > 0 then
647+
( if asyncMessages <> [] then
648648
"\n [OutputType(typeof(XenAPI.Task))]"
649649
else
650650
""
651651
)
652652
(ocaml_class_to_csharp_class classname)
653653
(print_xenobject_params obj classname true true true)
654-
( if List.length asyncMessages > 0 then
654+
( if asyncMessages <> [] then
655655
sprintf
656656
"\n\
657657
\ protected override bool GenerateAsyncParam\n\
@@ -725,7 +725,7 @@ and gen_remover obj classname messages =
725725
Licence.bsd_two_clause
726726
(ocaml_class_to_csharp_class classname)
727727
(qualified_class_name classname)
728-
( if List.length asyncMessages > 0 then
728+
( if asyncMessages <> [] then
729729
"\n [OutputType(typeof(XenAPI.Task))]"
730730
else
731731
""
@@ -790,7 +790,7 @@ and gen_setter obj classname messages =
790790
Licence.bsd_two_clause
791791
(ocaml_class_to_csharp_class classname)
792792
(qualified_class_name classname)
793-
( if List.length asyncMessages > 0 then
793+
( if asyncMessages <> [] then
794794
"\n [OutputType(typeof(XenAPI.Task))]"
795795
else
796796
""
@@ -855,7 +855,7 @@ and gen_adder obj classname messages =
855855
Licence.bsd_two_clause
856856
(ocaml_class_to_csharp_class classname)
857857
(qualified_class_name classname)
858-
( if List.length asyncMessages > 0 then
858+
( if asyncMessages <> [] then
859859
"\n [OutputType(typeof(XenAPI.Task))]"
860860
else
861861
""
@@ -1060,7 +1060,7 @@ and is_message_with_dynamic_params classname message =
10601060
let nonClassParams =
10611061
List.filter (fun x -> not (is_class x classname)) message.msg_params
10621062
in
1063-
if List.length nonClassParams > 0 || message.msg_async then
1063+
if nonClassParams <> [] || message.msg_async then
10641064
true
10651065
else
10661066
false

ocaml/tests/common/alcotest_comparators.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ let vdi_operations_set : API.vdi_operations_set Alcotest.testable =
5959
)
6060
(fun o1 o2 ->
6161
List.length (intersect o1 o2) = List.length o1
62-
&& List.length (set_difference o1 o2) = 0
63-
&& List.length (set_difference o2 o1) = 0
62+
&& set_difference o1 o2 = []
63+
&& set_difference o2 o1 = []
6464
)

ocaml/xapi-cli-server/cli_frontend.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3918,7 +3918,7 @@ let rio_help printer minimal cmd =
39183918
)
39193919
cmd.params
39203920
in
3921-
if List.length cmds > 0 then
3921+
if cmds <> [] then
39223922
List.iter docmd (List.map fst cmds)
39233923
else
39243924
let cmds =

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ let get_hosts_by_name_or_id rpc session_id name =
229229

230230
let get_host_by_name_or_id rpc session_id name =
231231
let hosts = get_hosts_by_name_or_id rpc session_id name in
232-
if List.length hosts = 0 then failwith ("Host " ^ name ^ " not found") ;
232+
if hosts = [] then failwith ("Host " ^ name ^ " not found") ;
233233
List.nth hosts 0
234234

235235
let get_host_from_session rpc session_id =
@@ -862,7 +862,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
862862
]
863863
in
864864
let ops =
865-
if List.length settable > 0 then
865+
if settable <> [] then
866866
( cli_name "param-set"
867867
, ["uuid"]
868868
, settable
@@ -877,7 +877,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
877877
ops
878878
in
879879
let ops =
880-
if List.length addable > 0 then
880+
if addable <> [] then
881881
ops
882882
@ [
883883
( cli_name "param-add"
@@ -902,7 +902,7 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
902902
ops
903903
in
904904
let ops =
905-
if List.length clearable > 0 then
905+
if clearable <> [] then
906906
ops
907907
@ [
908908
( cli_name "param-clear"
@@ -2928,13 +2928,7 @@ let event_wait_gen rpc session_id classname record_matches =
29282928
(List.map (fun r -> (r.name, fun () -> safe_get_field r)))
29292929
current_tbls
29302930
in
2931-
debug "Got %d records" (List.length all_recs) ;
2932-
(* true if anything matches now *)
2933-
let find_any_match recs =
2934-
let ls = List.map record_matches recs in
2935-
List.length (List.filter (fun x -> x) ls) > 0
2936-
in
2937-
find_any_match all_recs
2931+
List.exists record_matches all_recs
29382932
in
29392933
finally
29402934
(fun () ->
@@ -3305,9 +3299,9 @@ let do_host_op rpc session_id op params ?(multiple = true) ignore_params =
33053299
failwith "No matching hosts found"
33063300
| 1 ->
33073301
[op 1 (List.hd hosts)]
3308-
| _ ->
3302+
| len ->
33093303
if multiple && get_bool_param params "multiple" then
3310-
do_multiple (op (List.length hosts)) hosts
3304+
do_multiple (op len) hosts
33113305
else
33123306
failwith
33133307
( if not multiple then
@@ -3917,11 +3911,13 @@ let vm_install_real printer rpc session_id template name description params =
39173911
failwith
39183912
"SR specified via sr-uuid doesn't have the name specified via \
39193913
sr-name-label"
3920-
| None ->
3921-
if List.length sr_list > 1 then
3914+
| None -> (
3915+
match sr_list with
3916+
| [x] ->
3917+
Some x
3918+
| _ ->
39223919
failwith "Multiple SRs with that name-label found"
3923-
else
3924-
Some (List.hd sr_list)
3920+
)
39253921
)
39263922
else
39273923
sr_ref
@@ -4058,12 +4054,12 @@ let vm_install printer rpc session_id params =
40584054
List.fold_left filter_records_on_fields all_recs
40594055
(("name-label", name) :: filter_params)
40604056
in
4061-
match List.length templates with
4062-
| 0 ->
4057+
match templates with
4058+
| [] ->
40634059
failwith "No templates matched"
4064-
| 1 ->
4065-
(List.hd templates).getref ()
4066-
| _ ->
4060+
| [x] ->
4061+
x.getref ()
4062+
| _ :: _ :: _ ->
40674063
failwith "More than one matching template found"
40684064
in
40694065
if
@@ -4114,7 +4110,7 @@ let console fd _printer rpc session_id params =
41144110
| [] ->
41154111
marshal fd (Command (PrintStderr "No VM found\n")) ;
41164112
raise (ExitWithError 1)
4117-
| _ :: _ ->
4113+
| _ :: _ :: _ ->
41184114
marshal fd
41194115
(Command
41204116
(PrintStderr
@@ -4153,9 +4149,10 @@ let vm_uninstall_common fd _printer rpc session_id params vms =
41534149
(* add extra text if the VDI is being shared *)
41544150
let r = Client.VDI.get_record ~rpc ~session_id ~self:vdi in
41554151
Printf.sprintf "VDI: %s (%s) %s" r.API.vDI_uuid r.API.vDI_name_label
4156-
( if List.length r.API.vDI_VBDs <= 1 then
4152+
( match r.API.vDI_VBDs with
4153+
| [] | [_] ->
41574154
""
4158-
else
4155+
| _ :: _ :: _ ->
41594156
" ** WARNING: disk is shared by other VMs"
41604157
)
41614158
in
@@ -4477,18 +4474,15 @@ let vm_retrieve_wlb_recommendations printer rpc session_id params =
44774474
in
44784475
try
44794476
let vms = select_vms rpc session_id params [] in
4480-
match List.length vms with
4481-
| 0 ->
4477+
match vms with
4478+
| [] ->
44824479
failwith "No matching VMs found"
4483-
| 1 ->
4480+
| [x] ->
44844481
printer
44854482
(Cli_printer.PTable
4486-
[
4487-
("Host(Uuid)", "Stars, RecID, ZeroScoreReason")
4488-
:: table (List.hd vms)
4489-
]
4483+
[("Host(Uuid)", "Stars, RecID, ZeroScoreReason") :: table x]
44904484
)
4491-
| _ ->
4485+
| _ :: _ :: _ ->
44924486
failwith
44934487
"Multiple VMs found. Operation can only be performed on one VM at a \
44944488
time"
@@ -4628,7 +4622,7 @@ let vm_migrate printer rpc session_id params =
46284622
)
46294623
pifs
46304624
in
4631-
if List.length management_pifs = 0 then
4625+
if management_pifs = [] then
46324626
failwith
46334627
(Printf.sprintf "Could not find management PIF on host %s"
46344628
host_record.API.host_uuid
@@ -5026,7 +5020,7 @@ let vm_disk_remove printer rpc session_id params =
50265020
(fun x -> device = Client.VBD.get_userdevice ~rpc ~session_id ~self:x)
50275021
vm_record.API.vM_VBDs
50285022
in
5029-
if List.length vbd_to_remove < 1 then
5023+
if vbd_to_remove = [] then
50305024
failwith "Disk not found"
50315025
else
50325026
let vbd = List.nth vbd_to_remove 0 in
@@ -5052,7 +5046,7 @@ let vm_cd_remove printer rpc session_id params =
50525046
)
50535047
vm_record.API.vM_VBDs
50545048
in
5055-
if List.length vbd_to_remove < 1 then
5049+
if vbd_to_remove = [] then
50565050
raise (failwith "Disk not found")
50575051
else
50585052
let vbd = List.nth vbd_to_remove 0 in
@@ -5071,7 +5065,7 @@ let vm_cd_add printer rpc session_id params =
50715065
)
50725066
vdis
50735067
in
5074-
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found!") ;
5068+
if vdis = [] then failwith ("CD " ^ cd_name ^ " not found!") ;
50755069
let vdi = List.nth vdis 0 in
50765070
let op vm =
50775071
create_vbd_and_plug rpc session_id (vm.getref ()) vdi
@@ -5094,9 +5088,14 @@ let vm_cd_eject printer rpc session_id params =
50945088
(fun vbd -> Client.VBD.get_type ~rpc ~session_id ~self:vbd = `CD)
50955089
vbds
50965090
in
5097-
if List.length cdvbds = 0 then failwith "No CDs found" ;
5098-
if List.length cdvbds > 1 then
5099-
failwith "Two or more CDs found. Please use vbd-eject" ;
5091+
( match cdvbds with
5092+
| [] ->
5093+
failwith "No CDs found"
5094+
| [_] ->
5095+
()
5096+
| _ :: _ :: _ ->
5097+
failwith "Two or more CDs found. Please use vbd-eject"
5098+
) ;
51005099
let cd = List.hd cdvbds in
51015100
Client.VBD.eject ~rpc ~session_id ~vbd:cd
51025101
in
@@ -5113,13 +5112,18 @@ let vm_cd_insert printer rpc session_id params =
51135112
)
51145113
vdis
51155114
in
5116-
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found") ;
5117-
if List.length vdis > 1 then
5118-
failwith
5119-
("Multiple CDs named "
5120-
^ cd_name
5121-
^ " found. Please use vbd-insert and specify uuids"
5122-
) ;
5115+
( match vdis with
5116+
| [] ->
5117+
failwith ("CD " ^ cd_name ^ " not found")
5118+
| [_] ->
5119+
()
5120+
| _ :: _ :: _ ->
5121+
failwith
5122+
("Multiple CDs named "
5123+
^ cd_name
5124+
^ " found. Please use vbd-insert and specify uuids"
5125+
)
5126+
) ;
51235127
let op vm =
51245128
let vm_record = vm.record () in
51255129
let vbds = vm_record.API.vM_VBDs in
@@ -5131,15 +5135,16 @@ let vm_cd_insert printer rpc session_id params =
51315135
)
51325136
vbds
51335137
in
5134-
if List.length cdvbds = 0 then
5135-
raise
5136-
(Api_errors.Server_error
5137-
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
5138-
) ;
5139-
if List.length cdvbds > 1 then
5140-
failwith "Two or more empty CD devices found. Please use vbd-insert" ;
5141-
let cd = List.hd cdvbds in
5142-
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
5138+
match cdvbds with
5139+
| [] ->
5140+
raise
5141+
(Api_errors.Server_error
5142+
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
5143+
)
5144+
| [cd] ->
5145+
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
5146+
| _ :: _ :: _ ->
5147+
failwith "Two or more empty CD devices found. Please use vbd-insert"
51435148
in
51445149
ignore (do_vm_op printer rpc session_id op params ["cd-name"])
51455150

@@ -5555,7 +5560,7 @@ let pool_retrieve_wlb_report fd _printer rpc session_id params =
55555560
in
55565561
download_file_with_task fd rpc session_id filename Constants.wlb_report_uri
55575562
(Printf.sprintf "report=%s%s%s" (Http.urlencode report)
5558-
(if List.length other_params = 0 then "" else "&")
5563+
(if other_params = [] then "" else "&")
55595564
(String.concat "&"
55605565
(List.map
55615566
(fun (k, v) ->
@@ -5978,7 +5983,7 @@ let vm_is_bios_customized printer rpc session_id params =
59785983
let bios_strings =
59795984
Client.VM.get_bios_strings ~rpc ~session_id ~self:(vm.getref ())
59805985
in
5981-
if List.length bios_strings = 0 then
5986+
if bios_strings = [] then
59825987
printer
59835988
(Cli_printer.PMsg "The BIOS strings of this VM have not yet been set.")
59845989
else if bios_strings = Constants.generic_bios_strings then
@@ -7259,7 +7264,7 @@ let subject_role_common rpc session_id params =
72597264
let roles =
72607265
Client.Role.get_by_name_label ~rpc ~session_id ~label:role_name
72617266
in
7262-
if List.length roles > 0 then
7267+
if roles <> [] then
72637268
List.hd roles (* names are unique, there's either 0 or 1*)
72647269
else
72657270
Ref.null

0 commit comments

Comments
 (0)