jflex icon indicating copy to clipboard operation
jflex copied to clipboard

jflex has error-prone fall-through

Open regisd opened this issue 7 years ago • 9 comments

jflex/jflex-unicode-maven-plugin/target/generated-sources/jflex/jflex/ArchaicLineBreakScanner.java:702: error: [FallThrough] Execution may fall through from the previous case; add a // fall through comment before this line if it was deliberate

          case 11: break;
          ^

see http://errorprone.info/bugpattern/FallThrough

regisd avatar Nov 01 '17 19:11 regisd

The fall-through violations seem to be in generated code -- we could automatically add the // fall through comment in the generator to indicate that this is on purpose.

lsf37 avatar Nov 02 '17 21:11 lsf37

Unfortunately, that's not enough.

At the end of emitActions, I have added a fall-through comment

  println("            { " + action.content);
  println("            } ");
  println("            // fall through ");

The problem is that action.content can contain a return or a throw statement ; in which case the fall-through comment is not legitimate anymore.

regisd avatar Nov 09 '17 21:11 regisd

Right, that's actually what that specific fall through is for: to guard for the cases where there is no return or throw and the scan just continues. Do we get warnings for fall through comments that are spurious? If not, we could add another line with something like // allowed to fall through if action does not return or throw

lsf37 avatar Nov 09 '17 22:11 lsf37

Curious... why use fall through to a new case instead of adding an explicit break; ?

wrprice avatar Aug 28 '18 15:08 wrprice

// fall through is the exact opposite of break. It means the case falls to the following case statement, and that the developer has not forgotten the break.

regisd avatar Aug 28 '18 15:08 regisd

I understand, but I noticed quite a few cases that fall-through to cases that only break; and do nothing else. I was just about to edit my post because the answer is probably because you don't know whether the action code included a return or break, and without examining it the safest thing is to fall-through to a separate case that breaks. Blindly inserting a break after an action could result in an unreachable statement otherwise.

Sorry for the noise.

wrprice avatar Aug 28 '18 15:08 wrprice

There are still violations https://travis-ci.org/jflex-de/jflex/jobs/431900782

regisd avatar Sep 22 '18 16:09 regisd

Work around: Use javac option -Xep:FallThrough:WARN

regisd avatar Oct 15 '18 13:10 regisd

I also believe that JFlex 1.7.0 has fall-through violations that 1.6.1 didn't have.

regisd avatar Oct 18 '18 18:10 regisd