Skip to content

Commit d969a6e

Browse files
authored
Merge pull request #97 from upenn-cis1xx/esinx/neq-physical
Rule to prefer structural inequality `<>` over referential equality `==`
2 parents 88fea37 + e8dc1c8 commit d969a6e

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

lib/style/checkers.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let expr_checks = [
44
; Equality.EqOption.name
55
; Equality.EqBool.name
66
; Equality.EqPhysical.name
7+
; Equality.NeqPhysical.name
78
; Match.MatchBool.name
89
; Match.MatchInt.name
910
; Match.MatchListVerbose.name

lib/style/equality.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ module EqPhysical : EXPRCHECK = struct
6767
| _ -> ()
6868
end
6969
let name = "EqPhysical", check
70+
end
71+
72+
(** ------------------ Checks rules: (_ != _) ----------------------- *)
73+
module NeqPhysical : EXPRCHECK = struct
74+
type ctxt = Parsetree.expression_desc Pctxt.pctxt
75+
let fix = "using `<>` to evaluate structural inequality"
76+
let violation = "using `!=` when structural equality is intended"
77+
let check st (E {location; source; pattern}: ctxt) =
78+
begin match pattern with
79+
| Pexp_apply (application, [(_,_); (_,_)]) ->
80+
if application =~ "!=" then
81+
st := Hint.mk_hint location source fix violation :: !st
82+
| _ -> ()
83+
end
84+
let name = "NeqPhysical", check
7085
end

test/examples/equality.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ let b4 = false = bfalse
2424

2525
(* Physical equality (should raise issues) *)
2626
let g = 1 == 1
27-
let h = "ocaml" == "ocaml"
27+
let h = "ocaml" == "ocaml"
28+
let i = 1 != 1
29+
let j = "ocaml" != "ocaml"

test/test.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ let%expect_test _ =
8787
let to_lint = to_ast file in
8888
lint_and_hint to_lint;
8989
[%expect{|
90+
(* ------------------------------------------------------------------------ *)
91+
File ./examples/equality.ml, line 29, columns: 8-26
92+
Warning:
93+
using `!=` when structural equality is intended
94+
You wrote:
95+
"ocaml" != "ocaml"
96+
Consider:
97+
using `<>` to evaluate structural inequality
98+
99+
(* ------------------------------------------------------------------------ *)
100+
File ./examples/equality.ml, line 28, columns: 8-14
101+
Warning:
102+
using `!=` when structural equality is intended
103+
You wrote:
104+
1 != 1
105+
Consider:
106+
using `<>` to evaluate structural inequality
107+
90108
(* ------------------------------------------------------------------------ *)
91109
File ./examples/equality.ml, line 27, columns: 8-26
92110
Warning:

0 commit comments

Comments
 (0)