Skip to content

Commit dbd6d09

Browse files
committed
Eliminate unnecessary nested statement(s) within else clauses
Signed-off-by: Sven Strickroth <[email protected]>
1 parent cd61b17 commit dbd6d09

10 files changed

+53
-67
lines changed

src/main/java/org/owasp/html/AttributePolicy.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ static final class AttributePolicyJoiner
9393
Optional<List<AttributePolicy>> split(AttributePolicy x) {
9494
if (x instanceof JoinedAttributePolicy) {
9595
return Optional.of(((JoinedAttributePolicy) x).policies);
96-
} else {
97-
return Optional.empty();
9896
}
97+
return Optional.empty();
9998
}
10099

101100
@Override

src/main/java/org/owasp/html/CssTokens.java

+23-28
Original file line numberDiff line numberDiff line change
@@ -736,16 +736,15 @@ private boolean consumeIgnorable() {
736736
if (ast < 0) {
737737
pos = cssLimit; // Unclosed /* comment */
738738
break;
739-
} else {
740-
// Advance over a run of '*'s.
741-
pos = ast + 1;
742-
while (pos < cssLimit && css.charAt(pos) == '*') {
743-
++pos;
744-
}
745-
if (pos < cssLimit && css.charAt(pos) == '/') {
746-
++pos;
747-
break;
748-
}
739+
}
740+
// Advance over a run of '*'s.
741+
pos = ast + 1;
742+
while (pos < cssLimit && css.charAt(pos) == '*') {
743+
++pos;
744+
}
745+
if (pos < cssLimit && css.charAt(pos) == '/') {
746+
++pos;
747+
break;
749748
}
750749
}
751750
} else if (next == '/') { // Non-standard but widely supported
@@ -778,10 +777,9 @@ private boolean consumeIgnorable() {
778777
}
779778
if (pos == posBefore) {
780779
return false;
781-
} else {
782-
breakOutput();
783-
return true;
784780
}
781+
breakOutput();
782+
return true;
785783
}
786784

787785
private void breakOutput() {
@@ -846,9 +844,8 @@ private boolean consumeAtKeyword() {
846844
--pos; // back up over '@'
847845
sb.setLength(bufferLengthBeforeWrite); // Unwrite the '@'
848846
return false;
849-
} else {
850-
return true;
851847
}
848+
return true;
852849
}
853850

854851

@@ -1122,11 +1119,10 @@ private TokenType consumeString() {
11221119
pos += 2;
11231120
}
11241121
continue;
1125-
} else {
1126-
decoded = consumeAndDecodeEscapeSequence();
1127-
if (decoded < 0) {
1128-
break;
1129-
}
1122+
}
1123+
decoded = consumeAndDecodeEscapeSequence();
1124+
if (decoded < 0) {
1125+
break;
11301126
}
11311127
} else {
11321128
++pos;
@@ -1137,11 +1133,11 @@ private TokenType consumeString() {
11371133
if (closed) {
11381134
sb.append('\'');
11391135
return TokenType.STRING;
1140-
} else { // Drop <bad-string>s
1141-
sb.setLength(startOfStringOnOutput);
1142-
breakOutput();
1143-
return TokenType.WHITESPACE;
11441136
}
1137+
// Drop <bad-string>s
1138+
sb.setLength(startOfStringOnOutput);
1139+
breakOutput();
1140+
return TokenType.WHITESPACE;
11451141
}
11461142

11471143
private @Nullable TokenType consumeHash() {
@@ -1258,11 +1254,10 @@ private boolean consumeUnicodeRange() {
12581254
sb.setCharAt(bufferStart + 1, 'r');
12591255
sb.setCharAt(bufferStart + 2, 'l');
12601256
return TokenType.URL;
1261-
} else {
1262-
sb.setLength(bufferStart);
1263-
breakOutput();
1264-
return TokenType.WHITESPACE;
12651257
}
1258+
sb.setLength(bufferStart);
1259+
breakOutput();
1260+
return TokenType.WHITESPACE;
12661261
} else if (parenAfter) {
12671262
openBracket('(');
12681263
++pos;

src/main/java/org/owasp/html/Handler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ public void handle(Object x) {
5757
public void handle(Throwable th) {
5858
if (th instanceof RuntimeException) {
5959
throw (RuntimeException) th;
60-
} else {
61-
throw new AssertionError(null, th);
6260
}
61+
throw new AssertionError(null, th);
6362
}
6463
};
6564
}

src/main/java/org/owasp/html/HtmlEntities.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -2467,10 +2467,9 @@ public static int appendDecodedEntity(
24672467
if (codepoint < 0) {
24682468
sb.append('&');
24692469
return offset + 1;
2470-
} else {
2471-
sb.appendCodePoint(codepoint);
2472-
return tail;
24732470
}
2471+
sb.appendCodePoint(codepoint);
2472+
return tail;
24742473
}
24752474

24762475
private static boolean isHtmlIdContinueChar(char ch) {

src/main/java/org/owasp/html/HtmlLexer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,10 @@ protected HtmlToken produce() {
146146
state = State.SAW_EQ;
147147
// Skip the '=' token
148148
return produce();
149-
} else {
150-
// Reclassify as attribute name
151-
token = HtmlInputSplitter.reclassify(
152-
token, HtmlTokenType.ATTRNAME);
153149
}
150+
// Reclassify as attribute name
151+
token = HtmlInputSplitter.reclassify(
152+
token, HtmlTokenType.ATTRNAME);
154153
} else {
155154
state = State.IN_TAG;
156155
}

src/main/java/org/owasp/html/HtmlPolicyBuilder.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,8 @@ private HtmlTagSkipType getHtmlTagSkipType(String elementName) {
855855
if (htmlTagSkipType == null) {
856856
if (DEFAULT_SKIP_TAG_MAP_IF_EMPTY_ATTR.containsKey(elementName)) {
857857
return HtmlTagSkipType.SKIP_BY_DEFAULT;
858-
} else {
859-
return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT;
860858
}
859+
return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT;
861860
}
862861

863862
return htmlTagSkipType;

src/main/java/org/owasp/html/HtmlStreamRenderer.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,22 @@ private final void writeCloseTag(String uncanonElementName)
269269
if (!lastTagOpened.equals(elementName)) {
270270
error("Tag content cannot appear inside CDATA element", elementName);
271271
return;
272-
} else {
273-
StringBuilder cdataContent = pendingUnescaped;
274-
pendingUnescaped = null;
275-
Encoding.stripBannedCodeunits(cdataContent);
276-
int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent);
277-
if (problemIndex == -1) {
278-
if (cdataContent.length() != 0) {
279-
output.append(cdataContent);
280-
}
281-
} else {
282-
error(
283-
"Invalid CDATA text content",
284-
cdataContent.subSequence(
285-
problemIndex,
286-
Math.min(problemIndex + 10, cdataContent.length())));
287-
// Still output the close tag.
272+
}
273+
StringBuilder cdataContent = pendingUnescaped;
274+
pendingUnescaped = null;
275+
Encoding.stripBannedCodeunits(cdataContent);
276+
int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent);
277+
if (problemIndex == -1) {
278+
if (cdataContent.length() != 0) {
279+
output.append(cdataContent);
288280
}
281+
} else {
282+
error(
283+
"Invalid CDATA text content",
284+
cdataContent.subSequence(
285+
problemIndex,
286+
Math.min(problemIndex + 10, cdataContent.length())));
287+
// Still output the close tag.
289288
}
290289
if ("plaintext".equals(elementName)) { return; }
291290
}

src/main/java/org/owasp/html/PolicyFactory.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,11 @@ public <CTX> HtmlSanitizer.Policy apply(
9595
@Nullable CTX context) {
9696
if (listener == null) {
9797
return apply(out);
98-
} else {
99-
HtmlChangeReporter<CTX> r = new HtmlChangeReporter<>(
100-
out, listener, context);
101-
r.setPolicy(apply(r.getWrappedRenderer()));
102-
return r.getWrappedPolicy();
103-
}
98+
}
99+
HtmlChangeReporter<CTX> r = new HtmlChangeReporter<>(
100+
out, listener, context);
101+
r.setPolicy(apply(r.getWrappedRenderer()));
102+
return r.getWrappedPolicy();
104103
}
105104

106105
/** A convenience function that sanitizes a string of HTML. */

src/main/java/org/owasp/html/Trie.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ private Trie(
8080
this.childMap = ZERO_CHARS;
8181
this.children = ((Trie<T>[]) ZERO_TRIES);
8282
return;
83-
} else {
84-
++pos;
8583
}
84+
++pos;
8685
} else {
8786
this.value = null;
8887
}

src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ public void run() {
149149
if (failure != null) {
150150
if (failure instanceof RuntimeException) {
151151
throw (RuntimeException) failure;
152-
} else {
153-
throw new AssertionError(null, failure);
154152
}
153+
throw new AssertionError(null, failure);
155154
}
156155
}
157156

0 commit comments

Comments
 (0)