tact-lang / tact

Tact compiler main repository
https://tact-lang.org
MIT License
393 stars 109 forks source link

Need adding the lint check for Boolean value assignment. #6

Closed howardpen9 closed 3 weeks ago

howardpen9 commented 1 year ago

For example: https://t.me/tactlang/2185

I made a mistake in my code that took me two days to figure out. I was using the == operator instead of = to assign a value to a variable, but the code still compiled without any errors. The == operator is used for equality comparisons, not assignments. The correct operator for assignment is =.

Here's an example of the mistake I made:

self.status_A == 3; // 🔴 
self.status_A = 3; // ✅

The IDE should not accept the value assignment in == getting successfully compiled!

ex3ndr commented 1 year ago

I have evaluated options, but it is tricky. In practice we are better to issue a warning of not used return values of an expression, but too many corner cases appears immediately that would make language too strict. I will keep this issue open to figure out what we can do with it later.

jubnzv commented 3 weeks ago

Covered in Misti: https://github.com/nowarp/misti/pull/190.

@anton-trunov could we close it or do we need any changes in the compiler as well?

anton-trunov commented 3 weeks ago

Can you please add here Misti's output for the example from the original post?

jubnzv commented 3 weeks ago
contract C {
    status_A: Int = 42;
    fun test() {
        self.status_A == 3; // Bad
        self.status_A = 3; // OK
    }
}
[MEDIUM] UnusedExpressionResult: Result of evaluation of self.status_A == 3 is unused
sample.tact:4:9:
  3 |     fun test() {
> 4 |         self.status_A == 3; // Bad
              ^
  5 |         self.status_A = 3; // OK
Help: Remove the expression or assign its result
See: https://nowarp.io/tools/misti/docs/detectors/UnusedExpressionResult
error Command failed with exit code 1.

The detector will be available in the next release, so here is the actual link to its doc: https://nowarp.io/tools/misti/docs/next/detectors/UnusedExpressionResult