Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enforcing MISRA compliant Boolean values #7110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anshgoyalevil
Copy link

This PR adds a literal to boolean style check.

bool x = 1;  // Warning: Boolean variable assigned a numeric literal '1'. Consider using 'true' instead.
bool y = 0;  // Warning: Boolean variable assigned a numeric literal '0'. Consider using 'false' instead.

ref: commaai/panda#1876, commaai/panda#2105

Signed-off-by: Ansh Goyal <anshgoyal1704@gmail.com>
@okias
Copy link

okias commented Dec 16, 2024

damn, I'm too slow. Anyway, shouldn't this go into addons/misra.py ? I would guess Rule 23.6

@anshgoyalevil
Copy link
Author

Thanks for the suggestion, @okias. I am fairly new to this MISRA thing. I just read the discussion in the thread commaai/panda#1876 and this way looked like what adding the check into ustream meant.

Can you please help identify if that's the right way to do that? Adding a check into upstream without explicitly defining an addon code.

@okias
Copy link

okias commented Dec 17, 2024

In general, a C++ solution like the one you wrote looks better, but I understood it as part of the MISRA plugin—something like this: okias@6b3d84a. The code doesn't detect the initial bool x = 1;, only bool x; x = 1;. This was also my first attempt with cppcheck..

@anshgoyalevil
Copy link
Author

Thanks. Now I get the point. I would implement it the MISRA plugin way if this one is rejected by the maintainers.
Can you please review it @danmar

@danmar
Copy link
Owner

danmar commented Dec 22, 2024

Hi, sorry for the late review. I spontanously feel this might be too "stylistic" in nature. It's not to find "buggy" code, is it? What bugs could it lead to? Normally I like if "style" checkers have a motivation to prevent some possible bugs. For instance; unread variables can for instance indicate an overlook.

I wouldn't normally write bool x=1; myself explicitly.

In my humble opinion it does not violate MISRA 23.6 but it does violate 10.3 and Cppcheck Premium writes a warning about it.

@danmar
Copy link
Owner

danmar commented Dec 22, 2024

The implementation: I don't have major complaints. It's pretty good. But some test cases is needed and also I think a ticket should be added in trac and the release notes would need to mention the new checker..

@anshgoyalevil
Copy link
Author

Thanks for the inputs. This would not help prevent bugs to the code but would help increase readability at some places. I would add the required test cases.

Can you please elaborate that trac thing. How can I file a ticket out there?

@danmar
Copy link
Owner

danmar commented Jan 1, 2025

This would not help prevent bugs to the code but would help increase readability at some places. I would add the required test cases.

well.. I don't know why anybody would WANT to do this.. but still I don't have a very good feeling about this. This is not the high prio findings users want to see. Could you try to write a checker that looks for "mistakes" somehow? I believe trac has many suggestions as inspiration.

https://trac.cppcheck.net

to get an account on trac you'll need to send a htpasswd hash to me. You can generate a htpasswd hash online, google "htpasswd hash generator"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants