Bug 345470 - [fn] assignment in condition does not notice assignments in parenthesis
Summary: [fn] assignment in condition does not notice assignments in parenthesis
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-11 13:04 EDT by Andrew Gvozdev CLA
Modified: 2013-08-24 17:32 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Gvozdev CLA 2011-05-11 13:04:01 EDT
Assignments in conditions are slipping through in code like that:

if ( (x==0) && (y=0) ) // no marker on y
Comment 1 Elena Laskavaia CLA 2011-05-21 11:46:38 EDT
This is intended. More in the case like this 
if ((a=b)) {...}
The way to say to tool - I know what I am doing
Comment 2 Andrew Gvozdev CLA 2011-05-21 11:59:37 EDT
(In reply to comment #1)
> This is intended. More in the case like this
> if ((a=b)) {...}
> The way to say to tool - I know what I am doing
I vaguely recall this discussion but now I think it is pretty obscure to a user. I think it would be better to invent a system of tags which could be specified in comments to suppress codan warnings, similar as done for switch/break checker or java @SuppressWarnings annotation.
As far as this particular case, I think it is pretty common to have company policy not to allow assignments in conditions, it would be desirable to have the checker working under that policy too.
Comment 3 Tomasz Wesolowski CLA 2011-07-22 07:28:12 EDT
I agree. Even if if ( (a=b) ) is clear, the case in description is a good example of typical accidental assignment.

Should I modify the checker to check comments before the condition statement for some key text, just like with case fallthrough?
Comment 4 Tomasz Wesolowski CLA 2011-08-01 05:58:03 EDT
BTW this issue was already mentioned in bug 307414. 

The checker assumes that an assignment in () is always intended. This assumption only works for simple conditions, I think.
Some people do if ((a == 10) && (b == 20)) and put the extra parentheses because they are unsure about operator priorities or they just think it looks better. It's a matter of code style and the checker shouldn't ignore = instead of == here.

I'd suggest to drop using parentheses here and just use a comment tag like "no break" in case fallthrough- much more clear and won't cause problems.
Comment 5 Elena Laskavaia CLA 2011-08-20 13:29:43 EDT
For static analysis it is better to have false negatives than false positives,
assignment in conditions are quite typical is C

if ((k=read())!=-1) {
...
}

What we can do if to add a parameter to a checker, which is not on by default 
[ ] Report all assignment in conditions, even in brackets

(Or one that is on by default
[*] Do not report assignments in brackets
)

Adding suppressing comments is a good anyway, I am working (very slowly) 
on adding a framework to use it in all checkers.
Comment 6 Nathan Ridge CLA 2013-08-24 17:32:37 EDT
(In reply to comment #5)
> Adding suppressing comments is a good anyway, I am working (very slowly) 
> on adding a framework to use it in all checkers.

How is that coming along?