clarity-lang / reference

The Clarity Reference
146 stars 34 forks source link

`unwrap!` and `unwrap-panic!` should not evaluate both branches at the same time #59

Open hugocaillard opened 1 year ago

hugocaillard commented 1 year ago

In Clarity 1 and 2, both branches of unwrap! and unwrap-err! statements are always evaluated.

Consider the following must-be-some function:

(define-read-only (func (opt (optional int)))
  (unwrap!
    opt
    (begin (print "should not log") (err u500))
  )
)

When being call with (func (some 1)), it will return 1 but the print of the right branch will be evaluated and printed.

It does make sense because functions arguments are always evaluated before calling the function itself (eg: foo(3, bar())). But in this case, I think it should behave differently. Only one of the branch should be evaluated depending on the condition. Like if and asserts!

It will have an impact on code coverage as well