| Summary: | [checker] Suspicious semicolon (i.e. ";" after "if") | ||||||
|---|---|---|---|---|---|---|---|
| Product: | [Tools] CDT | Reporter: | Kirstin Weber <kirstin.weber> | ||||
| Component: | cdt-codan | Assignee: | Elena Laskavaia <elaskavaia.cdt> | ||||
| Status: | RESOLVED FIXED | QA Contact: | Elena Laskavaia <elaskavaia.cdt> | ||||
| Severity: | enhancement | ||||||
| Priority: | P3 | CC: | cdtdoug, fchen0000, jens.elmenthaler, malaperle, recoskie | ||||
| Version: | 8.0 | ||||||
| Target Milestone: | 8.0 | ||||||
| Hardware: | All | ||||||
| OS: | All | ||||||
| Whiteboard: | |||||||
| Attachments: |
|
||||||
Should it only check for
if(expr);
{
foo();
}
or also
if(expr);
foo();
I think it should check second case too. Consider
if(expr);
foo(); // indented line here
If semicolon is intended (i.e. "expr" produces side effects) one could be more explicit:
if(expr){}
I agree with Andrew. I think in most cases the semicolon after "if(expr);" is not intended. My first approach was comparing the line number of the condition and the line number of the null statement in AST. But the condition doesn't include the parentheses which means this will not report an error but should: if(cond ); I'm trying to figure out a way to get the line number of the parenthesis at the end of the condition in a cheap manner. And getTrailingSyntax doesn't include white space characters, humm. It occurred to me that there is a legitimate case with semicolon: if(expr); else foo(); (In reply to comment #5) > It occurred to me that there is a legitimate case with semicolon: > if(expr); > else > foo(); It's legit, but not entirely sure it's desirable either. Isn't that more succinctly/elegantly expressed as: if(!expr) foo(); It's a matter of taste I suppose. Not sure why you are trying do deal with line numbers, just check what is the statement on the if branch - if it empty statement - it is "if (a);" case (In reply to comment #4) > My first approach was comparing the line number of the condition and the line > number of the null statement in AST. But the condition doesn't include the > parentheses which means this will not report an error but should: > > if(cond > ); > > I'm trying to figure out a way to get the line number of the parenthesis at the > end of the condition in a cheap manner. And getTrailingSyntax doesn't include > white space characters, humm. (In reply to comment #7) > Not sure why you are trying do deal with line numbers, just check what is the > statement on the if branch - if it empty statement - it is "if (a);" case It depends. Should it consider this case suspicious or not: if(a) ; yes. Why do you write a code like that? Created attachment 180073 [details]
Suspicious semicolon checker
assigned Thanks! patch applied. I added a bit of code to protect from case where empty macro is used as statement *** cdt cvs genie on behalf of elaskavaia *** Bug 322119 - [checker] Suspicious semicolon (i.e. ";" after "if") [+] SuspiciousSemicolonCheckerTest.java http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/SuspiciousSemicolonCheckerTest.java?root=Tools_Project&revision=1.1&view=markup [*] AutomatedIntegrationSuite.java 1.13 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java?root=Tools_Project&r1=1.12&r2=1.13 [+] SuspiciousSemicolonChecker.java http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/SuspiciousSemicolonChecker.java?root=Tools_Project&revision=1.1&view=markup [*] plugin.xml 1.30 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/codan/org.eclipse.cdt.codan.checkers/plugin.xml?root=Tools_Project&r1=1.29&r2=1.30 [*] bundle.properties 1.6 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties?root=Tools_Project&r1=1.5&r2=1.6 Thanks for the checker! (In reply to Chris Recoskie from comment #6) > (In reply to comment #5) > > It occurred to me that there is a legitimate case with semicolon: > > if(expr); > > else > > foo(); > > It's legit, but not entirely sure it's desirable either. Isn't that more > succinctly/elegantly expressed as: > > if(!expr) > foo(); > > It's a matter of taste I suppose. I had one occasion the logic is much more clear when writing like: if(expr); else if(expr2) do_something; else if(expr3) do_sth_else; else do_sth; |
Build Identifier: I had the problem that I wrote a ";" after an "if" condition and could not find the problem for a long time. Example: if(myVariable == 1); // <= here I accidently added a ";" { // do something } It would be helpful if a checker would check for this case and show a hint that there might be a problem. Reproducible: Always Steps to Reproduce: See details