-1
    if(fn1())   //Line 1
     {
     a++;
     }

Here the return type of function fn1 is uint8_t. The function returns only values 0 or 1.

PC Lint throws error for line 1 says "Mismatched essential type categories for binary operator" [RULE 10.4 required]

Rule 10.4 says "Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category"

  • 2
    Probably `if (fn1() == 1)` will do the job. – Jabberwocky Nov 27 '19 at 08:13
  • I tried that already. But it says Mismatched essential type categories for binary operator [10.4 required] – user3340164 Nov 27 '19 at 08:16
  • 4
    If you've tried that, you should mention it in the question. Also show a [mcve]. It's better to _show_ your code rather than to _describe_ it. – Jabberwocky Nov 27 '19 at 08:20
  • Does changing the return type to `bool` (or `_Bool`) helps? – Bob__ Nov 27 '19 at 08:39
  • 2
    If you don't understand these warnings, I would strongly recommend to stop stumbling around in the dark and actually read the MISRA-C document. There's two appendix C and D which serve to educate programmers about the various dangers of the language. Also check [Implicit type promotion rules](https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules) – Lundin Nov 27 '19 at 08:45
  • MISRA is overly strict in most cases. Unless you have a hard requirement that your code should pass MISRA then I really wouldn't care about it. – Some programmer dude Nov 27 '19 at 09:21
  • If changing the function return type is not feasible, you could always check ```if ( fn() != 0 )``` – Andrew Nov 29 '19 at 14:18

1 Answers1

6

MISRA-C doesn't allow implicit checks against zero - all inputs to conditional statements must be "essentially boolean", which is what you get if you explicitly use logical operators like ==.

In addition, MISRA-C has various rules blocking you from mixing "essenitally signed" and "essentially unsigned" in the same expression, and thereby relying on implicit type promotions. So you can't write if (fn1() == 1) for that reason, since 1 is a signed type and there's also a rule requiring all integer constants to have u suffix.

So one possible fix is if (fn1() == 1u). However, since the function only returns 0 or 1, you can also cast its result to boolean type and that would be fine too: if ((bool)fn()). The best solution is however to rewrite the function to return bool, after which you can use if(fn1()).

Lundin
  • 195,001
  • 40
  • 254
  • 396