Skip to content

Commit 750f1ee

Browse files
authored
Merge pull request #1614 from goblint/semgrep-fold
Replace some `fold`s with `exists` or `for_all`
2 parents 62cb655 + 513662c commit 750f1ee

File tree

6 files changed

+41
-15
lines changed

6 files changed

+41
-15
lines changed

.semgrep/fold.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
rules:
2+
- id: fold-exists
3+
patterns:
4+
- pattern-either:
5+
- pattern: $D.fold ... false
6+
- pattern: $D.fold_left ... false
7+
- pattern: $D.fold_right ... false
8+
- pattern: fold ... false
9+
- pattern: fold_left ... false
10+
- pattern: fold_right ... false
11+
message: consider replacing fold with exists
12+
languages: [ocaml]
13+
severity: WARNING
14+
15+
- id: fold-for_all
16+
patterns:
17+
- pattern-either:
18+
- pattern: $D.fold ... true
19+
- pattern: $D.fold_left ... true
20+
- pattern: $D.fold_right ... true
21+
- pattern: fold ... true
22+
- pattern: fold_left ... true
23+
- pattern: fold_right ... true
24+
message: consider replacing fold with for_all
25+
languages: [ocaml]
26+
severity: WARNING

src/analyses/basePriv.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,11 @@ struct
10401040
let s = MustLockset.remove m (current_lockset ask) in
10411041
let t = current_thread ask in
10421042
let side_cpa = CPA.filter (fun x _ ->
1043-
GWeak.fold (fun s' tm acc ->
1043+
GWeak.exists (fun s' tm ->
10441044
(* TODO: swap 2^M and T partitioning for lookup by t here first? *)
10451045
let v = ThreadMap.find t tm in
1046-
(MustLockset.mem m s' && not (VD.is_bot v)) || acc
1047-
) (G.weak (getg (V.global x))) false
1046+
(MustLockset.mem m s' && not (VD.is_bot v))
1047+
) (G.weak (getg (V.global x)))
10481048
) st.cpa
10491049
in
10501050
sideg (V.mutex m) (G.create_sync (GSync.singleton s side_cpa));
@@ -1098,9 +1098,9 @@ struct
10981098
let unlock ask getg sideg (st: BaseComponents (D).t) m =
10991099
let s = MustLockset.remove m (current_lockset ask) in
11001100
let side_cpa = CPA.filter (fun x _ ->
1101-
GWeak.fold (fun s' v acc ->
1102-
(MustLockset.mem m s' && not (VD.is_bot v)) || acc
1103-
) (G.weak (getg (V.global x))) false
1101+
GWeak.exists (fun s' v ->
1102+
(MustLockset.mem m s' && not (VD.is_bot v))
1103+
) (G.weak (getg (V.global x)))
11041104
) st.cpa
11051105
in
11061106
sideg (V.mutex m) (G.create_sync (GSync.singleton s side_cpa));

src/domain/partitionDomain.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ struct
3131
let meet _ _ = failwith "PartitonDomain.Set.meet: unsound"
3232

3333
let collapse (s1:t) (s2:t): bool =
34-
let f vf2 res =
35-
res || exists (fun vf1 -> S.collapse vf1 vf2) s1
34+
let f vf2 =
35+
exists (fun vf1 -> S.collapse vf1 vf2) s1
3636
in
37-
fold f s2 false
37+
exists f s2
3838

3939
let add e s = join s (singleton e)
4040

src/incremental/compareCFG.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let reexamine f1 f2 (same : biDirectionNodeMap) (diffNodes1 : unit NH.t) (module
131131
false
132132
end in
133133
let cond n2 = Node.equal n2 (FunctionEntry f2) || check_all_nodes_in_same (List.map snd (CfgNew.prev n2)) n2 in
134-
let forall = NH.fold (fun n2 n1 acc -> acc && cond n2) same.node2to1 true in
134+
let forall = NH.fold (fun n2 n1 acc -> acc && cond n2) same.node2to1 true in (* nosemgrep: fold-for_all *) (* cond does side effects *)
135135
if not forall then repeat () in
136136
repeat ();
137137
NH.to_seq same.node1to2, NH.to_seq_keys diffNodes1

src/solver/td3.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ module Base =
289289
destabilize_vs y || b || was_stable && List.mem_cmp S.Var.compare y vs
290290
else
291291
true
292-
) w false
292+
) w false (* nosemgrep: fold-exists *) (* does side effects *)
293293
and solve ?reuse_eq x phase =
294294
if tracing then trace "sol2" "solve %a, phase: %s, called: %b, stable: %b, wpoint: %b" S.Var.pretty_trace x (show_phase phase) (HM.mem called x) (HM.mem stable x) (HM.mem wpoint x);
295295
init x;

src/witness/witness.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,14 @@ struct
342342
| UnreachCall _ ->
343343
(* error function name is globally known through Svcomp.task *)
344344
let is_unreach_call =
345-
LHT.fold (fun (n, c) v acc ->
345+
LHT.for_all (fun (n, c) v ->
346346
match n with
347347
(* FunctionEntry isn't used for extern __VERIFIER_error... *)
348348
| FunctionEntry f when Svcomp.is_error_function f.svar ->
349349
let is_dead = Spec.D.is_bot v in
350-
acc && is_dead
351-
| _ -> acc
352-
) lh true
350+
is_dead
351+
| _ -> true
352+
) lh
353353
in
354354

355355
if is_unreach_call then (

0 commit comments

Comments
 (0)