Bug 245257 - [compiler] Allow to suppress fall-through warning
Summary: [compiler] Allow to suppress fall-through warning
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.5 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 127730 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-08-26 10:29 EDT by Dani Megert CLA
Modified: 2008-09-16 07:50 EDT (History)
5 users (show)

See Also:


Attachments
Patch suggestion (8.42 KB, patch)
2008-09-05 12:33 EDT, Philipe Mulet CLA
no flags Details | Diff
Proposed patch (108.22 KB, patch)
2008-09-08 11:03 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (108.22 KB, patch)
2008-09-09 04:23 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (110.64 KB, patch)
2008-09-09 04:26 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (116.14 KB, patch)
2008-09-09 05:16 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2008-08-26 10:29:54 EDT
R3.4.

The fall through warning for switch statements is very good and we have it enabled. There is however some good/valid code were this warning is wrong and we would like to have a flag to ignore that.

I suggest to add a $FALL-THROUGH$ (like the NON-NLS) tag that tells the compiler that fall through is intended by the programmer.

Note that our source has to be 1.4 compliant and hence we cannot use @suppressWarning.
Comment 1 Philipe Mulet CLA 2008-08-26 10:54:43 EDT
Originally, we had thought of allowing any comment (i.e. signal undocumented fallthrough scenario).
Comment 2 Philipe Mulet CLA 2008-08-26 10:55:40 EDT
see bug 127730
Comment 3 Dani Megert CLA 2008-08-26 11:02:28 EDT
>Originally, we had thought of allowing any comment (i.e. signal undocumented
>fallthrough scenario).
You mean if there's a comment you treat it like there would be $FALL-THROUGH$?
Comment 4 Dani Megert CLA 2008-08-26 11:11:38 EDT
Suggest to mark as duplicate of bug 127730 and move bug 127730 back to inbox.
Comment 5 Markus Keller CLA 2008-08-26 12:52:12 EDT
The question is then what comments are counted as "valid fallthrough". In the "undocumented empty block" case, this is easy, but here, we can also have cases where there are other (possibly documented) statements before the fall through. 

As a heuristic, I would consider a fallthrough case as documented if the last token before the next 'case:' or 'default:' is a comment (line or block comment) and the comment is on its own line.

    switch(i) {
      case 0 :
        //still a fall-through problem
        System.out.println("very "); //still a fall-through problem
      case 1 :
        System.out.println("small ");
        /*
         * suppresses the fall-through problem
         */
      case 2 :
        System.out.println("number");
    }
 
See also 67836 comment 2.
Comment 6 Dani Megert CLA 2008-08-27 03:13:41 EDT
Using a heuristic is bad. What do you do in this scenario:

    switch(i) {
      case 0 :
        System.out.println("very ");
        // need to find out whether adding a break
      case 1 :
        fNumber= 1+1;
        //       ^^^ +1 is needed because... 
      case 2 :
        System.out.println("number");
    }
?

Just imagine the mess if we would have used heuristic comments to detect non-NLS-ed string.
Comment 7 Philipe Mulet CLA 2008-08-27 06:18:40 EDT
re: comment 4
I think this one bug as more useful information than bug 127730, so I'd rather keep this one instead.

re: specific comment vs. any comment
I was more keen on having the notion of documenting in general, like for empty method/class bodies... adding specific tags is more intrusive. The NON-NLS tag is pure legacy, and not a trend.

But I do not have a strong opinion at this point.
Comment 8 Philipe Mulet CLA 2008-08-27 06:19:36 EDT
*** Bug 127730 has been marked as a duplicate of this bug. ***
Comment 9 Dani Megert CLA 2008-08-27 06:29:23 EDT
>I think this one bug as more useful information than bug 127730, so I'd rather
>keep this one instead.
Agreed.

>I was more keen on having the notion of documenting in general,
Of course a general story (that does not require 1.5!) would be cool. However we always postponed that solution so far. My goal is to get rid of all problems from the 'Potential Programming Problems' category and so far the only one for which I need a suppress mechanism is this fall-through issue. Hence I'd like to see a specific solution now rather than wait forever for the generic solution ;-)
Comment 10 Philipe Mulet CLA 2008-09-05 12:33:51 EDT
Created attachment 111838 [details]
Patch suggestion

Simple solution for testing presence of leading comment (any will do) between last statement and next case.

It likely needs to be made more selective in the shape of the comment or its location.
Comment 11 Philipe Mulet CLA 2008-09-05 12:40:44 EDT
previous patch will consider ANY leading comment as good enough, e.g.
e.g. case 3 is no longer blamed

public class X {
	void foo1(int i) {
		switch(i) {
			case 0 :
			case 1 :
				System.out.println();
				// fallthrough doc
			case 2 : // should not be blamed
				System.out.println(); // not real fallthrough doc
			case 3 : // should be blamed
				System.out.println(); 
				/* fallthrough */
			case 4 : // should not be blamed
		}
	}
}
Comment 12 Philipe Mulet CLA 2008-09-05 12:55:21 EDT
I am enclined to think it should recognize a specific comment, or at least something in it which is non ambiguous.
Anything is just not good enough.

Would any comment starting with 'fall-through' do it ?
Comment 13 Kent Johnson CLA 2008-09-05 13:01:12 EDT
I would prefer a single comment on its own line as in :
                        case 1 :
                                System.out.println();
                                // something intelligent
                        case 2 :
                                System.out.println();
                                break;

I don't think we should start forcing the comment to start with a specific phrase unless its like the NLS comment.
Comment 14 Markus Keller CLA 2008-09-05 13:41:27 EDT
> Would any comment starting with 'fall-through' do it ?

No. "Comments starting with xyz" is a no-go. Dani's comment 6 makes sense to me, so I would prefer jut $FALL-THROUGH$, as last non-whitespace token in the last comment of a case section.

The discussions about the right heuristic for the "any comment" solution show that this will be confusing and difficult to explain, no matter what actual heuristic we chose. And it might also break down when the code formatter or a refactoring decide to add or remove a line break somewhere.
Comment 15 Dani Megert CLA 2008-09-06 03:14:29 EDT
Using a predefined tag also has the advantage that we can easily convert all such instances to something more appropriate later (e.g. some Java annotation based solution).
Comment 16 Markus Keller CLA 2008-09-06 15:33:36 EDT
> (e.g. some Java annotation based solution)

Maybe in Java 8, or whenever annotations get supported every statement ;-)

Comment 17 Philipe Mulet CLA 2008-09-08 07:13:04 EDT
If we want to stick with NON-NLS tag, we would need to detect comment starting strictly with:
//$FALL-THROUGH$

At least, it would be the same rule (least surprising). As a future request, we could improve special tag recognition to allow them in non-prefix position, or in other kind of comments (right now only in line comments).

Minor: I find $FALL-THROUGH$ to stand out a bit too much in the code actually, what about the following instead ?
//$fall-through$

Comment 18 Dani Megert CLA 2008-09-08 07:31:56 EDT
>Minor: I find $FALL-THROUGH$ to stand out a bit too much in the code actually,
>what about the following instead ?
>//$fall-through$
I suggest to implement it case-insensitive, which allows both patterns.
Comment 19 Markus Keller CLA 2008-09-08 08:16:40 EDT
I would do it the same as for $NON-NLS-1$, i.e. only allow all caps
//$FALL-THROUGH$.

Even if the compiler allowed lowercase, the quick fix should still generate all caps (for consistency).
Comment 20 Philipe Mulet CLA 2008-09-08 11:03:17 EDT
Created attachment 111962 [details]
Proposed patch

Only support capital tag, optionally allow whitespace(s) before the tag.
Comment 21 Philipe Mulet CLA 2008-09-08 11:12:16 EDT
Proposed patch also tolerates a block comment for the tagging.
Comment 22 Markus Keller CLA 2008-09-08 13:52:44 EDT
> Proposed patch also tolerates a block comment for the tagging.

For me, it only tolerates block comments and fails here:
        switch (1) {
            case 1:
                System.out.println();
                //$FALL-THROUGH$
            case 2:
                System.out.println();
        }
Comment 23 Philipe Mulet CLA 2008-09-09 03:32:14 EDT
Yes, I realized this. FYI the batch compiler behaves ok for line comment, but some other parser used in IDE reconcile is flushing line comments at this point, leaving the compiler clueless.

Will investigate.
Comment 24 Philipe Mulet CLA 2008-09-09 04:23:46 EDT
Created attachment 112048 [details]
Better patch

Issue came from negated commentStart in downstream parser implementations (see bug 246682)
Comment 25 Philipe Mulet CLA 2008-09-09 04:26:52 EDT
Created attachment 112049 [details]
Better patch

Better patch for real
Comment 26 Philipe Mulet CLA 2008-09-09 05:16:51 EDT
Created attachment 112057 [details]
Better patch

also tweaked some regression tests after change in problem message
Comment 27 Philipe Mulet CLA 2008-09-09 06:01:17 EDT
Released for 3.5M2
Fixed
Comment 28 Philipe Mulet CLA 2008-09-09 08:48:53 EDT
Improved compiler problem message again:

Switch case may be entered by falling through previous case. If intended, insert a //$FALL-THROUGH$ comment just before
Comment 29 Philipe Mulet CLA 2008-09-09 09:52:39 EDT
And again:

Switch case may be entered by falling through previous case. If intended, add a new comment //$FALL-THROUGH$ on the above line
Comment 30 Philipe Mulet CLA 2008-09-09 12:44:24 EDT
And again:

Switch case may be entered by falling through previous case. If intended, add a
new comment //$FALL-THROUGH$ on the line above
Comment 31 Jerome Lanneluc CLA 2008-09-16 07:50:07 EDT
Verified for 3.5M2 using I20080915-1800