Skip to content

Commit af4860b

Browse files
authored
Merge pull request #5762 from last-genius/private/asultanov/empty-length-checking
IH-640: Eliminate unnecessary usage of List.length to check for empty lists
2 parents 1db8c1b + 845ffdd commit af4860b

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
@@ -3936,7 +3936,7 @@ let rio_help printer minimal cmd =
39363936
)
39373937
cmd.params
39383938
in
3939-
if List.length cmds > 0 then
3939+
if cmds <> [] then
39403940
List.iter docmd (List.map fst cmds)
39413941
else
39423942
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"
@@ -2933,13 +2933,7 @@ let event_wait_gen rpc session_id classname record_matches =
29332933
(List.map (fun r -> (r.name, fun () -> safe_get_field r)))
29342934
current_tbls
29352935
in
2936-
debug "Got %d records" (List.length all_recs) ;
2937-
(* true if anything matches now *)
2938-
let find_any_match recs =
2939-
let ls = List.map record_matches recs in
2940-
List.length (List.filter (fun x -> x) ls) > 0
2941-
in
2942-
find_any_match all_recs
2936+
List.exists record_matches all_recs
29432937
in
29442938
finally
29452939
(fun () ->
@@ -3310,9 +3304,9 @@ let do_host_op rpc session_id op params ?(multiple = true) ignore_params =
33103304
failwith "No matching hosts found"
33113305
| 1 ->
33123306
[op 1 (List.hd hosts)]
3313-
| _ ->
3307+
| len ->
33143308
if multiple && get_bool_param params "multiple" then
3315-
do_multiple (op (List.length hosts)) hosts
3309+
do_multiple (op len) hosts
33163310
else
33173311
failwith
33183312
( if not multiple then
@@ -3922,11 +3916,13 @@ let vm_install_real printer rpc session_id template name description params =
39223916
failwith
39233917
"SR specified via sr-uuid doesn't have the name specified via \
39243918
sr-name-label"
3925-
| None ->
3926-
if List.length sr_list > 1 then
3919+
| None -> (
3920+
match sr_list with
3921+
| [x] ->
3922+
Some x
3923+
| _ ->
39273924
failwith "Multiple SRs with that name-label found"
3928-
else
3929-
Some (List.hd sr_list)
3925+
)
39303926
)
39313927
else
39323928
sr_ref
@@ -4063,12 +4059,12 @@ let vm_install printer rpc session_id params =
40634059
List.fold_left filter_records_on_fields all_recs
40644060
(("name-label", name) :: filter_params)
40654061
in
4066-
match List.length templates with
4067-
| 0 ->
4062+
match templates with
4063+
| [] ->
40684064
failwith "No templates matched"
4069-
| 1 ->
4070-
(List.hd templates).getref ()
4071-
| _ ->
4065+
| [x] ->
4066+
x.getref ()
4067+
| _ :: _ :: _ ->
40724068
failwith "More than one matching template found"
40734069
in
40744070
if
@@ -4119,7 +4115,7 @@ let console fd _printer rpc session_id params =
41194115
| [] ->
41204116
marshal fd (Command (PrintStderr "No VM found\n")) ;
41214117
raise (ExitWithError 1)
4122-
| _ :: _ ->
4118+
| _ :: _ :: _ ->
41234119
marshal fd
41244120
(Command
41254121
(PrintStderr
@@ -4158,9 +4154,10 @@ let vm_uninstall_common fd _printer rpc session_id params vms =
41584154
(* add extra text if the VDI is being shared *)
41594155
let r = Client.VDI.get_record ~rpc ~session_id ~self:vdi in
41604156
Printf.sprintf "VDI: %s (%s) %s" r.API.vDI_uuid r.API.vDI_name_label
4161-
( if List.length r.API.vDI_VBDs <= 1 then
4157+
( match r.API.vDI_VBDs with
4158+
| [] | [_] ->
41624159
""
4163-
else
4160+
| _ :: _ :: _ ->
41644161
" ** WARNING: disk is shared by other VMs"
41654162
)
41664163
in
@@ -4482,18 +4479,15 @@ let vm_retrieve_wlb_recommendations printer rpc session_id params =
44824479
in
44834480
try
44844481
let vms = select_vms rpc session_id params [] in
4485-
match List.length vms with
4486-
| 0 ->
4482+
match vms with
4483+
| [] ->
44874484
failwith "No matching VMs found"
4488-
| 1 ->
4485+
| [x] ->
44894486
printer
44904487
(Cli_printer.PTable
4491-
[
4492-
("Host(Uuid)", "Stars, RecID, ZeroScoreReason")
4493-
:: table (List.hd vms)
4494-
]
4488+
[("Host(Uuid)", "Stars, RecID, ZeroScoreReason") :: table x]
44954489
)
4496-
| _ ->
4490+
| _ :: _ :: _ ->
44974491
failwith
44984492
"Multiple VMs found. Operation can only be performed on one VM at a \
44994493
time"
@@ -4633,7 +4627,7 @@ let vm_migrate printer rpc session_id params =
46334627
)
46344628
pifs
46354629
in
4636-
if List.length management_pifs = 0 then
4630+
if management_pifs = [] then
46374631
failwith
46384632
(Printf.sprintf "Could not find management PIF on host %s"
46394633
host_record.API.host_uuid
@@ -5031,7 +5025,7 @@ let vm_disk_remove printer rpc session_id params =
50315025
(fun x -> device = Client.VBD.get_userdevice ~rpc ~session_id ~self:x)
50325026
vm_record.API.vM_VBDs
50335027
in
5034-
if List.length vbd_to_remove < 1 then
5028+
if vbd_to_remove = [] then
50355029
failwith "Disk not found"
50365030
else
50375031
let vbd = List.nth vbd_to_remove 0 in
@@ -5057,7 +5051,7 @@ let vm_cd_remove printer rpc session_id params =
50575051
)
50585052
vm_record.API.vM_VBDs
50595053
in
5060-
if List.length vbd_to_remove < 1 then
5054+
if vbd_to_remove = [] then
50615055
raise (failwith "Disk not found")
50625056
else
50635057
let vbd = List.nth vbd_to_remove 0 in
@@ -5076,7 +5070,7 @@ let vm_cd_add printer rpc session_id params =
50765070
)
50775071
vdis
50785072
in
5079-
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found!") ;
5073+
if vdis = [] then failwith ("CD " ^ cd_name ^ " not found!") ;
50805074
let vdi = List.nth vdis 0 in
50815075
let op vm =
50825076
create_vbd_and_plug rpc session_id (vm.getref ()) vdi
@@ -5099,9 +5093,14 @@ let vm_cd_eject printer rpc session_id params =
50995093
(fun vbd -> Client.VBD.get_type ~rpc ~session_id ~self:vbd = `CD)
51005094
vbds
51015095
in
5102-
if List.length cdvbds = 0 then failwith "No CDs found" ;
5103-
if List.length cdvbds > 1 then
5104-
failwith "Two or more CDs found. Please use vbd-eject" ;
5096+
( match cdvbds with
5097+
| [] ->
5098+
failwith "No CDs found"
5099+
| [_] ->
5100+
()
5101+
| _ :: _ :: _ ->
5102+
failwith "Two or more CDs found. Please use vbd-eject"
5103+
) ;
51055104
let cd = List.hd cdvbds in
51065105
Client.VBD.eject ~rpc ~session_id ~vbd:cd
51075106
in
@@ -5118,13 +5117,18 @@ let vm_cd_insert printer rpc session_id params =
51185117
)
51195118
vdis
51205119
in
5121-
if List.length vdis = 0 then failwith ("CD " ^ cd_name ^ " not found") ;
5122-
if List.length vdis > 1 then
5123-
failwith
5124-
("Multiple CDs named "
5125-
^ cd_name
5126-
^ " found. Please use vbd-insert and specify uuids"
5127-
) ;
5120+
( match vdis with
5121+
| [] ->
5122+
failwith ("CD " ^ cd_name ^ " not found")
5123+
| [_] ->
5124+
()
5125+
| _ :: _ :: _ ->
5126+
failwith
5127+
("Multiple CDs named "
5128+
^ cd_name
5129+
^ " found. Please use vbd-insert and specify uuids"
5130+
)
5131+
) ;
51285132
let op vm =
51295133
let vm_record = vm.record () in
51305134
let vbds = vm_record.API.vM_VBDs in
@@ -5136,15 +5140,16 @@ let vm_cd_insert printer rpc session_id params =
51365140
)
51375141
vbds
51385142
in
5139-
if List.length cdvbds = 0 then
5140-
raise
5141-
(Api_errors.Server_error
5142-
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
5143-
) ;
5144-
if List.length cdvbds > 1 then
5145-
failwith "Two or more empty CD devices found. Please use vbd-insert" ;
5146-
let cd = List.hd cdvbds in
5147-
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
5143+
match cdvbds with
5144+
| [] ->
5145+
raise
5146+
(Api_errors.Server_error
5147+
(Api_errors.vm_no_empty_cd_vbd, [Ref.string_of (vm.getref ())])
5148+
)
5149+
| [cd] ->
5150+
Client.VBD.insert ~rpc ~session_id ~vbd:cd ~vdi:(List.hd vdis)
5151+
| _ :: _ :: _ ->
5152+
failwith "Two or more empty CD devices found. Please use vbd-insert"
51485153
in
51495154
ignore (do_vm_op printer rpc session_id op params ["cd-name"])
51505155

@@ -5560,7 +5565,7 @@ let pool_retrieve_wlb_report fd _printer rpc session_id params =
55605565
in
55615566
download_file_with_task fd rpc session_id filename Constants.wlb_report_uri
55625567
(Printf.sprintf "report=%s%s%s" (Http.urlencode report)
5563-
(if List.length other_params = 0 then "" else "&")
5568+
(if other_params = [] then "" else "&")
55645569
(String.concat "&"
55655570
(List.map
55665571
(fun (k, v) ->
@@ -5983,7 +5988,7 @@ let vm_is_bios_customized printer rpc session_id params =
59835988
let bios_strings =
59845989
Client.VM.get_bios_strings ~rpc ~session_id ~self:(vm.getref ())
59855990
in
5986-
if List.length bios_strings = 0 then
5991+
if bios_strings = [] then
59875992
printer
59885993
(Cli_printer.PMsg "The BIOS strings of this VM have not yet been set.")
59895994
else if bios_strings = Constants.generic_bios_strings then
@@ -7264,7 +7269,7 @@ let subject_role_common rpc session_id params =
72647269
let roles =
72657270
Client.Role.get_by_name_label ~rpc ~session_id ~label:role_name
72667271
in
7267-
if List.length roles > 0 then
7272+
if roles <> [] then
72687273
List.hd roles (* names are unique, there's either 0 or 1*)
72697274
else
72707275
Ref.null

0 commit comments

Comments
 (0)