Skip to content

Commit

Permalink
Issue #10924: Update FallThroughCheck to use code points
Browse files Browse the repository at this point in the history
  • Loading branch information
MUzairS15 authored and romani committed Mar 16, 2022
1 parent 50be1f3 commit 7697df3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;

/**
* <p>
Expand Down Expand Up @@ -462,12 +462,6 @@ private boolean checkSynchronized(final DetailAST synchronizedAst, boolean useBr
private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase) {
boolean allThroughComment = false;
final int endLineNo = nextCase.getLineNo();
final int endColNo = nextCase.getColumnNo();

// Remember: The lines number returned from the AST is 1-based, but
// the lines number in this array are 0-based. So you will often
// see a "lineNo-1" etc.
final String[] lines = getLines();

// Handle:
// case 1:
Expand All @@ -478,8 +472,7 @@ private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase)
// default:
// /+ FALLTHRU +/}
//
final String linePart = lines[endLineNo - 1].substring(0, endColNo);
if (matchesComment(reliefPattern, linePart, endLineNo)) {
if (matchesComment(reliefPattern, endLineNo)) {
allThroughComment = true;
}
else {
Expand All @@ -496,8 +489,9 @@ private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase)
// }
final int startLineNo = currentCase.getLineNo();
for (int i = endLineNo - 2; i > startLineNo - 1; i--) {
if (!CommonUtil.isBlank(lines[i])) {
allThroughComment = matchesComment(reliefPattern, lines[i], i + 1);
final int[] line = getLineCodePoints(i);
if (!CodePointUtil.isBlank(line)) {
allThroughComment = matchesComment(reliefPattern, i + 1);
break;
}
}
Expand All @@ -510,13 +504,14 @@ private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase)
* possible match is within a comment.
*
* @param pattern The regular expression pattern to use.
* @param line The line of test to do the match on.
* @param lineNo The line number in the file.
* @return True if a match was found inside a comment.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean matchesComment(Pattern pattern, String line, int lineNo) {
private boolean matchesComment(Pattern pattern, int lineNo) {
final String line = getLine(lineNo - 1);

final Matcher matcher = pattern.matcher(line);
boolean matches = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ public void testOwnPatternTryWithResources() throws Exception {
expected);
}

@Test
public void testWithEmoji() throws Exception {
final String[] expected = {
"22:17: " + getCheckMessage(MSG_FALL_THROUGH),
"25:17: " + getCheckMessage(MSG_FALL_THROUGH),
"49:17: " + getCheckMessage(MSG_FALL_THROUGH),
"52:17: " + getCheckMessage(MSG_FALL_THROUGH),
};
verifyWithInlineConfigParser(
getPath("InputFallThroughWithEmoji.java"), expected);
}

@Test
public void testTokensNotNull() {
final FallThroughCheck check = new FallThroughCheck();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
FallThrough
checkLastCaseGroup = (default)false
reliefPattern = (default)falls?[ -]?thr(u|ough)
*/

package com.puppycrawl.tools.checkstyle.checks.coding.fallthrough;

public class InputFallThroughWithEmoji {

void foo() {

while (true) {
int i = 0;
switch ("👍") {
case "ds😂": // ok
case "": i++; break;
case "👇🏻":
i++;
case "😂sda":
// violation above 'Fall through from previous branch of the switch statement.'
i++;
case "d😂sda": return;
// violation above 'Fall through from previous branch of the switch statement.'

case "5😆": throw new RuntimeException("");
case "🧐6":
do {
System.identityHashCode("something");
return;
} while(true);
case "7🤛🏻":
for (int j1 = 0; j1 < 10; j1++) {
String.valueOf("something");
"7🤛🏻".toString(); return;
}
case "8🥳": try {
i++;
String s ="8🥳"; break;
} catch (RuntimeException e) {
i--;

} finally {
i++;
}
// fall👉🏻through,
case "9": String s = "s🥳d🥳s";
// violation above 'Fall through from previous branch of the switch statement.'
// FALLTHRU (case sensitive)
default: // violation 'Fall through from previous branch of the switch statement.'
"🥳".toString().equals("🥳");

// this is the last label
i++;
}
}
}

void fooFallThru() {
int i = 0;
switch ("") {

case "ds😂": // ok
case "":
i++;
break;
case "👇🏻": i++;

// fallthru 😂 works
case "😂sda":
i++;
// fallthru
case "dsda":
return;
case "5😆":
throw new RuntimeException("");
case "🧐6":
do {
System.identityHashCode("something");

} while(true);
/*falls through*/
case "7🤛🏻": for (int j1 = 0; j1 < 10; j1++) {
String.valueOf("something");
return;
}
case "8🥳": try { break;
} catch (RuntimeException e) {
i--;
break;
} finally {
i++;
}
case "9": String s = "s🥳d🥳s";
//🥳d🥳 fallthru

case "10": String s2 = "s🥳d🥳s";
/*🥳🥳🥳🥳🥳🥳*/ /* fallthru */ default: i++; // ok

}
}
}

0 comments on commit 7697df3

Please sign in to comment.