ocaml.lang.correctness.useless-compare.useless-compare

profile photo of semgrepsemgrep
Author
unknown
Download Count*

This comparison is useless because the expressions being compared are identical. This is expected to always return the same result, 0, unless your code is really strange.

Run Locally

Run in CI

Defintion

rules:
  - id: useless-compare
    patterns:
      - pattern-either:
          - pattern: compare $X $X
          - pattern: $MODULE.compare $X $X
    message: This comparison is useless because the expressions being compared are
      identical. This is expected to always return the same result, 0, unless
      your code is really strange.
    languages:
      - ocaml
    severity: ERROR
    metadata:
      category: correctness
      technology:
        - ocaml
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

useless-compare.ml

let test a b =
  (* ruleid:useless-compare *)
  let c = compare (a+b) (a+b) in
  if c <> 0 then c
  else
    compare a b

let test a b =
  (* ruleid:useless-compare *)
  let c = Int.compare (a+b) (a+b) in
  if c <> 0 then c
  else
    compare a b