Skip to content

Commit 997f1d0

Browse files
Wohopsbenzonico
authored andcommitted
SONARJAVA-1755 Add parameter to only target long (#908)
* SONARJAVA-1755 Add parameter to only target long * Remove link to deprecated rule
1 parent db89be6 commit 997f1d0

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

java-checks/src/main/java/org/sonar/java/checks/UppercaseSuffixesCheck.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,27 @@
2020
package org.sonar.java.checks;
2121

2222
import com.google.common.collect.ImmutableList;
23-
import com.google.common.collect.ImmutableSet;
23+
2424
import org.sonar.check.Rule;
25+
import org.sonar.check.RuleProperty;
2526
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2627
import org.sonar.plugins.java.api.tree.LiteralTree;
2728
import org.sonar.plugins.java.api.tree.Tree;
2829

2930
import java.util.List;
30-
import java.util.Set;
3131

3232
@Rule(key = "S818")
3333
public class UppercaseSuffixesCheck extends IssuableSubscriptionVisitor {
3434

35-
private static final Set<Character> LITERAL_SUFFIXES = ImmutableSet.of('f', 'd', 'l');
35+
@RuleProperty(
36+
key = "checkOnlyLong",
37+
description = "Set to \"true\" to ignore \"float\" and \"double\" declarations.",
38+
defaultValue = "false")
39+
public boolean checkOnlyLong = false;
40+
41+
private static final char LONG = 'l';
42+
private static final char DOUBLE = 'd';
43+
private static final char FLOAT = 'f';
3644

3745
@Override
3846
public List<Tree.Kind> nodesToVisit() {
@@ -43,8 +51,17 @@ public List<Tree.Kind> nodesToVisit() {
4351
public void visitNode(Tree tree) {
4452
String value = ((LiteralTree) tree).value();
4553
char suffix = value.charAt(value.length() - 1);
46-
if (LITERAL_SUFFIXES.contains(suffix)) {
47-
reportIssue(tree, "Upper-case this literal \"" + suffix + "\" suffix.");
54+
switch (suffix) {
55+
case DOUBLE:
56+
case FLOAT:
57+
if (checkOnlyLong) {
58+
return;
59+
}
60+
case LONG:
61+
reportIssue(tree, "Upper-case this literal \"" + suffix + "\" suffix.");
62+
break;
63+
default:
64+
// do nothing
4865
}
4966
}
5067
}

java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S818_java.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ <h2>See</h2>
2222
</li><li> <a href="https://www.securecoding.cert.org/confluence/x/n4AtAQ">CERT DCL16-CPP</a> - Use "L," not "l," to indicate a long value
2323
</li></ul>
2424

25-
<h3>See Also</h3>
26-
<ul>
27-
<li> {rule:squid:S1129} - Long suffix "L" should be upper case
28-
</li></ul>
29-
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class A {
2+
double f = 1.;
3+
long long1 = 1l; // Noncompliant [[sc=16;ec=18]] {{Upper-case this literal "l" suffix.}}
4+
float float1 = 1.0f; // Compliant
5+
double double1 = 1.0d; // Compliant
6+
7+
private void test () {
8+
9+
long retVal = (bytes[0] & 0xFF);
10+
for (int i = 1; i < Math.min(bytes.length, 8); i++) {
11+
retVal |= (bytes[i] & 0xFFL) << (i * 8);
12+
}
13+
}
14+
}

java-checks/src/test/java/org/sonar/java/checks/UppercaseSuffixesCheckTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public void test() {
2929
JavaCheckVerifier.verify("src/test/files/checks/UppercaseSuffixesCheck.java", new UppercaseSuffixesCheck());
3030
}
3131

32+
@Test
33+
public void test_only_long() {
34+
UppercaseSuffixesCheck check = new UppercaseSuffixesCheck();
35+
check.checkOnlyLong = true;
36+
JavaCheckVerifier.verify("src/test/files/checks/UppercaseSuffixesCheckOnlyLong.java", check);
37+
}
38+
3239
}

0 commit comments

Comments
 (0)